• 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
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

Grouping/Repeating application properties  RSS feed

 
Marshal
Posts: 4618
298
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?
 
Bartender
Posts: 5776
56
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: 5413
143
Android Firefox Browser Mac OS X Safari Tomcat Server VI Editor
  • 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
Marshal
Posts: 4618
298
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: 2232
94
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: 2232
94
Eclipse IDE Google Web Toolkit Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Tim Cooke
Marshal
Posts: 4618
298
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: 2232
94
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
    Marshal
    Posts: 4618
    298
    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: 2232
    94
    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: 21719
    102
    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
    Marshal
    Posts: 4618
    298
    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.
     
    Bras cause cancer. And tiny ads:
    Create Edit Print & Convert PDF Using Free API with Java
    https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!