1
package org.jboss.test.remoting.callback.multiple.client;
3
import org.jboss.jrunit.extensions.ServerTestCase;
4
import org.jboss.remoting.transport.Connector;
5
import org.jboss.remoting.InvokerLocator;
6
import org.jboss.remoting.ServerInvocationHandler;
7
import org.jboss.remoting.InvocationRequest;
8
import org.jboss.remoting.ServerInvoker;
9
import org.jboss.remoting.callback.Callback;
10
import org.jboss.remoting.callback.InvokerCallbackHandler;
11
import org.apache.log4j.Level;
13
import javax.management.MBeanServer;
14
import java.util.List;
15
import java.util.ArrayList;
16
import java.util.Iterator;
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 = 5500;
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
54
org.apache.log4j.BasicConfigurator.configure();
55
org.apache.log4j.Category.getRoot().setLevel(Level.DEBUG);
56
org.apache.log4j.Category.getInstance("org.jboss.remoting").setLevel(Level.DEBUG);
57
org.apache.log4j.Category.getInstance("org.jgroups").setLevel(Level.FATAL);
62
protected void tearDown() throws Exception
71
public static void main(String[] args)
73
CallbackTestServer server = new CallbackTestServer();
87
* Simple invocation handler implementation.
89
public static class SampleInvocationHandler implements ServerInvocationHandler
92
private List listeners = new ArrayList();
96
* called to handle a specific invocation
102
public Object invoke(InvocationRequest invocation) throws Throwable
104
// Just going to return static string as this is just simple example code.
106
// Will also fire callback to listeners if they were to exist using
107
// simple invocation request.
108
Callback callback = new Callback(CALLBACK_VALUE);
109
Iterator itr = listeners.iterator();
112
InvokerCallbackHandler callbackHandler = (InvokerCallbackHandler) itr.next();
113
callbackHandler.handleCallback(callback);
116
return RESPONSE_VALUE;
121
* Adds a callback handler that will listen for callbacks from
122
* the server invoker handler.
124
* @param callbackHandler
126
public void addListener(InvokerCallbackHandler callbackHandler)
128
listeners.add(callbackHandler);
132
* Removes the callback handler that was listening for callbacks
133
* from the server invoker handler.
135
* @param callbackHandler
137
public void removeListener(InvokerCallbackHandler callbackHandler)
139
listeners.remove(callbackHandler);
143
* set the mbean server that the handler can reference
147
public void setMBeanServer(MBeanServer server)
149
// NO OP as do not need reference to MBeanServer for this handler
153
* set the invoker that owns this handler
157
public void setInvoker(ServerInvoker invoker)
159
// NO OP as do not need reference back to the server invoker