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

« back to all changes in this revision

Viewing changes to examples/org/jboss/remoting/samples/callback/acknowledgement/CallbackAcknowledgeServer.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.HashMap;
37
 
 
38
 
import javax.management.MBeanServer;
39
 
 
40
 
import org.jboss.remoting.InvocationRequest;
41
 
import org.jboss.remoting.InvokerLocator;
42
 
import org.jboss.remoting.ServerInvocationHandler;
43
 
import org.jboss.remoting.ServerInvoker;
44
 
import org.jboss.remoting.callback.Callback;
45
 
import org.jboss.remoting.callback.CallbackListener;
46
 
import org.jboss.remoting.callback.InvokerCallbackHandler;
47
 
import org.jboss.remoting.callback.ServerInvokerCallbackHandler;
48
 
import org.jboss.remoting.transport.Connector;
49
 
import org.jboss.util.id.GUID;
50
 
 
51
 
 
52
 
public class CallbackAcknowledgeServer
53
 
{
54
 
   public static final String APPLICATION_ACKNOWLDEGEMENTS = "applicationAcknowledgements";
55
 
   public static final String REMOTING_ACKNOWLDEGEMENTS = "remotingAcknowledgements";
56
 
   
57
 
   private static String transport = "socket";
58
 
   private static String host;
59
 
   private static int port = 5401;
60
 
   
61
 
   private Connector connector;
62
 
 
63
 
   /**
64
 
    * Can pass transport and port to be used as parameters.
65
 
    * Default to "socket" and 5401.
66
 
    *
67
 
    * @param args
68
 
    */
69
 
   public static void main(String[] args)
70
 
   {
71
 
      if(args != null && args.length == 2)
72
 
      {
73
 
         transport = args[0];
74
 
         port = Integer.parseInt(args[1]);
75
 
      }
76
 
      try
77
 
      {
78
 
         host = InetAddress.getLocalHost().getHostName();
79
 
      }
80
 
      catch (UnknownHostException e1)
81
 
      {
82
 
         System.err.println("cannot get local host name");
83
 
         return;
84
 
      }
85
 
      String locatorURI = transport + "://" + host + ":" + port;
86
 
      CallbackAcknowledgeServer server = new CallbackAcknowledgeServer();
87
 
      try
88
 
      {
89
 
         server.setupServer(locatorURI);
90
 
 
91
 
         // wait forever, let the user kill us at any point (at which point, the client will detect we went down)
92
 
         while(true)
93
 
         {
94
 
            Thread.sleep(1000);
95
 
         }
96
 
 
97
 
      }
98
 
      catch(Exception e)
99
 
      {
100
 
         e.printStackTrace();
101
 
      }
102
 
   }
103
 
   
104
 
   public void setupServer(String locatorURI) throws Exception
105
 
   {
106
 
      connector = new Connector(new InvokerLocator(locatorURI));
107
 
      connector.start();
108
 
      connector.addInvocationHandler("test", new TestInvoocationHandler());
109
 
   }
110
 
 
111
 
   
112
 
   static class TestInvoocationHandler implements ServerInvocationHandler, CallbackListener
113
 
   {
114
 
      InvokerCallbackHandler callbackHandler;
115
 
      int counter;
116
 
      
117
 
      public void setMBeanServer(MBeanServer server) {}
118
 
 
119
 
      public void setInvoker(ServerInvoker invoker) {}
120
 
 
121
 
      public Object invoke(InvocationRequest invocation) throws Throwable
122
 
      {
123
 
         String command = (String) invocation.getParameter();
124
 
         System.out.println("command: " + command);
125
 
         
126
 
         Callback cb = new Callback("callback " + ++counter);
127
 
         
128
 
         // Register as listener and pass callback id.
129
 
         HashMap returnPayload = new HashMap();
130
 
         returnPayload.put(ServerInvokerCallbackHandler.CALLBACK_LISTENER, this);
131
 
         returnPayload.put(ServerInvokerCallbackHandler.CALLBACK_ID, new GUID());
132
 
         cb.setReturnPayload(returnPayload);
133
 
         
134
 
         if (REMOTING_ACKNOWLDEGEMENTS.equals(command))
135
 
         {
136
 
            returnPayload.put(ServerInvokerCallbackHandler.REMOTING_ACKNOWLEDGES_PUSH_CALLBACKS, "true");
137
 
         }
138
 
 
139
 
         callbackHandler.handleCallback(cb);         
140
 
         return null;
141
 
      }
142
 
 
143
 
      public void addListener(InvokerCallbackHandler callbackHandler)
144
 
      {
145
 
         this.callbackHandler = callbackHandler;
146
 
      }
147
 
 
148
 
      public void removeListener(InvokerCallbackHandler callbackHandler) {}
149
 
 
150
 
      /**
151
 
       * callbackSent() is called to acknowledgement the sending of a callback.
152
 
       */
153
 
      public void acknowledgeCallback(InvokerCallbackHandler callbackHandler, Object callbackId, Object response)
154
 
      {
155
 
         System.out.println("received acknowledgment for callback: " + callbackId);
156
 
         System.out.println("response: " + response);
157
 
         System.out.println("");
158
 
      }
159
 
   }
160
 
}