File: CallbackTestClient.java

package info (click to toggle)
libjboss-remoting-java 2.4.0~Beta2-1
  • links: PTS, VCS
  • area: contrib
  • in suites: lenny
  • size: 10,268 kB
  • ctags: 15,262
  • sloc: java: 115,889; xml: 1,019; makefile: 14; sh: 11
file content (146 lines) | stat: -rw-r--r-- 4,054 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package org.jboss.test.remoting.callback.pull;

import junit.framework.TestCase;
import org.jboss.remoting.Client;
import org.jboss.remoting.InvokerLocator;
import org.jboss.remoting.callback.Callback;
import org.jboss.remoting.callback.HandleCallbackException;
import org.jboss.remoting.callback.InvokerCallbackHandler;

import java.util.List;

/**
 * Tests that two separate clients with separate callback listeners
 * can be distinguished on the server and given different callback messages.
 *
 * @author <a href="mailto:telrod@e2technologies.net">Tom Elrod</a>
 */
public class CallbackTestClient extends TestCase
{
   // Default locator values
   private static String transport = "socket";
   private static String host = "localhost";
   private static int port = 5411;

   private String locatorURI = transport + "://" + host + ":" + port;

   private Client remotingClient;
   private CallbackHandler pullCallbackHandler;

   private Client remotingClient2;
   private CallbackHandler pullCallbackHandler2;

   public void createRemotingClient() throws Exception
   {
      InvokerLocator locator = new InvokerLocator(locatorURI);
      System.out.println("Calling remoting server with locator uri of: " + locatorURI);

      remotingClient = new Client(locator);
      remotingClient.connect();

      remotingClient2 = new Client(locator);
      remotingClient2.connect();

   }

   public void setUp() throws Exception
   {
      createRemotingClient();
   }

   public void tearDown() throws Exception
   {
      if(remotingClient != null)
      {
         if(pullCallbackHandler != null)
         {
            try
            {
               remotingClient.removeListener(pullCallbackHandler);
            }
            catch(Throwable throwable)
            {
               throw new Exception(throwable);
            }
         }
         remotingClient.disconnect();
      }
      if(remotingClient2 != null)
      {
         if(pullCallbackHandler2 != null)
         {
            try
            {
               remotingClient2.removeListener(pullCallbackHandler2);
            }
            catch(Throwable throwable)
            {
               throw new Exception(throwable);
            }
         }
         remotingClient2.disconnect();
      }
   }

   public void testPullCallback() throws Throwable
   {
      pullCallbackHandler = new CallbackHandler();
      pullCallbackHandler2 = new CallbackHandler();
      // by passing only the callback handler, will indicate pull callbacks
      remotingClient.addListener(pullCallbackHandler);
      remotingClient2.addListener(pullCallbackHandler2);

      // now make invocation on server, which should cause a callback to happen
      remotingClient.invoke("Do something");
      remotingClient2.invoke("Do something");

      Thread.sleep(5000);

      List callbacks = remotingClient.getCallbacks(pullCallbackHandler);
      List callbacks2 = remotingClient2.getCallbacks(pullCallbackHandler2);

      boolean callbackWorked = false;

      if(callbacks != null && callbacks.size() > 0)
      {
         for(int x = 0; x < callbacks.size(); x++)
         {
            Callback c = (Callback)callbacks.get(x);
            callbackWorked = c.getCallbackObject().equals(remotingClient.getSessionId());
            if(!callbackWorked)
            {
               break;
            }
         }
      }

      assertTrue(callbackWorked);

      boolean callbackWorked2 = false;

      if(callbacks2 != null && callbacks2.size() > 0)
      {
         for(int x = 0; x < callbacks2.size(); x++)
         {
            Callback c = (Callback)callbacks2.get(x);
            callbackWorked2 = c.getCallbackObject().equals(remotingClient2.getSessionId());
            if(!callbackWorked2)
            {
               break;
            }
         }
      }

      assertTrue(callbackWorked2);

   }

   public class CallbackHandler implements InvokerCallbackHandler
   {
      public void handleCallback(Callback callback) throws HandleCallbackException
      {
      }

   }

}