~ubuntu-branches/ubuntu/raring/libjboss-remoting-java/raring

« back to all changes in this revision

Viewing changes to examples/org/jboss/remoting/samples/callback/acknowledgement/CallbackAcknowledgeClient.java

  • Committer: Package Import Robot
  • Author(s): Torsten Werner
  • Date: 2011-09-09 14:01:03 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: package-import@ubuntu.com-20110909140103-hqokx61534tas9rg
Tags: 2.5.3.SP1-1
* Newer but not newest upstream release. Do not build samples.
* Change debian/watch to upstream's svn repo.
* Add patch to fix compile error caused by tomcat update.
  (Closes: #628303)
* Switch to source format 3.0.
* Switch to debhelper level 7.
* Remove useless Depends.
* Update Standards-Version: 3.9.2.
* Update README.source.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
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.
6
 
*
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.
11
 
*
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.
16
 
*
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.
21
 
*/
22
 
 
23
 
 
24
 
/**
25
 
 * Demonstrates Callback acknowledgements.
26
 
 * 
27
 
 * @author <a href="mailto:ron.sigal@jboss.com">Ron Sigal</a>
28
 
 * <p/>
29
 
 * Copyright (c) 2006
30
 
 * </p>
31
 
 */
32
 
package org.jboss.remoting.samples.callback.acknowledgement;
33
 
 
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;
40
 
 
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;
47
 
 
48
 
 
49
 
public class CallbackAcknowledgeClient
50
 
{  
51
 
   private static String transport = "socket";
52
 
   private static String host;
53
 
   private static int port = 5401;
54
 
   
55
 
   private Client client;
56
 
 
57
 
   
58
 
   /**
59
 
    * Can pass transport and port to be used as parameters.
60
 
    * Valid transports are 'rmi' and 'socket'.
61
 
    *
62
 
    * @param args
63
 
    */
64
 
   public static void main(String[] args)
65
 
   {
66
 
      if(args != null && args.length == 2)
67
 
      {
68
 
         transport = args[0];
69
 
         port = Integer.parseInt(args[1]);
70
 
      }
71
 
      try
72
 
      {
73
 
         host = InetAddress.getLocalHost().getHostName();
74
 
      }
75
 
      catch (UnknownHostException e1)
76
 
      {
77
 
         System.err.println("cannot get local host name");
78
 
         return;
79
 
      }
80
 
      String locatorURI = transport + "://" + host + ":" + port;
81
 
      CallbackAcknowledgeClient acknowledgeClient = new CallbackAcknowledgeClient();
82
 
      try
83
 
      {
84
 
         acknowledgeClient.createRemotingClient(locatorURI);
85
 
 
86
 
         acknowledgeClient.testPullCallbackAcknowledgements();
87
 
         acknowledgeClient.testPolledCallbackApplicationAcknowledgements();
88
 
         acknowledgeClient.testPolledCallbackRemotingAcknowledgements();
89
 
         acknowledgeClient.testPushCallbackApplicationAcknowledgements();
90
 
         acknowledgeClient.testPushCallbackRemotingAcknowledgements();
91
 
         
92
 
         acknowledgeClient.disconnectRemotingClient();
93
 
      }
94
 
      catch(Throwable e)
95
 
      {
96
 
         e.printStackTrace();
97
 
      }
98
 
      System.out.println("done.");
99
 
   }
100
 
   
101
 
   
102
 
   public void createRemotingClient(String locatorURI) throws Exception
103
 
   {
104
 
      client = new Client(new InvokerLocator(locatorURI));
105
 
      client.connect();
106
 
   }
107
 
   
108
 
   
109
 
   public void disconnectRemotingClient()
110
 
   {
111
 
      if (client != null)
112
 
         client.disconnect();
113
 
   }
114
 
   
115
 
   
116
 
   /**
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.
120
 
    */
121
 
   public void testPullCallbackAcknowledgements()
122
 
   {
123
 
      try
124
 
      {
125
 
         // Register callback handler.
126
 
         InvokerCallbackHandler callbackHandler = new NonAcknowledgingCallbackHandler();
127
 
         client.addListener(callbackHandler);
128
 
         
129
 
         // Request callbacks from server.
130
 
         client.invoke(CallbackAcknowledgeServer.APPLICATION_ACKNOWLDEGEMENTS);
131
 
         
132
 
         // Get callbacks.
133
 
         List callbacks = client.getCallbacks(callbackHandler);
134
 
         
135
 
         // Create responses.
136
 
         ArrayList responses = new ArrayList(callbacks.size());
137
 
         Iterator it = callbacks.iterator();
138
 
         while (it.hasNext())
139
 
         {
140
 
            Callback callback = (Callback) it.next();
141
 
            System.out.println("received pull callback: " + callback.getParameter());
142
 
            responses.add(callback.getParameter() + ": acknowledged");
143
 
         }
144
 
         
145
 
         // Acknowledge callbacks.
146
 
         client.acknowledgeCallbacks(callbackHandler, callbacks, responses);
147
 
         
148
 
         // Unregister callback handler.
149
 
         client.removeListener(callbackHandler);
150
 
      }
151
 
      catch (Throwable e)
152
 
      {
153
 
         System.out.println("failure: " + e.getMessage());
154
 
      }
155
 
   }
156
 
   
157
 
   
158
 
   /**
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.
164
 
    */
165
 
   public void testPolledCallbackApplicationAcknowledgements()
166
 
   {
167
 
      try
168
 
      {
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);
174
 
         
175
 
         // Request callbacks from server.
176
 
         client.invoke(CallbackAcknowledgeServer.APPLICATION_ACKNOWLDEGEMENTS);
177
 
         Thread.sleep(2000);
178
 
         
179
 
         // Unregister callback handler.
180
 
         client.removeListener(callbackHandler);
181
 
      }
182
 
      catch (Throwable e)
183
 
      {
184
 
         System.out.println("failure: " + e.getMessage());
185
 
      }
186
 
   }
187
 
   
188
 
   
189
 
   /**
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
193
 
    * by Remoting.
194
 
    */
195
 
   public void testPolledCallbackRemotingAcknowledgements()
196
 
   {
197
 
      try
198
 
      {
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);
204
 
         
205
 
         // Request callbacks from server.
206
 
         client.invoke(CallbackAcknowledgeServer.REMOTING_ACKNOWLDEGEMENTS);
207
 
         Thread.sleep(2000);
208
 
         
209
 
         // Unregister callback handler.
210
 
         client.removeListener(callbackHandler);
211
 
      }
212
 
      catch (Throwable e)
213
 
      {
214
 
         System.out.println("failure: " + e.getMessage());
215
 
      }
216
 
   }
217
 
   
218
 
   
219
 
   /**
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.
224
 
    */
225
 
   public void testPushCallbackApplicationAcknowledgements()
226
 
   {
227
 
      try
228
 
      {
229
 
         // Register callback handler.
230
 
         InvokerCallbackHandler callbackHandler = new AcknowledgingCallbackHandler(client);
231
 
         client.addListener(callbackHandler, null, null, true);
232
 
         
233
 
         // Request callbacks from servrr.
234
 
         client.invoke(CallbackAcknowledgeServer.APPLICATION_ACKNOWLDEGEMENTS);
235
 
         
236
 
         // Unregister callback handler.
237
 
         client.removeListener(callbackHandler);
238
 
      }
239
 
      catch (Throwable e)
240
 
      {
241
 
         System.out.println("failure: " + e.getMessage());
242
 
      }
243
 
   }
244
 
   
245
 
   
246
 
   /**
247
 
    * In this test the connection is configured for true push callbacks, and
248
 
    * Acknowledgements are handled implicitly by Remoting.
249
 
    */
250
 
   public void testPushCallbackRemotingAcknowledgements()
251
 
   {
252
 
      try
253
 
      {
254
 
         // Register callback handler.
255
 
         InvokerCallbackHandler callbackHandler = new NonAcknowledgingCallbackHandler();
256
 
         client.addListener(callbackHandler, null, null, true);
257
 
         
258
 
         // Request callbacks from server.
259
 
         client.invoke(CallbackAcknowledgeServer.REMOTING_ACKNOWLDEGEMENTS);
260
 
         
261
 
         // Unregister callback handler.
262
 
         client.removeListener(callbackHandler);
263
 
      }
264
 
      catch (Throwable e)
265
 
      {
266
 
         System.out.println("failure: " + e.getMessage());
267
 
      }
268
 
   }
269
 
  
270
 
   
271
 
   static class NonAcknowledgingCallbackHandler implements InvokerCallbackHandler
272
 
   {  
273
 
      public void handleCallback(Callback callback) throws HandleCallbackException
274
 
      {
275
 
         System.out.println("received push callback: " + callback.getParameter());
276
 
      }
277
 
   }
278
 
   
279
 
   
280
 
   static class AcknowledgingCallbackHandler implements InvokerCallbackHandler
281
 
   {  
282
 
      private Client client;
283
 
      
284
 
      public AcknowledgingCallbackHandler(Client client)
285
 
      {
286
 
         this.client = client;
287
 
      }
288
 
      
289
 
      public void handleCallback(Callback callback) throws HandleCallbackException
290
 
      {
291
 
         System.out.println("received push callback: " + callback.getParameter());
292
 
         Object response = callback.getParameter() + ": acknowledged";
293
 
         try
294
 
         {
295
 
            client.acknowledgeCallback(this, callback, response);
296
 
         }
297
 
         catch (Throwable e)
298
 
         {
299
 
            System.out.println("Unable to acknowledge callback: " + callback.getParameter());
300
 
         }
301
 
      }
302
 
   }
303
 
}