Binary WebSockets Messaging with Hessian

From Resin 4.0 Wiki

(Difference between revisions)
Jump to: navigation, search
(Created page with "{{Messaging}} {{Cookbook}} === HessianEchoListener implements WebSocketListener === import com.caucho.websocket.*; import com.caucho.hessian.io.*; public class HessianEc...")
 
Line 29: Line 29:
 
   }
 
   }
 
  }
 
  }
 +
 +
Note: a second technique would create a new Hessian2Input and Hessian2Output for each request, instead of reusing it with the initPacket. When you use initPacket, the Hessian streams clear the object references but *keep* the class references. So if you use initPacket, you further compress your hessian stream by only sending the class information once. If you use initPacket, you must only use it for the existing stream because class references must stay in sync between the reader and the writer. (It would be bad if #4 was qa.MyBean in the writer, but #6 for the reader. If you keep them in sync, both will be #4.)
 +
 +
=== HessianWebSocketServlet extends GenericServlet ===
 +
 +
  import com.caucho.websocket.*;
 +
  import java.io.*;
 +
 +
  public class HessianWebSocketServlet extends GenericServlet {
 +
  {
 +
    private HessianFactory _factory = new HessianFactory();
 +
 +
    public void service(ServletRequest request, ServletResponse res)
 +
      throws IOException, ServletException
 +
    {
 +
      WebSocketServletRequest req = (WebSocketServletRequest) request;
 +
   
 +
      req.startWebSocket(new HessianWebSocketListener(factory));
 +
    }
 +
  }

Revision as of 00:00, 13 January 2012

Email-48.pngCookbook-48.png

HessianEchoListener implements WebSocketListener

import com.caucho.websocket.*;
import com.caucho.hessian.io.*;

public class HessianEchoListener extends AbstractWebSocketListener {
  Hessian2Input _hIn;
  Hessian2Output _hOut;

  HessianEchoListener(HessianFactory factory)
  {
    _hIn = factory.createHessian2Input(null);
    _hOut = factory.createHessian2Output(null);
  }

  @Override
  public void onReadBinary(WebSocketContext context, InputStream is)
    throws IOException
  {
    _hIn.initPacket(is);

    Object obj = _hIn.readObject();

    OutputStream os = context.beginBinaryMessage();
    _hOut.initPacket(os);
    _hOut.writeObject(obj);
    _hOut.close();
  }
}

Note: a second technique would create a new Hessian2Input and Hessian2Output for each request, instead of reusing it with the initPacket. When you use initPacket, the Hessian streams clear the object references but *keep* the class references. So if you use initPacket, you further compress your hessian stream by only sending the class information once. If you use initPacket, you must only use it for the existing stream because class references must stay in sync between the reader and the writer. (It would be bad if #4 was qa.MyBean in the writer, but #6 for the reader. If you keep them in sync, both will be #4.)

HessianWebSocketServlet extends GenericServlet

 import com.caucho.websocket.*;
 import java.io.*;

 public class HessianWebSocketServlet extends GenericServlet {
 {
   private HessianFactory _factory = new HessianFactory();

   public void service(ServletRequest request, ServletResponse res)
     throws IOException, ServletException
   {
     WebSocketServletRequest req = (WebSocketServletRequest) request;
   
     req.startWebSocket(new HessianWebSocketListener(factory));
   }
 }
Personal tools
TOOLBOX
LANGUAGES