Win a copy of Terraform in Action this week in the Cloud 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Grouping/Repeating application properties

 
Marshal
Posts: 5157
321
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 8770
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 7178
166
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 5157
321
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2899
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How about Json format ?
 
salvin francis
Bartender
Posts: 2899
150
Google Web Toolkit Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Tim Cooke
Marshal
Posts: 5157
321
IntelliJ IDE Python Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thought you could just do that with yml?

 
salvin francis
Bartender
Posts: 2899
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Marshal
    Posts: 5157
    321
    IntelliJ IDE Python Java Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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: 2899
    150
    Google Web Toolkit Eclipse IDE Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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: 22510
    122
    Eclipse IDE Spring VI Editor Chrome Java Windows
    • Likes 2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
    Marshal
    Posts: 5157
    321
    IntelliJ IDE Python Java Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
     
    You showed up just in time for the waffles! And this tiny ad:
    Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    reply
      Bookmark Topic Watch Topic
    • New Topic