production environment - Shared java HashMap in a clustered enviroment -
i've client application requesting information url every 1 second.
in server (a servlet & jsp application), in order avoid db access when it's not necessary, it's been implemented next solution. here's snippet:
//a static hashmap save last record inserted in db public static map<long, long> values = new hashmap<long, long>(); // lastrecordread sent client if (values.get(id) != lastrecordread) { //access database information //cause last value read different last record inserted ... }else{ //do nothing //it's not necessary access db cause parameters match }
this working expected in development environment.
the problem comes when have clustered environment. have server deployed in 2 nodes (using jboss), each 1 own hashmap, , own values. depending on node attack, can different values...
¿is there way share hashmap between 2 nodes? looking answer don't need keep 2 maps updated, meaning not calls between nodes...
any kind of appreciated.
edit: i'm playing hazelcast, , seems easy i'm afraid i'm doing wrong...
in server i'm using hazelcast instead of hasmap:
public static map<long, long> values = (hazelcast.newhazelcastinstance(new config())).getmap("values");
when records inserted:
if (((values.get(id) == null)||(values.get(id) < lastidinserted))) { values.put(id, lastidinserted); }
when server called client app:
// lastrecordread sent client if (values.get(id) != lastrecordread) { //access database information //cause last value read different last record inserted ... }else{ //do nothing //it's not necessary access db cause parameters match }
and think, that's all. confirm if ok or missing something..? solution populating on nodes? i've been doing tests 2 tomcats , work, work differents ips?
thanks in advance!
you have 2 options:
- use distributed key-value like: http://memcached.org/
...and tons of others.
use 'publisher-subscriber' concept , update each hashmap instance events. typically implemented jms broker:
http://docs.oracle.com/cd/e19717-01/819-7759/aerbk/index.html https://www.rabbitmq.com/tutorials/tutorial-three-java.html
the choice depends on needs: fastest read , seek, without networking delays slow updates - use second option. solution data not changing frequently: geographical names, addresses , on.
as general case - use fist one.
Comments
Post a Comment