<document>
  <header>
    <product>resin</product>
    <title>Hessian Service</title>
    <description>
      <p>Writing a Hessian service as a plain-old Java object (POJO)
eliminates protocol dependencies and simplifies service testing.
</p>
    </description>
    <type>tutorial</type>
    <tutorial-startpage>demo.jsp</tutorial-startpage>
  </header>

  <body>
    <summary/>

<s1 title="Files in this tutorial">
<deftable>
<tr><td><viewfile-link file="WEB-INF/classes/example/LogService.java"/>
    </td><td>Interface for the logging service.
</td></tr><tr><td><viewfile-link file="WEB-INF/classes/example/LogServiceImpl.java"/>
    </td><td>The main service implementation.
</td></tr><tr><td><viewfile-link file="WEB-INF/web.xml"/>
    </td><td>Configures the environment
</td></tr><tr><td><viewfile-link file="demo.jsp"/>
    </td><td>Client JSP
</td></tr></deftable>
</s1>

<s1 title="Service Implementation">

<p>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.</p>

<example title="LogService.java" language="java">
package example;

public interface LogService {
  public void log(String message);
  public String getLog();
}
</example>

<example title="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();
  }
}
</example>

<p>
The configuration for this example is the following:
</p>

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

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

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

<p>
Finally, the service is called from a JSP page:
</p>

<example title="demo.jsp">
&lt;%@ page import="javax.naming.Context" %>
&lt;%@ page import="javax.naming.InitialContext" %>

&lt;%@ page import="com.caucho.hessian.client.HessianProxyFactory" %>

&lt;%@ page import="example.LogService" %>

&lt;%
// 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:&lt;br/>");
out.println("&lt;pre>");
out.println(logService.getLog());
out.println("&lt;/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");
%>
</example>

<p>
<a href="demo.jsp">Try the example</a>
</p>

</s1>

  </body>
</document>
