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.
25
* Demonstrates Callback acknowledgements.
27
* @author <a href="mailto:ron.sigal@jboss.com">Ron Sigal</a>
32
package org.jboss.remoting.samples.callback.acknowledgement;
34
import java.net.InetAddress;
35
import java.net.UnknownHostException;
36
import java.util.ArrayList;
37
import java.util.HashMap;
38
import java.util.Iterator;
39
import java.util.List;
41
import org.jboss.remoting.Client;
42
import org.jboss.remoting.InvokerLocator;
43
import org.jboss.remoting.callback.Callback;
44
import org.jboss.remoting.callback.CallbackPoller;
45
import org.jboss.remoting.callback.HandleCallbackException;
46
import org.jboss.remoting.callback.InvokerCallbackHandler;
49
public class CallbackAcknowledgeClient
51
private static String transport = "socket";
52
private static String host;
53
private static int port = 5401;
55
private Client client;
59
* Can pass transport and port to be used as parameters.
60
* Valid transports are 'rmi' and 'socket'.
64
public static void main(String[] args)
66
if(args != null && args.length == 2)
69
port = Integer.parseInt(args[1]);
73
host = InetAddress.getLocalHost().getHostName();
75
catch (UnknownHostException e1)
77
System.err.println("cannot get local host name");
80
String locatorURI = transport + "://" + host + ":" + port;
81
CallbackAcknowledgeClient acknowledgeClient = new CallbackAcknowledgeClient();
84
acknowledgeClient.createRemotingClient(locatorURI);
86
acknowledgeClient.testPullCallbackAcknowledgements();
87
acknowledgeClient.testPolledCallbackApplicationAcknowledgements();
88
acknowledgeClient.testPolledCallbackRemotingAcknowledgements();
89
acknowledgeClient.testPushCallbackApplicationAcknowledgements();
90
acknowledgeClient.testPushCallbackRemotingAcknowledgements();
92
acknowledgeClient.disconnectRemotingClient();
98
System.out.println("done.");
102
public void createRemotingClient(String locatorURI) throws Exception
104
client = new Client(new InvokerLocator(locatorURI));
109
public void disconnectRemotingClient()
117
* In this test, the connection is configured for pull callbacks, and
118
* acknowledgements are made by an explicit call to Client.acknowledgeCallback()
119
* after the callbacks have been received.
121
public void testPullCallbackAcknowledgements()
125
// Register callback handler.
126
InvokerCallbackHandler callbackHandler = new NonAcknowledgingCallbackHandler();
127
client.addListener(callbackHandler);
129
// Request callbacks from server.
130
client.invoke(CallbackAcknowledgeServer.APPLICATION_ACKNOWLDEGEMENTS);
133
List callbacks = client.getCallbacks(callbackHandler);
136
ArrayList responses = new ArrayList(callbacks.size());
137
Iterator it = callbacks.iterator();
140
Callback callback = (Callback) it.next();
141
System.out.println("received pull callback: " + callback.getParameter());
142
responses.add(callback.getParameter() + ": acknowledged");
145
// Acknowledge callbacks.
146
client.acknowledgeCallbacks(callbackHandler, callbacks, responses);
148
// Unregister callback handler.
149
client.removeListener(callbackHandler);
153
System.out.println("failure: " + e.getMessage());
159
* In this test the connection is configured for push callbacks implemented in
160
* Remoting by polling the server for callbacks and pushing them (on the client
161
* side) to the InvokerCallbackHandler. Acknowledgements are made from
162
* TestCallbackHandler.handleCallback() by an explicit call to
163
* Client.acknowledgeCallback() after the callbacks have been received.
165
public void testPolledCallbackApplicationAcknowledgements()
169
// Register callback handler.
170
InvokerCallbackHandler callbackHandler = new AcknowledgingCallbackHandler(client);
171
HashMap metadata = new HashMap();
172
metadata.put(CallbackPoller.CALLBACK_POLL_PERIOD, "1000");
173
client.addListener(callbackHandler, metadata);
175
// Request callbacks from server.
176
client.invoke(CallbackAcknowledgeServer.APPLICATION_ACKNOWLDEGEMENTS);
179
// Unregister callback handler.
180
client.removeListener(callbackHandler);
184
System.out.println("failure: " + e.getMessage());
190
* In this test the connection is configured for push callbacks implemented in
191
* Remoting by polling the server for callbacks and pushing them (on the client
192
* side) to the InvokerCallbackHandler. Acknowledgements are handled implicitly
195
public void testPolledCallbackRemotingAcknowledgements()
199
// Register callback handler.
200
InvokerCallbackHandler callbackHandler = new NonAcknowledgingCallbackHandler();
201
HashMap metadata = new HashMap();
202
metadata.put(CallbackPoller.CALLBACK_POLL_PERIOD, "1000");
203
client.addListener(callbackHandler, metadata);
205
// Request callbacks from server.
206
client.invoke(CallbackAcknowledgeServer.REMOTING_ACKNOWLDEGEMENTS);
209
// Unregister callback handler.
210
client.removeListener(callbackHandler);
214
System.out.println("failure: " + e.getMessage());
220
* In this test the connection is configured for true push callbacks.
221
* Acknowledgements are made from TestCallbackHandler.handleCallback()
222
* by an explicit call to Client.acknowledgeCallback() after the callbacks
223
* have been received.
225
public void testPushCallbackApplicationAcknowledgements()
229
// Register callback handler.
230
InvokerCallbackHandler callbackHandler = new AcknowledgingCallbackHandler(client);
231
client.addListener(callbackHandler, null, null, true);
233
// Request callbacks from servrr.
234
client.invoke(CallbackAcknowledgeServer.APPLICATION_ACKNOWLDEGEMENTS);
236
// Unregister callback handler.
237
client.removeListener(callbackHandler);
241
System.out.println("failure: " + e.getMessage());
247
* In this test the connection is configured for true push callbacks, and
248
* Acknowledgements are handled implicitly by Remoting.
250
public void testPushCallbackRemotingAcknowledgements()
254
// Register callback handler.
255
InvokerCallbackHandler callbackHandler = new NonAcknowledgingCallbackHandler();
256
client.addListener(callbackHandler, null, null, true);
258
// Request callbacks from server.
259
client.invoke(CallbackAcknowledgeServer.REMOTING_ACKNOWLDEGEMENTS);
261
// Unregister callback handler.
262
client.removeListener(callbackHandler);
266
System.out.println("failure: " + e.getMessage());
271
static class NonAcknowledgingCallbackHandler implements InvokerCallbackHandler
273
public void handleCallback(Callback callback) throws HandleCallbackException
275
System.out.println("received push callback: " + callback.getParameter());
280
static class AcknowledgingCallbackHandler implements InvokerCallbackHandler
282
private Client client;
284
public AcknowledgingCallbackHandler(Client client)
286
this.client = client;
289
public void handleCallback(Callback callback) throws HandleCallbackException
291
System.out.println("received push callback: " + callback.getParameter());
292
Object response = callback.getParameter() + ": acknowledged";
295
client.acknowledgeCallback(this, callback, response);
299
System.out.println("Unable to acknowledge callback: " + callback.getParameter());