KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > remoting > samples > oneway > OnewayServer


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.remoting.samples.oneway;
8
9 import javax.management.MBeanServer JavaDoc;
10 import org.jboss.remoting.InvocationRequest;
11 import org.jboss.remoting.InvokerLocator;
12 import org.jboss.remoting.ServerInvocationHandler;
13 import org.jboss.remoting.ServerInvoker;
14 import org.jboss.remoting.callback.InvokerCallbackHandler;
15 import org.jboss.remoting.transport.Connector;
16
17 /**
18  * Simple remoting server. Uses inner class SampleInvocationHandler
19  * as the invocation target handler class.
20  *
21  * @author <a HREF="mailto:telrod@e2technologies.net">Tom Elrod</a>
22  */

23 public class OnewayServer
24 {
25    // Default locator values
26
private static String JavaDoc transport = "socket";
27    private static String JavaDoc host = "localhost";
28    private static int port = 5400;
29
30    public void setupServer(String JavaDoc locatorURI) throws Exception JavaDoc
31    {
32       // create the InvokerLocator based on url string format
33
// to indicate the transport, host, and port to use for the
34
// server invoker.
35
InvokerLocator locator = new InvokerLocator(locatorURI);
36       System.out.println("Starting remoting server with locator uri of: " + locatorURI);
37       Connector connector = new Connector();
38       connector.setInvokerLocator(locator.getLocatorURI());
39       // creates all the connector's needed resources, such as the server invoker
40
connector.create();
41
42       // create the handler to receive the invocation request from the client for processing
43
SampleInvocationHandler invocationHandler = new SampleInvocationHandler();
44       // first parameter is sub-system name. can be any String value.
45
connector.addInvocationHandler("sample", invocationHandler);
46
47       connector.start();
48    }
49
50    /**
51     * Can pass transport and port to be used as parameters.
52     * Valid transports are 'rmi' and 'socket'.
53     *
54     * @param args
55     */

56    public static void main(String JavaDoc[] args)
57    {
58       if(args != null && args.length == 2)
59       {
60          transport = args[0];
61          port = Integer.parseInt(args[1]);
62       }
63       String JavaDoc locatorURI = transport + "://" + host + ":" + port;
64       OnewayServer server = new OnewayServer();
65       try
66       {
67          server.setupServer(locatorURI);
68
69          // wait forever, let the user kill us at any point (at which point, the client will detect we went down)
70
while(true)
71          {
72             Thread.sleep(1000);
73          }
74
75       }
76       catch(Exception JavaDoc e)
77       {
78          e.printStackTrace();
79       }
80    }
81
82    /**
83     * Simple invocation handler implementation.
84     * This is the code that will be called with the invocation payload from the client.
85     */

86    public static class SampleInvocationHandler implements ServerInvocationHandler
87    {
88       /**
89        * called to handle a specific invocation
90        *
91        * @param invocation
92        * @return
93        * @throws Throwable
94        */

95       public Object JavaDoc invoke(InvocationRequest invocation) throws Throwable JavaDoc
96       {
97          // Print out the invocation request
98
System.out.println("Invocation request is: " + invocation.getParameter());
99          return null;
100       }
101
102       /**
103        * Adds a callback handler that will listen for callbacks from
104        * the server invoker handler.
105        *
106        * @param callbackHandler
107        */

108       public void addListener(InvokerCallbackHandler callbackHandler)
109       {
110          // NO OP as do not handling callback listeners in this example
111
}
112
113       /**
114        * Removes the callback handler that was listening for callbacks
115        * from the server invoker handler.
116        *
117        * @param callbackHandler
118        */

119       public void removeListener(InvokerCallbackHandler callbackHandler)
120       {
121          // NO OP as do not handling callback listeners in this example
122
}
123
124       /**
125        * set the mbean server that the handler can reference
126        *
127        * @param server
128        */

129       public void setMBeanServer(MBeanServer JavaDoc server)
130       {
131          // NO OP as do not need reference to MBeanServer for this handler
132
}
133
134       /**
135        * set the invoker that owns this handler
136        *
137        * @param invoker
138        */

139       public void setInvoker(ServerInvoker invoker)
140       {
141          // NO OP as do not need reference back to the server invoker
142
}
143
144    }
145 }
Popular Tags