This week's book giveaway is in the Spring forum.
We're giving away four copies of Spring in Action (5th edition) and have Craig Walls on-line!
See this thread for details.
Win a copy of Spring in Action (5th edition) this week in the Spring 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
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Grouping/Repeating application properties  RSS feed

 
Sheriff
Posts: 4568
286
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: 5144
54
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: 5040
134
  • 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: 4568
286
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.
 
Bartender
Posts: 1973
57
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about Json format ?
 
salvin francis
Bartender
Posts: 1973
57
Eclipse IDE Google Web Toolkit Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Tim Cooke
Sheriff
Posts: 4568
286
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
Bartender
Posts: 1973
57
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: 4568
    286
    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
    Bartender
    Posts: 1973
    57
    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: 21464
    97
    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: 4568
    286
    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.
     
    Don't get me started about those stupid light bulbs.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!