KickJava   Java API By Example, From Geeks To Geeks.

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


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.Iterator JavaDoc;
10 import java.util.List JavaDoc;
11 import org.jboss.remoting.Client;
12 import org.jboss.remoting.InvokerLocator;
13 import org.jboss.remoting.callback.Callback;
14 import org.jboss.remoting.callback.HandleCallbackException;
15 import org.jboss.remoting.callback.InvokerCallbackHandler;
16 import org.jboss.remoting.transport.Connector;
17
18 /**
19  * Sample client showing how to register for callbacks from remoting server.
20  * The callback server we will make invocations to will generate random
21  * callback messages that we can receive.
22  *
23  * @author <a HREF="mailto:telrod@e2technologies.net">Tom Elrod</a>
24  */

25 public class CallbackClient
26 {
27    // Default locator values
28
private static String JavaDoc transport = "socket";
29    private static String JavaDoc host = "localhost";
30    private static int port = 5400;
31
32    private Client remotingClient;
33
34    // callback server, which receives push callbacks.
35
private Connector callbackServerConnector;
36
37    /**
38     * Create the remoting client to use to make calls on the remoting server.
39     *
40     * @param locatorURI
41     * @throws Exception
42     */

43    public void createRemotingClient(String JavaDoc locatorURI) throws Exception JavaDoc
44    {
45       InvokerLocator locator = new InvokerLocator(locatorURI);
46       System.out.println("Calling remoting server with locator uri of: " + locatorURI);
47
48       remotingClient = new Client(locator);
49    }
50
51    /**
52     * Makes a call on the remoting server.
53     *
54     * @throws Throwable
55     */

56    public void makeInvocation() throws Throwable JavaDoc
57    {
58       Object JavaDoc response = remotingClient.invoke("Do something", null);
59       System.out.println("Invocation response: " + response);
60    }
61
62    /**
63     * Shows how to register for pull callbacks and then get any callbacks
64     * that are waiting on the server.
65     *
66     * @throws Throwable
67     */

68    public void testPullCallback() throws Throwable JavaDoc
69    {
70       // our impplementation of the InvokerCallbackHandler interface, which
71
// is defined as an inner class below.
72
CallbackHandler callbackHandler = new CallbackHandler();
73       // by passing only the callback handler, will indicate pull callbacks
74
remotingClient.addListener(callbackHandler);
75
76       // the callback server generates callback messages on its own and need
77
// to give a few seconds to generate them.
78
Thread.currentThread().sleep(2000);
79
80       // call also make regular invocations on the server at any time.
81
makeInvocation();
82
83       // go get our callbacks residing on the server
84
List JavaDoc callbacks = remotingClient.getCallbacks(callbackHandler);
85       Iterator JavaDoc itr = callbacks.iterator();
86       while(itr.hasNext())
87       {
88          Callback callbackObject = (Callback) itr.next();
89          System.out.println("Pull Callback value = " + callbackObject.getCallbackObject());
90       }
91
92       // remove callback handler from server
93
remotingClient.removeListener(callbackHandler);
94    }
95
96    /**
97     * Shows how to register for push callbacks where the server will
98     * callback on our callback handler when it generates a callback.
99     *
100     * @throws Throwable
101     */

102    public void testPushCallback() throws Throwable JavaDoc
103    {
104       // First, need to create remoting server to receive callbacks.
105
// Using loctor with port value one higher than the target server
106
String JavaDoc callbackLocatorURI = transport + "://" + host + ":" + (port + 1);
107       InvokerLocator callbackLocator = new InvokerLocator(callbackLocatorURI);
108
109       // call to create remoting server to the
110
// callbacks generated by the target remoting server.
111
setupServer(callbackLocator);
112
113       CallbackHandler callbackHandler = new CallbackHandler();
114       // Callback handle object will be passed back as part of the callback object
115
// This can be used at the time the callback is received to identify context
116
String JavaDoc callbackHandleObject = "myCallbackHandleObject";
117       // by passing the callback handler and callback locator, will indicate push callbacks
118
// could also have not included callbackHandleObject as last parameter if did not care
119
// callback handle (i.e. addListener(callbackHandler, callbackLocator); )
120
remotingClient.addListener(callbackHandler, callbackLocator, callbackHandleObject);
121
122       // need to wait for brief moment so server can callback
123
Thread.sleep(2000);
124
125       // remove callback handler from server
126
remotingClient.removeListener(callbackHandler);
127
128       // shut down callback server
129
callbackServerConnector.stop();
130       callbackServerConnector.destroy();
131    }
132
133    /**
134     * Sets up the callback server that the target remoting server can call on
135     * with the callbacks that it generates.
136     *
137     * @param locator
138     * @throws Exception
139     */

140    public void setupServer(InvokerLocator locator) throws Exception JavaDoc
141    {
142       System.out.println("Starting remoting server with locator uri of: " + locator);
143       callbackServerConnector = new Connector();
144       callbackServerConnector.setInvokerLocator(locator.getLocatorURI());
145       callbackServerConnector.start();
146
147       // are using the same invocation handler as the one on the sample callback server
148
CallbackServer.SampleInvocationHandler invocationHandler = new CallbackServer.SampleInvocationHandler();
149       // first parameter is sub-system name. can be any String value.
150
callbackServerConnector.addInvocationHandler("sample", invocationHandler);
151    }
152
153
154    /**
155     * Can pass transport and port to be used as parameters.
156     * Valid transports are 'rmi' and 'socket'.
157     *
158     * @param args
159     */

160    public static void main(String JavaDoc[] args)
161    {
162       if(args != null && args.length == 2)
163       {
164          transport = args[0];
165          port = Integer.parseInt(args[1]);
166       }
167       String JavaDoc locatorURI = transport + "://" + host + ":" + port;
168       CallbackClient client = new CallbackClient();
169       try
170       {
171          client.createRemotingClient(locatorURI);
172
173          // make call to test pull callbacks
174
client.testPullCallback();
175
176          // make call to test push callbacks
177
client.testPushCallback();
178       }
179       catch(Throwable JavaDoc e)
180       {
181          e.printStackTrace();
182       }
183    }
184
185    /**
186     * Our implementation of the InvokerCallbackHandler. Simply
187     * prints out the callback object's message upon receiving it.
188     */

189    public class CallbackHandler implements InvokerCallbackHandler
190    {
191       /**
192        * Will take the callback and print out its values.
193        *
194        * @param callback
195        * @throws org.jboss.remoting.callback.HandleCallbackException
196        *
197        */

198       public void handleCallback(Callback callback) throws HandleCallbackException
199       {
200          System.out.println("Received push callback.");
201          System.out.println("Received callback value of: " + callback.getCallbackObject());
202          System.out.println("Received callback handle object of: " + callback.getCallbackHandleObject());
203          System.out.println("Received callback server invoker of: " + callback.getServerLocator());
204       }
205    }
206
207 }
Popular Tags