[jboss-user] [Clustering/JBoss] - Re: Load-balance the invocation of a MBean in the cluster fr
bstansberry@jboss.com
do-not-reply at jboss.com
Tue Dec 9 16:36:27 EST 2008
First, the service implementation:
| package org.jboss.test.cluster.invokerha;
|
| import java.lang.reflect.Method;
| import java.lang.reflect.InvocationTargetException;
| import java.lang.reflect.UndeclaredThrowableException;
| import java.security.Principal;
| import java.util.Collections;
| import java.util.HashMap;
| import java.util.Map;
|
| import org.jboss.ha.jmx.HAServiceMBeanSupport;
| import org.jboss.invocation.Invocation;
| import org.jboss.invocation.MarshalledInvocation;
| import org.jboss.logging.Logger;
| import org.jboss.security.SecurityAssociation;
| import org.jboss.system.Registry;
|
| public class HAService
| extends HAServiceMBeanSupport
| implements HAServiceRemote, HAServiceMBean
| {
| private static final Logger log = Logger.getLogger(HAService.class);
|
| private int count = 0;
|
| private Map marshalledInvocationMapping;
|
| public void startService()
| throws Exception
| {
| super.startService();
|
| // Calulate method hashes for remote invocation
| Method[] methods = HAServiceRemote.class.getMethods();
| HashMap tmpMap = new HashMap(methods.length);
| for(int m = 0; m < methods.length; m ++)
| {
| Method method = methods[m];
| Long hash = new Long(MarshalledInvocation.calculateHash(method));
| tmpMap.put(hash, method);
| }
| marshalledInvocationMapping = Collections.unmodifiableMap(tmpMap);
|
| // Place our ObjectName hash into the Registry so invokers can resolve it
| Registry.bind(new Integer(serviceName.hashCode()), serviceName);
| }
|
| public void stopService()
| throws Exception
| {
| super.stopService();
|
| // No longer available to the invokers
| Registry.unbind(new Integer(serviceName.hashCode()));
| }
|
| /**
| * Expose the client mapping
| */
| public Map getMethodMap()
| {
| return marshalledInvocationMapping;
| }
|
| /**
| * This is the "remote" entry point
| */
| public Object invoke(Invocation invocation)
| throws Exception
| {
| log.info("Invoked");
|
| // Invoked remotely, inject method resolution
| if (invocation instanceof MarshalledInvocation)
| {
| MarshalledInvocation mi = (MarshalledInvocation) invocation;
| mi.setMethodMap(marshalledInvocationMapping);
| }
| Method method = invocation.getMethod();
| Object[] args = invocation.getArguments();
|
| log.info("Invocation of " + method);
|
| // Setup any security context (only useful if something checks it, this impl doesn't)
| Principal principal = invocation.getPrincipal();
| Object credential = invocation.getCredential();
| SecurityAssociation.setPrincipal(principal);
| SecurityAssociation.setCredential(credential);
|
| // Dispatch the invocation
| try
| {
| return method.invoke(this, args);
| }
| catch(InvocationTargetException e)
| {
| Throwable t = e.getTargetException();
| if( t instanceof Exception )
| throw (Exception) t;
| else
| throw new UndeclaredThrowableException(t, method.toString());
| }
| finally
| {
| // Clear the security context
| SecurityAssociation.clear();
| }
| }
|
| // Implementation of remote methods
|
| public String hello()
| {
| log.info("hello() invoked");
| return "Hello";
| }
|
| public String getClusterNode()
| {
| log.info("getClusterNode() invoked");
| return getHAPartition().getNodeName();
| }
|
| }
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4195433#4195433
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4195433
More information about the jboss-user
mailing list