KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > invocation > pooled > server > PooledInvokerHA


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.invocation.pooled.server;
23
24 import org.jboss.system.Registry;
25 import java.rmi.MarshalledObject JavaDoc;
26 import javax.management.ObjectName JavaDoc;
27 import org.jboss.invocation.Invocation;
28 import org.jboss.invocation.MarshalledInvocation;
29 import org.jboss.invocation.pooled.interfaces.PooledInvokerProxy;
30 import org.jboss.invocation.pooled.interfaces.ServerAddress;
31 import java.util.HashMap JavaDoc;
32 import org.jboss.invocation.Invoker;
33 import org.jboss.invocation.InvokerHA;
34 import org.jboss.invocation.jrmp.interfaces.JRMPInvokerProxyHA;
35 import org.jboss.ha.framework.interfaces.HARMIResponse;
36 import org.jboss.ha.framework.server.HATarget;
37 import org.jboss.ha.framework.interfaces.LoadBalancePolicy;
38 import org.jboss.ha.framework.interfaces.GenericClusteringException;
39 import javax.management.InstanceNotFoundException JavaDoc;
40 import javax.management.ReflectionException JavaDoc;
41
42 import java.util.ArrayList JavaDoc;
43
44 /**
45  * This invoker pools Threads and client connections to one server socket.
46  * The purpose is to avoid a bunch of failings of RMI.
47  *
48  * 1. Avoid making a client socket connection with every invocation call.
49  * This is very expensive. Also on windows if too many clients try
50  * to connect at the same time, you get connection refused exceptions.
51  * This invoker/proxy combo alleviates this.
52  *
53  * 2. Avoid creating a thread per invocation. The client/server connection
54  * is preserved and attached to the same thread.
55
56  * So we have connection pooling on the server and client side, and thread pooling
57  * on the server side. Pool, is an LRU pool, so resources should be cleaned up.
58  *
59  *
60  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
61  * @version $Revision: 46260 $
62  *
63  * @jmx:mbean extends="org.jboss.system.ServiceMBean"
64  */

65 public final class PooledInvokerHA extends PooledInvoker implements InvokerHA
66 {
67    protected HashMap JavaDoc beanMap = new HashMap JavaDoc();
68
69    protected void jmxBind()
70    {
71       Registry.bind(getServiceName(), this);
72    }
73
74    // JRMPInvoker.destroyService() does the right thing
75

76    public java.io.Serializable JavaDoc getStub()
77    {
78       ServerAddress sa = new ServerAddress(clientConnectAddress,
79          clientConnectPort, enableTcpNoDelay, timeout, clientSocketFactory);
80       return new PooledInvokerProxy(sa, clientMaxPoolSize);
81    }
82
83    public void registerBean(ObjectName JavaDoc beanName, HATarget target) throws Exception JavaDoc
84    {
85       Integer JavaDoc hash = new Integer JavaDoc(beanName.hashCode());
86       log.debug("registerBean: "+beanName);
87       
88       if (beanMap.containsKey(hash))
89       {
90          // FIXME [oleg] In theory this is possible!
91
throw new IllegalStateException JavaDoc("Trying to register bean with the existing hashCode");
92       }
93       beanMap.put(hash, target);
94    }
95    
96    public Invoker createProxy(ObjectName JavaDoc beanName, LoadBalancePolicy policy,
97       String JavaDoc proxyFamilyName) throws Exception JavaDoc
98    {
99       Integer JavaDoc hash = new Integer JavaDoc(beanName.hashCode());
100       HATarget target = (HATarget) beanMap.get(hash);
101       if (target == null)
102       {
103          throw new IllegalStateException JavaDoc("The bean hashCode not found");
104       }
105
106       String JavaDoc familyName = proxyFamilyName;
107       if (familyName == null)
108          familyName= target.getAssociatedPartition().getPartitionName() + "/" + beanName;
109
110       JRMPInvokerProxyHA proxy = new JRMPInvokerProxyHA(target.getReplicants(),
111                                                         policy,
112                                                         familyName,
113                                                         target.getCurrentViewId ());
114       return proxy;
115    }
116
117    public void unregisterBean(ObjectName JavaDoc beanName) throws Exception JavaDoc
118    {
119       Integer JavaDoc hash = new Integer JavaDoc(beanName.hashCode());
120       beanMap.remove(hash);
121    }
122
123    /**
124     * Invoke a Remote interface method.
125     */

126    public Object JavaDoc invoke(Invocation invocation)
127       throws Exception JavaDoc
128    {
129       ClassLoader JavaDoc oldCl = Thread.currentThread().getContextClassLoader();
130       try
131       {
132          // Deserialize the transaction if it is there
133
invocation.setTransaction(importTPC(((MarshalledInvocation) invocation).getTransactionPropagationContext()));
134
135          // Extract the ObjectName, the rest is still marshalled
136
ObjectName JavaDoc mbean = (ObjectName JavaDoc) Registry.lookup(invocation.getObjectName());
137          long clientViewId = ((Long JavaDoc)invocation.getValue("CLUSTER_VIEW_ID")).longValue();
138
139          HATarget target = (HATarget)beanMap.get(invocation.getObjectName());
140          if (target == null)
141          {
142             // We could throw IllegalStateException but we have a race condition that could occur:
143
// when we undeploy a bean, the cluster takes some time to converge
144
// and to recalculate a new viewId and list of replicant for each HATarget.
145
// Consequently, a client could own an up-to-date list of the replicants
146
// (before the cluster has converged) and try to perform an invocation
147
// on this node where the HATarget no more exist, thus receiving a
148
// wrong exception and no failover is performed with an IllegalStateException
149
//
150
throw new GenericClusteringException(GenericClusteringException.COMPLETED_NO,
151                                                  "target is not/no more registered on this node");
152          }
153          
154          if (!target.invocationsAllowed ())
155             throw new GenericClusteringException(GenericClusteringException.COMPLETED_NO,
156                                         "invocations are currently not allowed on this target");
157
158          // The cl on the thread should be set in another interceptor
159
Object JavaDoc rtn = getServer().invoke(mbean,
160                                          "invoke",
161                                          new Object JavaDoc[] { invocation },
162                                          Invocation.INVOKE_SIGNATURE);
163          
164          HARMIResponse rsp = new HARMIResponse();
165
166          if (clientViewId != target.getCurrentViewId())
167          {
168             rsp.newReplicants = new ArrayList JavaDoc(target.getReplicants());
169             rsp.currentViewId = target.getCurrentViewId();
170          }
171          rsp.response = rtn;
172          return rsp;
173       }
174       catch (InstanceNotFoundException JavaDoc e)
175       {
176          throw new GenericClusteringException(GenericClusteringException.COMPLETED_NO, e);
177       }
178       catch (ReflectionException JavaDoc e)
179       {
180          throw new GenericClusteringException(GenericClusteringException.COMPLETED_NO, e);
181       }
182       catch (Exception JavaDoc e)
183       {
184          org.jboss.mx.util.JMXExceptionDecoder.rethrow(e);
185
186          // the compiler does not know an exception is thrown by the above
187
throw new org.jboss.util.UnreachableStatementException();
188       }
189       finally
190       {
191          Thread.currentThread().setContextClassLoader(oldCl);
192       }
193    }
194 }
195 // vim:expandtab:tabstop=3:shiftwidth=3
196
Popular Tags