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

« back to all changes in this revision

Viewing changes to src/main/org/jboss/remoting/samples/multiplex/invoker/Client3Server1.java

  • Committer: Package Import Robot
  • Author(s): Torsten Werner
  • Date: 2011-09-09 14:01:03 UTC
  • mto: This revision was merged to the branch mainline in revision 9.
  • Revision ID: package-import@ubuntu.com-20110909140103-o8ucrolqt5g25k57
Tags: upstream-2.5.3.SP1
ImportĀ upstreamĀ versionĀ 2.5.3.SP1

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
 * Created on Oct 4, 2005
 
25
 */
 
26
package org.jboss.remoting.samples.multiplex.invoker;
 
27
/*
 
28
 * JBoss, the OpenSource J2EE webOS
 
29
 *
 
30
 * Distributable under LGPL license.
 
31
 * See terms of license at gnu.org.
 
32
 */
 
33
 
 
34
import java.util.HashMap;
 
35
import java.util.Map;
 
36
 
 
37
import org.jboss.remoting.Client;
 
38
import org.jboss.remoting.InvokerLocator;
 
39
import org.jboss.remoting.callback.Callback;
 
40
import org.jboss.remoting.callback.HandleCallbackException;
 
41
import org.jboss.remoting.callback.InvokerCallbackHandler;
 
42
import org.jboss.remoting.transport.Connector;
 
43
import org.jboss.remoting.transport.multiplex.Multiplex;
 
44
 
 
45
 
 
46
/**
 
47
 * @author <a href="mailto:r.sigal@computer.org">Ron Sigal</a>
 
48
 */
 
49
public class Client3Server1
 
50
{
 
51
   private Client client;
 
52
   private SampleCallbackHandler handler;
 
53
   private Connector connector;
 
54
   private InvokerLocator locator;
 
55
 
 
56
 
 
57
   /**
 
58
    *  This will create the Client.
 
59
    */
 
60
   public void init()
 
61
   {
 
62
      try
 
63
      {
 
64
         String locatorURI = "multiplex://localhost:9090";
 
65
         InvokerLocator locator = new InvokerLocator(locatorURI);
 
66
         Map configuration = new HashMap();
 
67
         configuration.put(Multiplex.CLIENT_MULTIPLEX_ID, "client3server1");
 
68
         client = new Client(locator, "sample", configuration);
 
69
      }
 
70
      catch(Exception e)
 
71
      {
 
72
         e.printStackTrace();
 
73
      }
 
74
   }
 
75
 
 
76
  
 
77
   /**
 
78
    * This will be used to create callback server
 
79
    *
 
80
    * @param port
 
81
    * @return
 
82
    * @throws Exception
 
83
    */
 
84
   private InvokerLocator initServer() throws Exception
 
85
   {
 
86
      String locatorURI = "multiplex://localhost:8080";
 
87
      InvokerLocator locator = new InvokerLocator(locatorURI);
 
88
      Map configuration = new HashMap();
 
89
      configuration.put(Multiplex.SERVER_MULTIPLEX_ID, "client3server1");
 
90
      connector = new Connector(locator.getLocatorURI(), configuration);
 
91
      connector.create();
 
92
      connector.start();
 
93
      System.out.println("Started callback server at:    " + connector.getInvokerLocator());
 
94
      return connector.getLocator();
 
95
   }
 
96
 
 
97
 
 
98
   public void setUp() throws Exception
 
99
   {
 
100
      init();
 
101
      locator = initServer();
 
102
      client.connect();
 
103
      System.out.println("Connected client to server at: " + client.getInvoker().getLocator().getLocatorURI());
 
104
   }
 
105
 
 
106
   public void tearDown() throws Throwable
 
107
   {
 
108
      while (!handler.gotCallbacks)
 
109
         Thread.sleep(1000);
 
110
      
 
111
      client.removeListener(handler);
 
112
      
 
113
      if(connector != null)
 
114
      {
 
115
         connector.stop();
 
116
         connector.destroy();
 
117
         connector = null;
 
118
      }
 
119
      locator = null;
 
120
      if(client != null)
 
121
      {
 
122
         client.disconnect();
 
123
         client = null;
 
124
      }
 
125
   }
 
126
 
 
127
 
 
128
   public void makeClientCall() throws Throwable
 
129
   {
 
130
      handler = new SampleCallbackHandler();
 
131
 
 
132
      // Need to add callback listener to get callback
 
133
      client.addListener(handler, locator, client.getSessionId());
 
134
 
 
135
      // make invocation
 
136
      Object answer = client.invoke(new Integer(17));
 
137
      
 
138
      System.out.println("invocation returns: " + ((Integer) answer).intValue());
 
139
   }
 
140
 
 
141
 
 
142
   public static void main(String[] args)
 
143
   {
 
144
      Client3Server1 test = new Client3Server1();
 
145
      
 
146
      try
 
147
      {
 
148
         test.setUp();
 
149
         test.makeClientCall();
 
150
         test.tearDown();
 
151
      }
 
152
      catch(Throwable throwable)
 
153
      {
 
154
         throwable.printStackTrace();
 
155
      }
 
156
   }
 
157
 
 
158
   
 
159
   public static class SampleCallbackHandler implements InvokerCallbackHandler
 
160
   {
 
161
      int callbackCounter;
 
162
      boolean gotCallbacks;
 
163
      
 
164
      public void handleCallback(Callback callback) throws HandleCallbackException
 
165
      {
 
166
         Object ret = callback.getCallbackObject();
 
167
         System.out.println("callback value: " + ret);
 
168
 
 
169
         if (++callbackCounter == 2)
 
170
            gotCallbacks = true;
 
171
      }
 
172
      
 
173
      public boolean gotCallbacks()
 
174
      {
 
175
         return gotCallbacks;
 
176
      }
 
177
   }
 
178
}
 
179
 
 
180