Question

I am developing java library that calls a HTTP script on one of multiple server to process data. The core functionality works ok and now I am looking into configuration issues.

The library needs a few information (URL,Username,Password,...) of the server and if the connection fails it needs to get another until it gets response (simple High Availability). And there can be multiple different set of servers (i.e. for standard vs high-priority requests). Maybe even the order of the servers can be randomized for load balancing. And in the future we may want to implement some sort of server state monitoring into this mechanism.

I have to add that I am new to java so my ideas may be biased to solutions from other languages. My first idea was to create class for storing single server config and then some sort of configuration manager that would be iterable (it would return iterator iterating over the server configs) and serializable. That way the library could get iterator and iterate over servers until it gets valid response (or run out of servers). And users of our class could use whichever mechanism for storing the configuration they need.

I tried to implement this and I ran into some questions:

  • How should iterator access the data of the configuration manager - the server configurations themselves and any other data that may be relevant to iterating? Should the configuration manager "render" some sort of simple list when creating the iterator? Or should the iterator access configuration manager's internal fields? Or should the configurationManager be the iterator itself?

  • Is there any way how to extend serialization of the class if it would need to use fields that are not serializable (probably it will non happen but I want to be ready if it does)

Is this a viable approach or should I rather use any library (Commons configuration or Java Properties)?

Was it helpful?

Solution

For a single application, a HashMap is serializable and can be saved to a file then read later. You don't need to save your whole class, just the HashMap. Your class can still manage the HashMap.

HashMap works on key-value pairs. (e.g. key = "password" and value = "secret")

From what you describe, you have a set of known parameters you want to save, so when you load your HashMap you can lookup the values using the key, it is fast. You would not need to iterate. But you can iterate on a HashMap if you needed to.

To store configurations for multiple systems you could have a collection (e.g. ArrayList) of HashMaps. ArrayList is also serializable so you can write the entire list to a file and retrieve it, and it is very simple to iterate.

To have a custom way of ordering your selections, a priority queue with a custom comparator allows that. If you wanted to randomize the selection method I think this would require creating parallel priority queues referring to the same content, but each with different comparator methods, then randomly choose which queue to read from. I am thinking along the lines of a database table with multiple indices. You have the same set of rows but depending which index you choose you can traverse them in different ways.

If you are thinking of having a configuration shared by multiple systems you probably want to store your configuration in a database, but it sounds like this is not the case.

OTHER TIPS

I'd probably have a configuration service.

public interface ConfigurationService {
    Iterable<Configuration> getAllConfigurations();
    Configuration getConfiguration(String id);
    void saveConfiguration(Configuration config);
}

Then you can do whatever you like in the future (property files, database, spring config, clustered, test, etc. etc.).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top