get servlet context variable data in tomcat cluster environment - java

Currently I am developing module to display list of online user in my application. I am using comet streaming technology. When users log in I put data in map and then sending data in message queue. Now message queue is stored in servlet context.
Now problem I am facing is it is working in local environment but it is not working in production environment because in production environment i have set up tomcat cluster. so data set in servlet context for tomcat 1 is not accessible in tomcat 2.
I have already develop module but not getting any way to solve above issue. I google and found that tomcat doesn't support context replication.
I have one doubt that how many JVM instance will be created in tomcat cluster web application. e.g I have two tomcat cluster.

I would not use servlet context to store data for a cluster. The common pattern is to use a database for data that must be shared across different servers.
For your use case, it is no need persisting the values between different runs, so the database is not necessarily a nice solution, even if it is easy to setup. IMHO what you need it just a shared data cache or better a memory data grid. hazelcast should be easy to use for your requirements. If I correctly understand them, what you need is a distributed map, with a concatenation of node_id, session_id as key (or maybe simply session_id), and a user object as value.

In tomcat7 this requires writing a custom valve to force replication, the same is true in tomcat 6. Refer to Is there a useDirtyFlag option for Tomcat 6 cluster configuration? to see how to do this.

Related

two tomcat sharing the cache and supporting each other in failure situation

Please advise as I am stuck up with this following scenario that is let's say there is a client who sends the request to the Apache server.
On the other side there are two tomcat servers running parallel on different ports in the cluster, and on both the tomcat servers the same war file is deployed.
Now the problem is that let's say if the client sends the request to apache server and apache server sends the request to the first tomcat and in that first tomcat cache was written and then the first tomcat goes down after writing the cache.
And then comes the second request from a client which again goes to apache server and then apache server redirect it two-second tomcat since the first tomcat is down and it also sends the cache of the first tomcat to the second tomcat please advise how we can achieve this scenario,
The purpose is that I do not want the application to suffer even if the first tomcat goes down. and also I am looking for best clustering that can be happened in tomcat as I am expecting heavy traffic from the end client
Have a look at distributed caches. They solve this kind of challenges.
Options are:
Ehcache/Terracotta
Redis
Memcached
Hazelcast
Bozho's tech blog has a good and current overview. You might want to read it.
Update
The challenge you are facing is that the cached data needs to be available to all Tomcat instances and that the cached data needs to stay consistent if it is modified and queried on several Tomcat instances.
That's exactly what these distributed caches provide:
They write modified data to a shared storage or send it to other instances so it will be available to other instances if needed.
They keep the distributed data consistent so that no Tomcat instance uses outdated data or modifies it in a way that would break consistency.
The distributed caches usually use a two stage concept where the some of the data is kept in memory and everything is persisted on disk.
So by using the distributed cahce, your second Tomcat instances can serve the clients of the first instance in case that instance goes down.

Heterogeneous Java EE cluster

Is it possible to form a cluster in which there are different types of application servers? For instance, 1 JBoss, 1 Glassfish and 1 WebSphere? Lets assume we are using EJB3.0.
Stateless session beans should be relatively easy and simple load balancing among the instances should do the work, but what about SFSBs and session replication? Is it possible to utilize some cache storage like infinispan for it?
I would appreciate any comments or sharing your experience on this topic.
I assume it may be possible if you use some application server agnostic solution like Hazelcast. According to its documentation it's pretty easy to configure web session replication and the only requirements it has are
Target application or web server should support Java 1.5+
Target application or web server should support Servlet 2.4+ spec
Session objects that needs to be clustered have to be Serializable
I've not tried to configure a cluster the way you've described, however I think it may do the trick.
The responce is simply NO. Clusturing is a non standard feature, it is up to the Java EE implementation to provide clustoring keeping standard behaviour (with very litle constrains, as stickiness is expected and session object are expected to be serializable) and no interoperability is forseen.
You can of course made the cluster your self, setting up an external data grid to serve as session store and manage your self the cache, but then you will lose any framework functionality related to the session (you will need to do every thing by your self) and what the point any more to use a full Java EE application server. Yes you will then need to forget about SFSBs.
I am ready curous what issue you want to solve by this type of architecture. I don't see any that can over come the cost of maintining 3 differents apps (app server have slite difference on the dev side) and more importantly 3 differents infrastructure operation stack (on this side there is lot of difference, so you need to multiply the opperation team knowlages).

Java Memcached not replicating entries across instances

I am using xmemcached-1.3.5 jar version of MemCache server.
Now, I have two instances of this server on different machine. I am
using xmemcached-1.3.5 jar to make entry in memcache-server through my
java application.
When both servers are up, the entry is made on only one of the MemCache instances.
Is there any configuration that needs to be made to get the entries duplicated onto both instances?
Memcached does not provide replication. You will need to investigate repcached if you want your cached data replicated.

Java EE Application-scoped variables in a clustered environment (Websphere)?

Is there any easy way in a Java EE application (running on Websphere) to share an object in an application-wide scope across the entire cluster? Something maybe similar to Servlet Context parameters, but that is shared across the cluster.
For example, in a cluster of servers "A" and "B", if a value is set on server A (key=value), that value should immediately (or nearly so) be available to requests on server B.
(Note: Would like to avoid distributed caching solutions if possible. This really isn't a caching scenario as the objects being stored are fairly dynamic)
I'm watching this to see if any simple solutions appear, but I don't know of any myself. Last time I asked something like this, the answer was to use a distributed object store.
Our substitute was manual notification over HTTP to a configured list of URLs, one for each Web Container's direct server:port combination. (That is, bypassing any fronting proxy/web server/plugin.)
Try using the WebSphere workarea

Creating an application instance specific cache for Java/WebLogic Web Services

I'm new to both J2EE and WebLogic. I'd trying to determine the best way to implement a non-distributed cache (one cache per application instance) in a Java Web Services application running on WebLogic 10.3. I need to cache several different POJO's.
There will be multiple WebLogic instances running on each server in a cluster. When reading about ServletContext and InitialContext, I was a bit confused. I believe ServletContext is instance specific, but I can only access it from a Servlet, correct? I will need to access to the cache in separate threads so I'm not sure if this is possible outside of a Servlet.
I was reading a bit about JNDI, but it seems to work at the server or cluster level and not for each WebLogic/application instance.
Can anyone provide me with a suggestion and a code example to initialize, access, and destroy a cache of Java POJO's?
Thanks!
Leon
Here is an example on how to implement a method cache with Spring and EHCache:
http://opensource.atlassian.com/confluence/spring/display/DISC/Caching+the+result+of+methods+using+Spring+and+EHCache
The cache will be local if configured as in the example.
I am using this method in a web service client library to cache the results of a frequently used service that has nearly no update to its data.

Categories