[jboss-user] [Remoting] - Re: Writing a custom invoker howto?

ron.sigal@jboss.com do-not-reply at jboss.com
Mon Jul 20 22:10:43 EDT 2009


Well, I've written probably the simplest possible transport, called "test".  It's dumb, but it works.

The client invoker is


  | package org.jboss.remoting.transport.test;
  | 
  | /* imports */
  | 
  | public class TestClientInvoker extends RemoteClientInvoker {
  |    
  |    public TestClientInvoker(InvokerLocator locator, Map configuration) {
  |       super(locator, configuration);
  |    }
  | 
  |    protected String getDefaultDataType() {
  |       return SerializableMarshaller.DATATYPE;
  |    }
  | 
  |    protected void handleConnect() throws ConnectionFailedException {
  |    }
  | 
  |    protected void handleDisconnect() {
  |    }
  | 
  |    protected Object transport(String sessionId, Object invocation, Map metadata, Marshaller marshaller, UnMarshaller unmarshaller)
  |    throws IOException, ConnectionFailedException, ClassNotFoundException {
  |       Socket s = new Socket(locator.getHost(), locator.getPort());
  |       InputStream is = s.getInputStream();
  |       OutputStream os = s.getOutputStream();
  |       System.out.println(this + " writing " + invocation);
  |       marshaller.write(invocation, os);
  |       Object response = unmarshaller.read(is, metadata);
  |       System.out.println(this + " returning " + response);
  |       s.close();
  |       return response;
  |    }
  | }
  | 

and the server invoker is


  | package org.jboss.remoting.transport.test;
  | 
  | /* imports */
  | 
  | public class TestServerInvoker extends ServerInvoker
  | {
  |    private Thread t;
  |    private ServerSocket ss;
  |    private boolean running;
  |    private UnMarshaller unmarshaller;
  |    private Marshaller marshaller;
  |    
  |    public TestServerInvoker(InvokerLocator locator, Map configuration) {
  |       super(locator, configuration);
  |    }
  | 
  |    protected String getDefaultDataType() {
  |       return SerializableMarshaller.DATATYPE;
  |    }
  | 
  |    public boolean isTransportBiDirectional() {
  |       return false;
  |    }
  |    
  |    public void start() throws IOException {
  |       super.start();
  |       unmarshaller = MarshalFactory.getUnMarshaller(getDataType(), serializationType);
  |       marshaller = MarshalFactory.getMarshaller(getDataType(), serializationType);
  |       ss = new ServerSocket(locator.getPort(), -1, InetAddress.getByName(locator.getHost()));
  |       t = new Thread() {
  |          public void run() {
  |             try {
  |                while (running) {
  |                   Socket s = ss.accept();
  |                   InputStream is = s.getInputStream();
  |                   OutputStream os = s.getOutputStream();
  |                   Object request = unmarshaller.read(is, null);
  |                   System.out.println(TestServerInvoker.this + " read " + request);
  |                   Object response = invoke(request);
  |                   System.out.println(TestServerInvoker.this + " returning " + response);
  |                   marshaller.write(response, os);
  |                   s.close();
  |                }
  |             } catch (Exception e) {
  |                running = false;
  |             }
  |          }
  |       };
  |       t.start();
  |       running = true;
  |    }
  |    
  |    public void stop() {
  |       running = false;
  |       try {
  |          ss.close();
  |       } catch (IOException e) {
  |       }
  |    }
  | }
  | 

Note that by setting the default datatype to "serializable", I'm reusing the standard serializing marshaller and unmarshaller used by, for example, the "socket" transport.

Hope that helps.

View the original post : http://www.jboss.org/index.html?module=bb&op=viewtopic&p=4244786#4244786

Reply to the post : http://www.jboss.org/index.html?module=bb&op=posting&mode=reply&p=4244786



More information about the jboss-user mailing list