• 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

Java storage/ Map surviving app restart  RSS feed

 
Ranch Hand
Posts: 1204
3
Java Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I wold like to implement a pesistent storage using  map  standard Java libraries. I would like that it would survive to application restarts.

For example, I have a map of key value pairs having an interface like this ;



Particularly, I dont manage to find an approach for making the app survives to restarts ...

Any idea, please?

Regards,

 
Isaac Ferguson
Ranch Hand
Posts: 1204
3
Java Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And once, I have the first version of the persistent storage implementation using Map standard libraries which survives restarting, I would like to do it without any standard library...

Any idea also for this please?
 
Saloon Keeper
Posts: 5150
54
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To persist a Map of anything other that Strings will involve serialization.

If you can get away with values that are Strings you can use Properties but it would also be easy enough to roll your own.
 
Isaac Ferguson
Ranch Hand
Posts: 1204
3
Java Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,



Okay, so I move forward with serialization.



Okay, so properties could be used, but only when values are not Strings, isnt it?

And when values would be string, which approach should be better ,please?

Regards,
 
Carey Brown
Saloon Keeper
Posts: 5150
54
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Isaac Ferguson wrote:Okay, so properties could be used, but only when values are not Strings, isnt it?


Properties, only when values ARE strings. If they aren't strings then you'd have to provide ways to convert them to/from strings.
 
Isaac Ferguson
Ranch Hand
Posts: 1204
3
Java Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically, I would like that the map would behave like a persistent storage surviving restarts. This approach creates a HashMap & fill it with pairs of values - keys, then I just exit the program and run it again. The first issue is that, as I am creating the HashMap before put the content in the HashMap within the main method, it always is empty after retart because a new HashMap is created for every execution. Any idea to avoid this, please?

I am working on the first part of the solution, the code looks like this;




I have wrote a class to save the content of the map into memory.




 
Saloon Keeper
Posts: 5044
134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have occasionally used the Java Preferences API for persistent storage. That is generally tied to the account running the code -which may or may not be a problem in your situation-, but it avoids having to deal with files, which comes with its own set of problems in some circumstances.
 
Isaac Ferguson
Ranch Hand
Posts: 1204
3
Java Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes I am going to check the JAVA Preferences API, during the meantime I re wrote it this way. The thing is that as the main method run every time a new hashMap is created everytime so the one from memory gets deleted because of that and that should be avoided...Any idea, please?

 
Isaac Ferguson
Ranch Hand
Posts: 1204
3
Java Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just read about the JAVA API Preferences but for my case I need to do it using standard Java libraries.

On the other hand, I want to do it without using any library...
 
Rancher
Posts: 3757
40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just reading through that main() method and this is what I see.

1. It creates an instance of the PersistentStorage class (xd), which is there just to load and savea Map.
2. It then creates a Map, and populates it with various entries.
3. If the created Map has a key for "keyNull" it then replaces the Map with one read in.
4. It saves the Map.

Now, since in step 2 one of the entries it adds is for "keyNull", then step 3 always happens.

So the whole of  step 1 is pointless as it always gets replaced anyway.

ETA:  indeed, step 4 is also pointle since at the moment it simply writes back the same data it has just read out.
 
Isaac Ferguson
Ranch Hand
Posts: 1204
3
Java Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I am trying to avoid that after removing  "keyNull" it input it again into the map.

I am thinking in a conditional at the very beginning for check if the map instance is containing it. When not it would mean that the program was already ran and that the data is stored on memory so it should be loaded instead of create a new map with again the same corrupt content "keyNull"
 
Tim Moores
Saloon Keeper
Posts: 5044
134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Isaac Ferguson wrote:I just read about the JAVA API Preferences but for my case I need to do it using standard Java libraries.


Does not compute. The Preferences API is part of the JRE class libraries. Surely that qualifies as "without using any libraries".
 
Dave Tolls
Rancher
Posts: 3757
40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a feeling there's a disconnect here somewhere.

You say:


I am thinking in a conditional at the very beginning for check if the map instance is containing it. When not it would mean that the program was already ran and that the data is stored on memory so it should be loaded instead of create a new map with again the same corrupt content "keyNull"



But when you run the code the Map does not exist, ever, until the line that creates it as an empty one.

Normally, for data that is intended to survive between executions of a program, you would load that data on start (or when it was needed).
So I would expect your Map to be loaded first, if it exists.
If it doesn't exist then an empty one would be used instead.
 
Isaac Ferguson
Ranch Hand
Posts: 1204
3
Java Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anyone can think of how a distributed version of this kind of persistent storage could be created? By distributed I mean existing with the same state in multiple processes at the same time.. DistributedMap from IMB is there, but I want to understand how it could be done just using text I dont want to program it at the moment.

Any idea, please?
 
Tim Moores
Saloon Keeper
Posts: 5044
134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can distribute any changes happening in one of the processes using a messaging system to the others.
 
Isaac Ferguson
Ranch Hand
Posts: 1204
3
Java Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Normally, for data that is intended to survive between executions of a program, you would load that data on start (or when it was needed).
So I would expect your Map to be loaded first, if it exists.



Yes indeed, the thing is where to create it for the first time, because if I want it to survive restarts (stored in memory) and this small program has only one class containing the main and all the other methods...
 
Isaac Ferguson
Ranch Hand
Posts: 1204
3
Java Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You can distribute any changes happening in one of the processes using a messaging system to the others.



A messaging system..do you mean like RMI?
 
Carey Brown
Saloon Keeper
Posts: 5150
54
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want multiple running instances to be able to read and write to some persistent resource, why not use a database?
 
Isaac Ferguson
Ranch Hand
Posts: 1204
3
Java Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe just for conceptualize a properties file could be lighter?
 
Isaac Ferguson
Ranch Hand
Posts: 1204
3
Java Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

If you want multiple running instances to be able to read and write to some persistent resource, why not use a database?



And when having just one instance running in several proceses, I think in using Spring ioc but what when using just Java, maybe Singleton?
 
Tim Moores
Saloon Keeper
Posts: 5044
134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Isaac Ferguson wrote:
A messaging system..do you mean like RMI?


RMI is not a messaging platform. I mean something based on JMS, to name the relevant Java API.
 
Tim Moores
Saloon Keeper
Posts: 5044
134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Isaac Ferguson wrote:Maybe just for conceptualize a properties file could be lighter?


Files are tricky if multiple processes are involved - synchronization is a difficult problem.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why don't you back the map with an sql database?
 
Isaac Ferguson
Ranch Hand
Posts: 1204
3
Java Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

And when having just one instance running in several proceses, I think in using Spring ioc but what when using just Java, maybe Singleton?



I dont know if this could make some much sense, but maybe using the singleton pattern (same that Spring uses) could bring some benefits for the solution?
 
Isaac Ferguson
Ranch Hand
Posts: 1204
3
Java Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

But when you run the code the Map does not exist, ever, until the line that creates it as an empty one.

Normally, for data that is intended to survive between executions of a program, you would load that data on start (or when it was needed).
So I would expect your Map to be loaded first, if it exists.
If it doesn't exist then an empty one would be used instead.



Yes I am missing a condition to make it happen. Any idea, pelase?
 
author
Posts: 23811
140
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Isaac Ferguson wrote:
I dont know if this could make some much sense, but maybe using the singleton pattern (same that Spring uses) could bring some benefits for the solution?



What would be the purpose of a singleton in this use case? How would it help here?

Henry
 
Isaac Ferguson
Ranch Hand
Posts: 1204
3
Java Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well I am assuming that only one instance gets replicated in different processes... I am not sure if that matches the question "how could a distributed system (existing with the same state in multiple processes at the same time)". I mean make the previous persistent map distributed (Different instances?)
 
Carey Brown
Saloon Keeper
Posts: 5150
54
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You would probably need something like a map server that processes can access and can be the traffic cop as to who gets to update an entry.
 
Isaac Ferguson
Ranch Hand
Posts: 1204
3
Java Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your comment applies to the use of a singleton isnt it? The map server would not be needed when having multiple instances...?
 
Carey Brown
Saloon Keeper
Posts: 5150
54
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Isaac Ferguson wrote:Your comment applies to the use of a singleton isnt it? The map server would not be needed when having multiple instances...?


Maybe I misread. I thought that you had the case where multiple running programs would access and update the same Properties, Preferences, or some other form of persisted data. Having a Singleton would only control access inside a single running program. A Singleton might be necessary if you have multiple threads trying to update the persisted data but it wouldn't help in the case of multiple programs running unless each running program gets its own persisted file(s).
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!