How to access HA Singleton MBean clusterwide via EJB
twwwt Aug 17, 2004 2:06 PMHello!
I'm trying to access a HA Singleton MBean from session EJBs but experiencing a problem which really makes me ill. First the EJB looks up the RMIAdaptor:
Properties properties = new Properties(); properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory"); properties.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces"); properties.put(Context.PROVIDER_URL, "jnp://localhost:1100"); try { Context context = new InitialContext(properties); Object o = context.lookup("jmx/invoker/RMIAdaptor"); mbeanServer = new RMIConnectorImpl((RMIAdaptor) o); } catch (NamingException e) { handleException(e); }
Then I test wheter the singleton service is registered before invoking methodes on the service:
ObjectName name = new ObjectName("myDomain:service=MyService"); Boolean ok = mbeanServer.isRegistered(name); if (ok) { Object result = mbeanServer.invoke(...); }
The problem is on the nodes which are currently not master. As expected "isRegistered()" returns false and I can't invoke the MBean. On the node which is master everything works fine.
And now, most incomprehensible to me is that nearly the same code put in a simple test class works really ok:
String port = "1100"; String[] clusterMembers = {"192.168.2.10", "192.168.2.25", "192.168.2.30"}; ObjectName name = new ObjectName("myDomain:service=MyService"); RemoteMBeanServer server = null; int i = 0; while (i < hosts.length) { properties.put(Context.PROVIDER_URL, "jnp://" + hosts + port); Context context = new InitialContext(properties); Object o = context.lookup("jmx/invoker/RMIAdaptor"); server = new RMIConnectorImpl((RMIAdaptor) o); if (server.isRegistered(name)) { System.out.println(i + " " + clusterMembers + " " + server.isRegistered(name)); System.out.println(server.getAttribute(name, "MasterNode").toString()); } i++; }
For every loop the isRegistered() returns true and "MasterNode" is also true. The conclusion to me is that for the EJB I get somthing like a "local" RMIAdaptor with the HA singleton service available only on the master node. For the test class, started in another VM, I always get something like a "global" RMIAdaptor with the HA singleton service available.
Is that correct? Or do I completely mixed something up.
And most importantly: What is the "safe" way to access HA singleton MBean from EJBs.
(Please note that all the nodes really build ONE cluster, i.e. they find each other and I can see messages on the console telling me that too.)
Thanks a lot for your answers.
Oh, I forgot to say that I use V3.2.6RC1.
Thorsten