EJB fail-over
jboss2005_01 Dec 2, 2005 11:45 AM
Hello,
I have the following situation:
1. Two JBoss 4.0.2 servers running on WinXP PRO and Suse 9.2 configured as a cluster using the all configuration
2. I create a very simple EAR containing a stateful session bean with the clusterable attribute set in the deployment descriptor and deployed by farming to both application servers.
3. A simple J2SE client application (command line) that connects to the session bean and calls a string returning "Hello-World"-like method.
Both members in the cluster are aware of eachother because when the client application creates several instances to the session bean, they are load-balanced between the two servers.
The problem occures when I shutdown one of both instances of Tomcat. Connections made to this server get lost although I would like to use the remaining to process the requests. It seems that fail-over is not working for my configuration. Does anyone know some good documents/examples to achieve this kind of behaviour? I really like JBoss so it would be a pitty to notice that such stuff would not be possible ;-)
Thanks for the advise !!!!
Regards,
Kurt
The client application
package client; import java.util.*; import javax.naming.*; import javax.rmi.*; import server.ejb.*; public class ClusterClient { public ClusterClient() { for(int j = 0 ; j < 5 ; j++){ Runnable runner = new ClusterClientRunner(); Thread thread = new Thread(runner, "Runner " + j); thread.start(); try{ Thread.sleep(1000); }catch(InterruptedException e){ } } } private class ClusterClientRunner implements Runnable{ public void run(){ System.out.println(Thread.currentThread().getName() + " started."); try{ Properties props = new Properties(); props.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory"); //props.setProperty(Context.PROVIDER_URL, // "192.168.3.120:1100,192.168.3.159:1100"); Context ctx = new InitialContext(props); StatefullSessionRemoteHome home = (StatefullSessionRemoteHome) PortableRemoteObject.narrow( ctx.lookup("StatefullSessionBean"), StatefullSessionRemoteHome.class); StatefullSessionRemote bean = home.create(); for(int i = 0 ; i < 20 ; i++){ System.out.println(Thread.currentThread().getName() + " triggering with ID = " + i); bean.executedAt(Thread.currentThread().getName() + " triggering with ID = " + i); try{Thread.sleep(2500); }catch(InterruptedException e){} } }catch(Exception ex){ ex.printStackTrace(); } System.out.println(Thread.currentThread().getName() + " completed."); } } public static void main(String[] args) { new ClusterClient(); } }
The session bean's code
package server.ejb; import javax.ejb.*; import javax.naming.*; public class StatefullSessionBean implements javax.ejb.SessionBean, server.ejb.StatefullSessionRemoteBusiness, server.ejb.StatefullSessionLocalBusiness { private javax.ejb.SessionContext context; private static int counter; public void setSessionContext(javax.ejb.SessionContext aContext) { context = aContext; } public void ejbActivate() { } public void ejbPassivate() { } public void ejbRemove() { } public void ejbCreate() { } public String showMessage() { StringBuffer buffer = new StringBuffer(); buffer.append("Bean executed at : " + (String)System.getProperty("server")); System.out.println(counter + " == " + buffer.toString()); counter++; return buffer.toString(); } public void executedAt(java.lang.String location) { System.out.println("Triggered by : " + location); } }
The deployment descriptor
<?xml version="1.0" encoding="UTF-8"?> <jboss> <enterprise-bean> <session> <ejb-name>StatefullSessionBean</ejb-name> <jndi-name>StatefullSessionBean</jndi-name> <clustered>True</clustered> <cluster-config> <partition-name>DefaultPartition</partition-name> <home-load-balance-policy> org.jboss.ha.framework.interfaces.RoundRobin </home-load-balance-policy> <bean-load-balance-policy> org.jboss.ha.framework.interfaces.RoundRobin </bean-load-balance-policy> <session-state-manager-jndi-name> /HASessionState/Default </session-state-manager-jndi-name> </cluster-config> </session> </enterprise-bean> </jboss>