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

« back to all changes in this revision

Viewing changes to tests/org/jboss/test/remoting/callback/push/disconnect/CallbackTestClient.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
 
package org.jboss.test.remoting.callback.push.disconnect;
24
 
 
25
 
import java.util.ArrayList;
26
 
import java.util.Iterator;
27
 
import java.util.List;
28
 
import javax.management.MBeanServer;
29
 
import org.jboss.remoting.Client;
30
 
import org.jboss.remoting.InvocationRequest;
31
 
import org.jboss.remoting.InvokerLocator;
32
 
import org.jboss.remoting.ServerInvocationHandler;
33
 
import org.jboss.remoting.ServerInvoker;
34
 
import org.jboss.remoting.callback.Callback;
35
 
import org.jboss.remoting.callback.HandleCallbackException;
36
 
import org.jboss.remoting.callback.InvokerCallbackHandler;
37
 
import org.jboss.remoting.transport.Connector;
38
 
 
39
 
import junit.framework.TestCase;
40
 
 
41
 
/**
42
 
 * @author <a href="mailto:telrod@e2technologies.net">Tom Elrod</a>
43
 
 */
44
 
public class CallbackTestClient extends TestCase
45
 
{
46
 
   // Default locator values
47
 
   private static String transport = "socket";
48
 
   private static String host = "localhost";
49
 
   private static int port = 5500;
50
 
   private static String callbackHost = "localhost";
51
 
   private static int callbackPort = 5501;
52
 
 
53
 
   private String locatorURI = transport + "://" + host + ":" + port;
54
 
 
55
 
//   public static final String RESPONSE_VALUE = "This is the return to SampleInvocationHandler invocation";
56
 
 
57
 
   public void testPullCallbackDisconnect() throws Throwable
58
 
   {
59
 
      String callbackServerURI = "socket://" + callbackHost + ":" + callbackPort;
60
 
      Connector callbackServer = new Connector();
61
 
      callbackServer.setInvokerLocator(callbackServerURI);
62
 
      callbackServer.start();
63
 
//      SampleInvocationHandler invocationHandler = new SampleInvocationHandler();
64
 
 
65
 
//      callbackServer.getServerInvoker().addInvocationHandler("callbackTest", invocationHandler);
66
 
 
67
 
      Client client = new Client(new InvokerLocator(locatorURI), "test");
68
 
      client.connect();
69
 
      CallbackHandler callbackHandler = new CallbackHandler();
70
 
      client.addListener(callbackHandler, new InvokerLocator(callbackServerURI));
71
 
 
72
 
      // send invocation that will trigger sending of callbacks.
73
 
      client.invoke("foo");
74
 
 
75
 
      // wait to get some callbacks.
76
 
      System.out.println("Waiting three seconds for callbacks.");
77
 
      Thread.currentThread().sleep(3000);
78
 
 
79
 
      System.out.println("Done waiting for callbacks, will shutdown callback server and client.");
80
 
      // shutdown client and callback server
81
 
//      callbackServer.getServerInvoker().removeInvocationHandler(
82
 
//            callbackServer.getServerInvoker().getInvocationHandlers()[0].toString()
83
 
//      );
84
 
      client.removeListener(callbackHandler);
85
 
      client.disconnect();
86
 
      callbackServer.stop();
87
 
      callbackServer.destroy();
88
 
 
89
 
      System.out.println("Callback total number after shutdown is " + callbackHandler.getCallbackNumber());
90
 
      int callbackNumber = callbackHandler.getCallbackNumber();
91
 
 
92
 
      // sleep to allow server to send more callbacks if is going to
93
 
      System.out.println("Waiting ten seconds to make sure no more callbacks are delivered by server.");
94
 
      Thread.currentThread().sleep(10000);
95
 
 
96
 
      // check to make sure no more callbacks have been delivered.
97
 
      System.out.println("Callback total number after waiting ten seconds is " + callbackHandler.getCallbackNumber());
98
 
      assertEquals(callbackNumber, callbackHandler.getCallbackNumber());
99
 
 
100
 
   }
101
 
 
102
 
   /**
103
 
    * Simple invocation handler implementation.
104
 
    */
105
 
//   public static class SampleInvocationHandler implements ServerInvocationHandler
106
 
//   {
107
 
//
108
 
//      private List listeners = new ArrayList();
109
 
//      private boolean sendCallbacks = false;
110
 
//      private int callbackCounter = 1;
111
 
//
112
 
//      public SampleInvocationHandler()
113
 
//      {
114
 
//         sendCallbacks();
115
 
//      }
116
 
//
117
 
//      /**
118
 
//       * called to handle a specific invocation
119
 
//       *
120
 
//       * @param invocation
121
 
//       * @return
122
 
//       * @throws Throwable
123
 
//       */
124
 
//      public Object invoke(InvocationRequest invocation) throws Throwable
125
 
//      {
126
 
//         // Just going to return static string as this is just simple example code.
127
 
//
128
 
//         sendCallbacks = true;
129
 
//         return RESPONSE_VALUE;
130
 
//
131
 
//      }
132
 
//
133
 
//      private void sendCallbacks()
134
 
//      {
135
 
//         new Thread(new Runnable()
136
 
//         {
137
 
//            public void run()
138
 
//            {
139
 
//               while(true)
140
 
//               {
141
 
//
142
 
//                  try
143
 
//                  {
144
 
//                     Thread.currentThread().sleep(1000);
145
 
//                  }
146
 
//                  catch(InterruptedException e)
147
 
//                  {
148
 
//                     e.printStackTrace();
149
 
//                  }
150
 
//
151
 
//                  while(sendCallbacks)
152
 
//                  {
153
 
//
154
 
//                     // Will also fire callback to listeners if they were to exist using
155
 
//                     // simple invocation request.
156
 
//                     Callback callback = new Callback(new Integer(callbackCounter));
157
 
//                     Iterator itr = listeners.iterator();
158
 
//                     while(itr.hasNext())
159
 
//                     {
160
 
//                        InvokerCallbackHandler callbackHandler = (InvokerCallbackHandler) itr.next();
161
 
//                        try
162
 
//                        {
163
 
//                           callbackHandler.handleCallback(callback);
164
 
//                        }
165
 
//                        catch(HandleCallbackException e)
166
 
//                        {
167
 
//                           e.printStackTrace();
168
 
//                        }
169
 
//                     }
170
 
//                  }
171
 
//               }
172
 
//
173
 
//            }
174
 
//         }).start();
175
 
//      }
176
 
//
177
 
//      /**
178
 
//       * Adds a callback handler that will listen for callbacks from
179
 
//       * the server invoker handler.
180
 
//       *
181
 
//       * @param callbackHandler
182
 
//       */
183
 
//      public void addListener(InvokerCallbackHandler callbackHandler)
184
 
//      {
185
 
//         listeners.add(callbackHandler);
186
 
//      }
187
 
//
188
 
//      /**
189
 
//       * Removes the callback handler that was listening for callbacks
190
 
//       * from the server invoker handler.
191
 
//       *
192
 
//       * @param callbackHandler
193
 
//       */
194
 
//      public void removeListener(InvokerCallbackHandler callbackHandler)
195
 
//      {
196
 
//         listeners.remove(callbackHandler);
197
 
//      }
198
 
//
199
 
//      /**
200
 
//       * set the mbean server that the handler can reference
201
 
//       *
202
 
//       * @param server
203
 
//       */
204
 
//      public void setMBeanServer(MBeanServer server)
205
 
//      {
206
 
//         // NO OP as do not need reference to MBeanServer for this handler
207
 
//      }
208
 
//
209
 
//      /**
210
 
//       * set the invoker that owns this handler
211
 
//       *
212
 
//       * @param invoker
213
 
//       */
214
 
//      public void setInvoker(ServerInvoker invoker)
215
 
//      {
216
 
//         // NO OP as do not need reference back to the server invoker
217
 
//      }
218
 
//
219
 
//   }
220
 
 
221
 
 
222
 
   public class CallbackHandler implements InvokerCallbackHandler
223
 
   {
224
 
      private Callback callback;
225
 
      private Integer callbackNumber;
226
 
 
227
 
      /**
228
 
       * Will take the callback message and send back to client.
229
 
       * If client locator is null, will store them till client polls to get them.
230
 
       *
231
 
       * @param callback
232
 
       * @throws org.jboss.remoting.callback.HandleCallbackException
233
 
       *
234
 
       */
235
 
      public void handleCallback(Callback callback) throws HandleCallbackException
236
 
      {
237
 
         this.callback = callback;
238
 
         System.out.println("Received callback value of: " + callback.getCallbackObject());
239
 
         System.out.println("Received callback handle object of: " + callback.getCallbackHandleObject());
240
 
         System.out.println("Received callback server invoker of: " + callback.getServerLocator());
241
 
 
242
 
         callbackNumber = (Integer) callback.getCallbackObject();
243
 
      }
244
 
 
245
 
      public int getCallbackNumber()
246
 
      {
247
 
         if(callbackNumber != null)
248
 
         {
249
 
            return callbackNumber.intValue();
250
 
         }
251
 
         else
252
 
         {
253
 
            return 0;
254
 
         }
255
 
      }
256
 
 
257
 
      public Callback getCallback()
258
 
      {
259
 
         return callback;
260
 
      }
261
 
 
262
 
   }
263
 
 
264
 
}
 
 
b'\\ No newline at end of file'