1
package org.jboss.test.remoting.callback.multiple;
3
import java.util.ArrayList;
4
import java.util.Iterator;
7
import javax.management.MBeanServer;
9
import org.jboss.jrunit.extensions.ServerTestCase;
10
import org.jboss.remoting.InvocationRequest;
11
import org.jboss.remoting.InvokerLocator;
12
import org.jboss.remoting.ServerInvocationHandler;
13
import org.jboss.remoting.ServerInvoker;
14
import org.jboss.remoting.callback.Callback;
15
import org.jboss.remoting.callback.InvokerCallbackHandler;
16
import org.jboss.remoting.transport.Connector;
19
* Simple remoting server. Uses inner class SampleInvocationHandler
20
* as the invocation target handler class.
22
* @author <a href="mailto:telrod@e2technologies.net">Tom Elrod</a>
24
public class CallbackTestServer extends ServerTestCase
26
// Default locator values
27
private static String transport = "socket";
28
private static String host = "localhost";
29
private static int port = 5530;
31
private String locatorURI = transport + "://" + host + ":" + port;
32
private Connector connector;
34
// String to be returned from invocation handler upon client invocation calls.
35
public static final String RESPONSE_VALUE = "This is the return to SampleInvocationHandler invocation";
36
public static final String CALLBACK_VALUE = "This is the payload of callback invocation.";
39
public void setupServer() throws Exception
41
InvokerLocator locator = new InvokerLocator(locatorURI);
42
System.out.println("Starting remoting server with locator uri of: " + locatorURI);
43
connector = new Connector();
44
connector.setInvokerLocator(locator.getLocatorURI());
47
CallbackTestServer.SampleInvocationHandler invocationHandler = new CallbackTestServer.SampleInvocationHandler();
48
// first parameter is sub-system name. can be any String value.
49
connector.addInvocationHandler("sample", invocationHandler);
52
protected void setUp() throws Exception
57
protected void tearDown() throws Exception
66
public static void main(String[] args)
68
CallbackTestServer server = new CallbackTestServer();
82
* Simple invocation handler implementation.
84
public static class SampleInvocationHandler implements ServerInvocationHandler
87
private List listeners = new ArrayList();
91
* called to handle a specific invocation
97
public Object invoke(InvocationRequest invocation) throws Throwable
99
// Just going to return static string as this is just simple example code.
101
// Will also fire callback to listeners if they were to exist using
102
// simple invocation request.
103
Callback callback = new Callback(CALLBACK_VALUE);
104
Iterator itr = listeners.iterator();
107
InvokerCallbackHandler callbackHandler = (InvokerCallbackHandler) itr.next();
108
callbackHandler.handleCallback(callback);
111
return RESPONSE_VALUE;
116
* Adds a callback handler that will listen for callbacks from
117
* the server invoker handler.
119
* @param callbackHandler
121
public void addListener(InvokerCallbackHandler callbackHandler)
123
listeners.add(callbackHandler);
127
* Removes the callback handler that was listening for callbacks
128
* from the server invoker handler.
130
* @param callbackHandler
132
public void removeListener(InvokerCallbackHandler callbackHandler)
134
listeners.remove(callbackHandler);
138
* set the mbean server that the handler can reference
142
public void setMBeanServer(MBeanServer server)
144
// NO OP as do not need reference to MBeanServer for this handler
148
* set the invoker that owns this handler
152
public void setInvoker(ServerInvoker invoker)
154
// NO OP as do not need reference back to the server invoker