• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Is it possible to access HashMap data in one application from another Java application?

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
I have a situation here. I need to load around 2 GB of data in to hash map. No memory constraints. This data will be loaded in to a hashmap via a Jar utility.
There is one different application which needs to access this hashmap. Both the application and Jar will be deployed on Tomcat.

The reason I am not making Jar as a component of the application is that every time when the application restarts, I don't want the hashmap to be reloaded.

Hashmap should be reloaded only when the server restarts.

Is there a way this can be done?

Thank you in advance.
J
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could certainly write another application whose only purpose is to provide a service which encapsulates that Map. Your "different application" would then send a request to that service and receive data from the Map in the response. It would be a very simple application because (as far as I can see) there is only one request to support.
 
Jujhar Khurana
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you so much. Let me try this.
 
Saloon Keeper
Posts: 27763
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note that this only works if your "different application" is running in the same VM.

Java VMs don't share memory between them. To get an object from one JVM to another JVM requires serializing the object in question and then deserializing to the receiving VM. If the object isn't serializable, you're out of luck. In any event, you'd require another 2GB of RAM to hold the second copy, plus a lot of time to ship the copy. To save nothing of having to add extra logic if you wanted to keep the copies in sync after changes to the data.

So shared access to a HashMap is limited. Two webapps in the same server could make arrangements (although this would mean pushing the object up into the server's classpath, since webapps have independent sets of classes). Or if it's not a webapp, some other sort of multi-application container (such as Apache Karaf) would have to be used. Or you could roll your own equivalent.

On the whole, it's usually easier to simply share a basic name/value NoSQL database like Oracle Berkeley DB. With maybe a cache.
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, I was assuming that the whole map occupied 2 GB, not that asking for a particular value from the map would return 2 GB of data. (I think that my assumption is supported by the OP better than your assumption is.) And I was assuming that we wouldn't be forced to use Java serialization but that we could use (say) JSON to format the values.

However your suggestion of using a database to contain the data, I agree with that part. Too often people decide on an in-memory solution because they subconsciously feel that accessing a database couldn't possibly be fast enough, but often (usually?) that feeling isn't correct.
 
Tim Holloway
Saloon Keeper
Posts: 27763
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I didn't assume that each Map entry would return 2 GB (Yikes!), but nevertheless, 2 JVMs = 2 maps, and at 2 GB per copy of the Map, that's 4 GB right there.
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My idea was that the Map would be in one place only and that a request to that place would get a single Map entry. And I furthermore assumed that there would be a large number of small entries. However there's a lot of assumptions going on there and we really don't have that much information.
 
Tim Holloway
Saloon Keeper
Posts: 27763
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Like I said, though, a Map is directly visible only in the VM that contains it, so only if both applications are running in the same VM would that work and even then only if they're not running on divergent classpaths the way that webapps and OSGi bundles do.

To access a Map from a different VM, either you have to replicate that map in the other VM or provide an alternative Map class in the other VM that makes cross-VM calls to access the data. Which is basically what things like the BerkelyDB database do already.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why does everything need to be loaded in memory all at once?
 
Tim Holloway
Saloon Keeper
Posts: 27763
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It was stated as a condition of the problem.

Whether it's a valid condition or not is another matter.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which is why asking why is a valid question.

This has a red flag waving in the breeze as a very possible XY Problem.
 
Jujhar Khurana
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you again for putting your thoughts on this.

Around 2 GB of data has to be loaded in memory to act as a Cache. I know there are many other solutions like Ehcache or hazelcast but I am not allowed to use them due to policies or approval. As Paul quoted, writing another app which will have the Map part and making it a restful service deployed on the same server (to reduce the latency if at all)
seems a good solution to me. This will also facilitate changing other part of the application which would now be a separate WAR file, as and when required, without having to restart the other (cache) WAR and there by loading the cache again from scratch.

 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:Which is why asking why is a valid question.



So now we know: there are PHB's at work here.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jujhar Khurana wrote:Around 2 GB of data has to be loaded in memory to act as a Cache. I know there are many other solutions like Ehcache or hazelcast but I am not allowed to use them due to policies or approval.


So you have to write a complex, error-prone, space-hogging, possibly hackable and probably slow piece of custom software from scratch, and test it like crazy, because someone has decided that an existing, well-written, fast, and track-tested product, written by people who have probably forgotten more than you or I will ever know about caches, needs to be "approved"?

Definitely PHB-ware.

My advice:
1. Go with Tim's suggestion.
2. Find a good 3rd party caching product (maybe even a Java-based one) and get it approved.
3. (as Bear suggested): Examine the reasons why you need (or think you need) to load two gigabytes of data into a program in the first place.

Winston
 
Jujhar Khurana
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for the suggestions. Now I am going to try Hazelcast or Ehcache or Gemfire and see which one fits my needs and also can be approved

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic