Hessian JMS Example

From Resin 4.0 Wiki

Jump to: navigation, search

Contents

Hessian Service

Writing a Hessian service as a plain-old Java object (POJO) eliminates protocol dependencies and simplifies service testing.

Files in this tutorial

File Description
WEB-INF/classes/example/LogService.java Interface for the logging service.
WEB-INF/classes/example/LogServiceImpl.java The main service implementation.
WEB-INF/web.xml Configures the environment
demo.jsp Client JSP

Service Implementation

The HelloService implementation is just a Java class that implements the HelloService API. This service responds by placing the results of the request on a JMS queue.

LogService.java" language="java

package example;

public interface LogService {
  public void log(String message);
  public String getLog();
}

LogServiceImpl.java" language="java

package example;

public class LogServiceImpl implements LogService {
  private int _sequenceNumber = 1;
  private StringBuilder _log = new StringBuilder();

  public void log(String message)
  {
    _log.append(_sequenceNumber + ": " + message + "\n");
    _sequenceNumber++;
  }

  public String getLog()
  {
    return _log.toString();
  }
}

The configuration for this example is the following:

web.xml" language="xml

<web-app xmlns="http://caucho.com/ns/resin">
  <resource var="logService"
            jndi-name="example/LogService"
            type="example.LogServiceImpl" />

  <resource var="serviceQueue" 
            jndi-name="jms/ServiceQueue"
            type="com.caucho.jms.memory.MemoryQueue" />
  
  <resource var="jmsFactory"
            jndi-name="jms/ConnectionFactory"
            type="com.caucho.jms.ConnectionFactoryImpl" />

  <resource type="com.caucho.hessian.server.HessianListener">
    <init>
      <connection-factory>${jmsFactory}</connection-factory>
      <destination>${serviceQueue}</destination>
      <service>${logService}</service>
    </init>
  </resource>
</web-app>

Finally, the service is called from a JSP page:

demo.jsp

<%@ page import="javax.naming.Context" %>
<%@ page import="javax.naming.InitialContext" %>

<%@ page import="com.caucho.hessian.client.HessianProxyFactory" %>

<%@ page import="example.LogService" %>

<%
// Check the log

Context context = (Context) new InitialContext().lookup("java:comp/env");
LogService logService = (LogService) context.lookup("example/LogService");

out.println("<a href=\"\">Refresh</a><br/>");
out.println("Logged messages:<br/>");
out.println("<pre>");
out.println(logService.getLog());
out.println("</pre>");

// Make a request

HessianProxyFactory factory = new HessianProxyFactory();

String url = "jms:jms/ServiceQueue";

LogService log = (LogService) factory.create(LogService.class, url);

log.log("Hello, World");
%>
Personal tools
TOOLBOX
LANGUAGES