KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > remoting > samples > callback > CallbackServer


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.remoting.samples.callback;
8
9 import java.util.ArrayList JavaDoc;
10 import java.util.Iterator JavaDoc;
11 import java.util.List JavaDoc;
12 import javax.management.MBeanServer JavaDoc;
13 import org.jboss.remoting.InvocationRequest;
14 import org.jboss.remoting.InvokerLocator;
15 import org.jboss.remoting.ServerInvocationHandler;
16 import org.jboss.remoting.ServerInvoker;
17 import org.jboss.remoting.callback.Callback;
18 import org.jboss.remoting.callback.HandleCallbackException;
19 import org.jboss.remoting.callback.InvokerCallbackHandler;
20 import org.jboss.remoting.transport.Connector;
21
22 /**
23  * Simple remoting server. Uses inner class SampleInvocationHandler
24  * as the invocation target handler class, which will generate
25  * callback messages upon callback listeners being added.
26  *
27  * @author <a HREF="mailto:telrod@e2technologies.net">Tom Elrod</a>
28  */

29 public class CallbackServer
30 {
31    // Default locator values
32
private static String JavaDoc transport = "socket";
33    private static String JavaDoc host = "localhost";
34    private static int port = 5400;
35
36    // counter indicating the number of callbacks generated
37
private static int callbackCounter = 1;
38
39    // remoting server connector
40
private Connector connector = null;
41
42    // String to be returned from invocation handler upon client invocation calls.
43
private static final String JavaDoc RESPONSE_VALUE = "This is the return to SampleInvocationHandler invocation";
44
45    /**
46     * Sets up target remoting server.
47     *
48     * @param locatorURI
49     * @throws Exception
50     */

51    public void setupServer(String JavaDoc locatorURI) throws Exception JavaDoc
52    {
53       InvokerLocator locator = new InvokerLocator(locatorURI);
54       System.out.println("Starting remoting server with locator uri of: " + locatorURI);
55       connector = new Connector();
56       connector.setInvokerLocator(locator.getLocatorURI());
57       connector.create();
58
59       SampleInvocationHandler invocationHandler = new SampleInvocationHandler();
60       // first parameter is sub-system name. can be any String value.
61
connector.addInvocationHandler("sample", invocationHandler);
62
63       connector.start();
64    }
65
66    /**
67     * Shuts down the server
68     */

69    public void shutdownServer()
70    {
71       connector.stop();
72       connector.destroy();
73    }
74
75    /**
76     * Can pass transport and port to be used as parameters.
77     * Valid transports are 'rmi' and 'socket'.
78     *
79     * @param args
80     */

81    public static void main(String JavaDoc[] args)
82    {
83       if(args != null && args.length == 2)
84       {
85          transport = args[0];
86          port = Integer.parseInt(args[1]);
87       }
88       String JavaDoc locatorURI = transport + "://" + host + ":" + port;
89       CallbackServer server = new CallbackServer();
90       try
91       {
92          server.setupServer(locatorURI);
93          
94          // wait forever, let the user kill us at any point (at which point, the client will detect we went down)
95
while(true)
96          {
97             Thread.sleep(1000);
98          }
99
100       }
101       catch(Exception JavaDoc e)
102       {
103          e.printStackTrace();
104       }
105    }
106
107    /**
108     * Simple invocation handler implementation. When callback client's are registered, will
109     * generate callbacks periodically.
110     */

111    public static class SampleInvocationHandler implements ServerInvocationHandler, Runnable JavaDoc
112    {
113       // list of callback listeners registered
114
private List JavaDoc listeners = new ArrayList JavaDoc();
115
116       // flag to indicate when should generate callback messages
117
private boolean shouldGenerateCallbacks = false;
118
119       public SampleInvocationHandler()
120       {
121          // will start a new thread for generating callbacks.
122
Thread JavaDoc callbackThread = new Thread JavaDoc(this);
123          callbackThread.setDaemon(true);
124          callbackThread.start();
125       }
126
127       /**
128        * called by the remoting server to handle the invocation from client.
129        *
130        * @param invocation
131        * @return
132        * @throws Throwable
133        */

134       public Object JavaDoc invoke(InvocationRequest invocation) throws Throwable JavaDoc
135       {
136          // Print out the invocation request
137
System.out.println("Invocation request is: " + invocation.getParameter());
138
139          // Just going to return static string as this is just simple example code.
140
return RESPONSE_VALUE;
141       }
142
143       /**
144        * Adds a callback handler that will listen for callbacks from
145        * the server invoker handler.
146        *
147        * @param callbackHandler
148        */

149       public void addListener(InvokerCallbackHandler callbackHandler)
150       {
151          System.out.println("Adding callback listener.");
152          listeners.add(callbackHandler);
153          shouldGenerateCallbacks = true;
154       }
155
156       /**
157        * Removes the callback handler that was listening for callbacks
158        * from the server invoker handler.
159        *
160        * @param callbackHandler
161        */

162       public void removeListener(InvokerCallbackHandler callbackHandler)
163       {
164          System.out.println("Removing callback listener.");
165          listeners.remove(callbackHandler);
166          if(listeners.size() == 0)
167          {
168             shouldGenerateCallbacks = false;
169          }
170       }
171
172       /**
173        * Will generate callback messages every second while shouldGenerateCallbacks
174        * flag is true.
175        */

176       public void run()
177       {
178          // keep looping while waiting to fire callbacks.
179
while(true)
180          {
181             while(shouldGenerateCallbacks)
182             {
183                // create new callback message
184
Callback callback = new Callback("Callback " + callbackCounter++ + ": This is the payload of callback invocation.");
185                Iterator JavaDoc itr = listeners.iterator();
186                while(itr.hasNext())
187                {
188                   InvokerCallbackHandler callbackHandler = (InvokerCallbackHandler) itr.next();
189                   try
190                   {
191                      callbackHandler.handleCallback(callback);
192                   }
193                   catch(HandleCallbackException e)
194                   {
195                      e.printStackTrace();
196                   }
197                }
198                // sleep for a second before firing next callback message
199
try
200                {
201                   Thread.currentThread().sleep(1000);
202                }
203                catch(InterruptedException JavaDoc e)
204                {
205                }
206
207             }
208             // sleep for a second before while waiting for flag to be set
209
try
210             {
211                Thread.currentThread().sleep(1000);
212             }
213             catch(InterruptedException JavaDoc e)
214             {
215             }
216          }
217       }
218
219       /**
220        * set the mbean server that the handler can reference
221        *
222        * @param server
223        */

224       public void setMBeanServer(MBeanServer JavaDoc server)
225       {
226          // NO OP as do not need reference to MBeanServer for this handler
227
}
228
229       /**
230        * set the invoker that owns this handler
231        *
232        * @param invoker
233        */

234       public void setInvoker(ServerInvoker invoker)
235       {
236          // NO OP as do not need reference back to the server invoker
237
}
238
239    }
240 }
Popular Tags