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

« back to all changes in this revision

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