Win a copy of Java XML & JSON this week in the XML and Related Technologies forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Grouping/Repeating application properties  RSS feed

 
Sheriff
Posts: 4585
288
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm writing an application that listens to messages sent from remote systems and I'd like to configure the details of the hosts in a config file. It sounds simple but I'm stuck.

If it were listening from a single remote system then I can configure the details in a properties file like so:

Here's where I'm stuck. I want to be able to define an arbitrary number of servers in my config file and have the Java application iterate over them to create multiple listener connections. The config file may look something like this:

I'm using Spring Boot so by default it supports .properties and YAML configurations, but I'm open to do something else if it fits better.

Any ideas?
 
Saloon Keeper
Posts: 5459
55
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the number of hosts is small you could do something like this with a properties file:

 
Saloon Keeper
Posts: 5219
143
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since what you showed looks like INI anyway, why not actually use that format? The ini4j library makes that easy.
 
Tim Cooke
Sheriff
Posts: 4585
288
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Both good ideas. I think I'll take a deeper look into using an ini format, I've not programmed with one of those before. It looks like an MS format which might have something to do with it as I've not touched Windows for over a decade.
 
Saloon Keeper
Posts: 2134
80
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about Json format ?
 
salvin francis
Saloon Keeper
Posts: 2134
80
Eclipse IDE Google Web Toolkit Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Tim Cooke
Sheriff
Posts: 4585
288
Clojure IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
json would be ok if it were written by a computer, just as xml would also be but it's less friendly as a config file format intended to be written by a real person because the syntax is fussy to get right.
 
Greenhorn
Posts: 23
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought you could just do that with yml?

 
salvin francis
Saloon Keeper
Posts: 2134
80
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are pros and cons to all types of format.

Json vs other formats

Pros :
  • json has less content as opposed to XML
  • json has hierarchy as opposed to simple properties file
  • data type is clear: String/number/boolean/null/array


  • Cons:
  • No comments can be added
  • No new lines in data



  • XML vs other formats
    Pros :
  • Well defined hierarchy as opposed to properties file
  • Hierarchy can be enforced by xsd and users can use an editor that disallows invalid hierarchy
  • Data type can be defined in xsd


  • Cons:
  • Lots of meta-data


  • Add your own points to my list.
     
    Tim Cooke
    Sheriff
    Posts: 4585
    288
    Clojure IntelliJ IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    YAML is interesting. How would I process that on the Java side?
     
    Beeimbolo Paul
    Greenhorn
    Posts: 23
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Here is an article on how to autowire it: https://www.fortisfio.com/yaml-file-mapping-values-to-object-list-with-spring-boot/

    The formatting is off on my example but you get the general idea, anyway hope it helps!
     
    salvin francis
    Saloon Keeper
    Posts: 2134
    80
    Eclipse IDE Google Web Toolkit Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Tim Cooke wrote:... config file format intended to be written by a real person because the syntax is fussy to get right.


    People get properties file wrong too. e.g.
     
    Sheriff
    Posts: 21601
    101
    Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
    • Likes 2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Tim Cooke wrote:YAML is interesting. How would I process that on the Java side?


    If you can't use Spring Boot, you can use Jackson. A quick example:
     
    Tim Cooke
    Sheriff
    Posts: 4585
    288
    Clojure IntelliJ IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Spring Boot is definitely on the menu, so should have little trouble with YAML. I had to pick up something else for a bit but I'll be back on it in a few days and I'll let you know what I settle on.

    Thanks for all the input, most helpful.
     
    All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
    RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
    https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!