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.
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.
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.
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.
23
package org.jboss.test.remoting.performance.synchronous;
25
import java.util.ArrayList;
26
import java.util.List;
28
import javax.management.MBeanServer;
29
import org.jboss.logging.Logger;
30
import org.jboss.remoting.InvocationRequest;
31
import org.jboss.remoting.ServerInvocationHandler;
32
import org.jboss.remoting.ServerInvoker;
33
import org.jboss.remoting.callback.InvokerCallbackHandler;
34
import org.jboss.remoting.callback.ServerInvokerCallbackHandler;
35
import org.jboss.remoting.invocation.RemoteInvocation;
37
import java.util.concurrent.ConcurrentHashMap;
38
import java.util.concurrent.FIFOReadWriteLock;
39
import java.util.concurrent.SyncList;
42
* @author <a href="mailto:tom.elrod@jboss.com">Tom Elrod</a>
44
public class PerformanceInvocationHandler implements ServerInvocationHandler
46
private ServerInvoker invoker;
48
private List listeners = new SyncList(new ArrayList(), new FIFOReadWriteLock());
49
private Map callTrackers = new ConcurrentHashMap();
51
private static final Logger log = Logger.getLogger(PerformanceInvocationHandler.class);
55
* set the mbean server that the handler can reference
59
public void setMBeanServer(MBeanServer server)
64
* set the invoker that owns this handler
68
public void setInvoker(ServerInvoker invoker)
70
this.invoker = invoker;
74
* called to handle a specific invocation. Please take care to make sure
75
* implementations are thread safe and can, and often will, receive concurrent
76
* calls on this method.
82
public Object invoke(InvocationRequest invocation) throws Throwable
84
Object param = invocation.getParameter();
85
String sessionId = invocation.getSessionId();
86
String methodName = "";
87
Object[] params = null;
90
if(param instanceof RemoteInvocation)
92
RemoteInvocation rminvo = (RemoteInvocation) param;
93
methodName = rminvo.getMethodName();
94
params = rminvo.getParameters();
98
throw new Exception("Unknown invocation payload (" + param + "). " +
99
"Should be instance of RemoteInvocation.");
103
if(methodName.equals(PerformanceClientTest.NUM_OF_CALLS))
105
Integer totalCountInteger = (Integer) params[0];
106
int totalCount = totalCountInteger.intValue();
107
System.out.println("received totalCallCount call with total count of " + totalCount + " from " + sessionId);
108
CallTracker tracker = (CallTracker) callTrackers.get(sessionId);
111
tracker.createTotalCount(totalCount);
112
ret = totalCountInteger;
116
log.error("Calling " + methodName + " but no call tracker exists for session id " + sessionId);
117
throw new Exception("Calling " + methodName + " but no call tracker exists for session id " + sessionId);
120
else if(methodName.equals(PerformanceClientTest.TEST_INVOCATION))
124
Payload payload = (Payload) params[0];
125
//System.out.println(payload);
126
int clientInvokeCallCount = payload.getCallNumber();
128
CallTracker tracker = (CallTracker) callTrackers.get(sessionId);
131
tracker.verifyClientInvokeCount(clientInvokeCallCount);
135
log.error("Calling " + methodName + " but no call tracker exists for session id " + sessionId);
136
throw new Exception("Calling " + methodName + " but no call tracker exists for session id " + sessionId);
138
// just passing return, even though not needed
139
ret = new Integer(clientInvokeCallCount);
143
log.error("no parameter passed for method call " + methodName);
149
throw new Exception("Don't know what to do with call to " + methodName);
156
private void createCallTracker(String sessionId, InvokerCallbackHandler callbackHandler)
158
CallTracker tracker = new CallTracker(sessionId, callbackHandler);
159
callTrackers.put(sessionId, tracker);
163
* Adds a callback handler that will listen for callbacks from
164
* the server invoker handler.
166
* @param callbackHandler
168
public void addListener(InvokerCallbackHandler callbackHandler)
170
ServerInvokerCallbackHandler handler = (ServerInvokerCallbackHandler) callbackHandler;
171
String sessionId = handler.getClientSessionId();
172
System.out.println("Adding callback listener. Callback handler has session id: " + sessionId);
173
createCallTracker(sessionId, callbackHandler);
174
listeners.add(callbackHandler);
178
* Removes the callback handler that was listening for callbacks
179
* from the server invoker handler.
181
* @param callbackHandler
183
public void removeListener(InvokerCallbackHandler callbackHandler)
185
//TODO: -TME Need to figure out how this should be handled.
186
// Could look up CallTracker based on session id (as in addListener() method)
187
// and then remove from tracker or kill tracker all together.
188
listeners.remove(callbackHandler);