Resin 4 Hessian Examples

From Resin 4.0 Wiki

(Difference between revisions)
Jump to: navigation, search
(Created page with " <p>The addition example creates a Hessian web services with a servlet and uses that web service from a JSP client and a Python client.</p> <p>Hessian is a lightweigh...")
 
Line 1: Line 1:
 
      <p>The addition example creates a Hessian web services
 
with a servlet and uses that web service from a
 
JSP client and a Python client.</p>
 
 
<p>Hessian is a lightweight binary RPC protocol.  Transferring binary objects
 
like files, images, or mp3s can avoid the protocol overhead
 
that XML-based protocols require.  Since it's simple, its performance
 
should be usable for almost all sites.  Hessian is designed to be
 
self-describing, eliminating the requirement for external IDLs or WSDL files.
 
Because it is as small as possible and language-independent,
 
non-Java Hessian implementations are can easily develop comprehensive
 
test suites.</p>
 
 
<p>This tutorial only requires the open source Java implementation of
 
the Hessian client and server.  It can be downloaded
 
from <a href="http://caucho.com/hessian/">http://www.caucho.com/hessian/</a>
 
for non-Resin clients and servers.</p>
 
 
<p>Because Resin's EJB implementation uses Hessian as its primary
 
remote procedure call protocol, EJB developers can easily expose
 
services to clients from other languages.</p>
 
 
<p>Because EJB clients and servers are written without knowledge of the
 
underlying protocol, even if you intend to deploy with another protocol,
 
like RMI/IIOP, you can develop using Resin's Hessian.</p>
 
 
<p>The <a href="doc|hessian-1.0-spec.xtp">Hessian 1.0 spec</a>
 
and  <a href="doc|hessian-2.0-spec.xtp">Hessian 2.0 spec</a> describe
 
the full Hessian protocol.</p>
 
 
=Files in this tutorial=
 
<table>
 
<tr>
 
  <th>File</th>
 
  <th>Description</th>
 
</tr>
 
<tr><td><viewfile-link file="WEB-INF/classes/example/MathService.java"/>
 
    </td><td>Interface for the math service.
 
</td></tr><tr><td><viewfile-link file="WEB-INF/classes/example/HessianMathService.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></table>
 
 
=The Hessian Protocol=
 
 
<p>A Hessian call is just an HTTP POST to a URL.  The arguments are
 
serialized into the Hessian binary format and passed to the server.</p>
 
 
<p>Most applications will never need to look at the Hessian
 
protocol, but it's simple enough that a basic example can help show
 
what's happening underneath the API.</p>
 
 
====Hessian call====
 
<pre>
 
c x01 x00
 
  m x00 x03 add
 
  I x00 x00 x00 x02
 
  I x00 x00 x00 x03
 
z
 
</pre>
 
 
====Hessian reply====
 
<pre>
 
r x01 x00
 
  I x00 x00 x00 x05
 
z
 
</pre>
 
 
<p>The call does not need to specify the service name because the
 
service is uniquely specified by the URL.</p>
 
 
<p>The following Addition example shows how to create a basic
 
server so you can test Hessian.</p>
 
 
 
=A Hessian Example=
 
<p>Using Hessian generally uses three components:</p>
 
<ol>
 
<li>A remote interface
 
</li><li>The server implementation
 
</li><li>The client (JSP or servlet)
 
</li></ol>
 
 
<p>The remote interface is used by the Hessian proxy
 
factory to create a proxy stub implementing the service's interface.</p>
 
 
 
=Service Implementation=
 
 
<p>Resin's Hessian provides a simple way of creating a server.  Just extend
 
<code>HessianServlet</code> with your remote methods.  The Hessian call will just
 
be a POST to that servlet.  HessianServlet will introspect the
 
service and expose the methods.</p>
 
 
====HessianMathService.java====
 
<pre>
 
package example;
 
 
import com.caucho.hessian.server.HessianServlet;
 
 
public class HessianMathService extends HessianServlet {
 
  public int add(int a, int b)
 
  {
 
    return a + b;
 
  }
 
}
 
</pre>
 
 
 
=Remote Interface=
 
 
<p>The Java interface describes the remote API.  This example has an
 
addition method, <var>add()</var>.</p>
 
 
<p>Resin's proxy client implementation uses the remote interface to
 
expose the API to the proxy stub.  Strictly speaking, though,
 
the Java remote interface is not required for Hessian.  A non-Java client
 
will not use the Java interface, except possibly as documentation.</p>
 
 
====MathService.java====
 
<pre>
 
package example;
 
 
public interface MathService {
 
  public int add(int a, int b);
 
}
 
</pre>
 
 
 
=Java Client=
 
 
<p>RPC clients follow the following steps in using a remote object:</p>
 
<ol>
 
<li>Determine the URL of the remote object.
 
</li><li>Obtain a proxy stub from a proxy factory.
 
</li><li>Call methods on the proxy stub.
 
</li></ol>
 
 
====client.jsp====
 
<pre>
 
<%@ page import="com.caucho.hessian.client.HessianProxyFactory" %>
 
<%@ page import="example.MathService" %>
 
<%
 
HessianProxyFactory factory = new HessianProxyFactory();
 
 
// http://localhost:8080/resin-doc/protocols/tutorial/hessian-add/hessian/math
 
 
String url = ("http://" +
 
              request.getServerName() + ":" + request.getServerPort() +
 
              request.getContextPath() + "/hessian/math");
 
 
MathService math = (MathService) factory.create(MathService.class, url);
 
 
out.println("3 + 2 = " + math.add(3, 2));
 
%>
 
</pre>
 
<pre>
 
3 + 2 = 5
 
</pre>
 
 
 
=Python Client=
 
 
<p>The <a href="http://www.caucho.com/hessian">Hessian site</a> has
 
a basic Python library for Hessian.</p>
 
 
====client.py====
 
<pre>
 
from hessianlib import Hessian
 
 
site = "http://localhost:8080/resin-doc/protocols/tutorial/hessian-add"
 
url = site + "/hessian/math"
 
 
proxy = Hessian(url);
 
 
print "3 + 2 =", proxy.add(2, 3)
 
</pre>
 
<pre>
 
3 + 2 = 5
 
</pre>
 
 
 
 
      <p>Using Hessian with Dependency Injection pattern
 
creates services which are simpler, protocol-independent and more easily
 
tested.  The Dependency Injection pattern (aka inversion of control)
 
gives Resin the responsibility of configuring and assembling
 
the service, protocol, and client.</p>
 
 
=Files in this tutorial=
 
<table>
 
<tr>
 
  <th>File</th>
 
  <th>Description</th>
 
</tr>
 
<tr>
 
  <td><viewfile-link file="WEB-INF/classes/example/GreetingAPI.java"/></td>
 
  <td>Interface for the greeting service.</td>
 
</tr>
 
<tr>
 
  <td><viewfile-link file="WEB-INF/classes/example/GreetingImpl.java"/></td>
 
  <td>The service implementation as a Java object</td>
 
</tr>
 
<tr>
 
  <td><viewfile-link file="WEB-INF/resin-web.xml"/></td>
 
  <td>Configures the environment</td>
 
</tr>
 
<tr>
 
  <td><viewfile-link file="WEB-INF/classes/example/GreetingClientServlet.java"/></td>
 
  <td>Client Servlet</td>
 
</tr>
 
</table>
 
 
=Service Implementation=
 
 
====GreetingAPI.java====
 
<pre>
 
package example;
 
 
public interface GreetingAPI {
 
  public String hello();
 
}
 
</pre>
 
 
==Service Implementation==
 
 
 
<p>The Greeting implementation is a plain Java class that implements
 
the MatchService API.  Making the service a plain class offers a number of advantages:</p>
 
 
<ul>
 
<li><b>Simplicity:</b> It can concentrate on its business logic because it doesn't need to implement any protocol-dependent code.
 
</li>
 
<li><b>Independence:</b> It can be reused more easily because it isn't tied to a distributed framework (e.g. in contrast to EJB).
 
</li>
 
<li><b>Testability:</b> It is more easily tested since the test harness doesn't need to implement the protocol or its stubs.  The harness can just test the service as a plain old Java object.
 
</li>
 
</ul>
 
 
<example title="GreetingImpl.java">
 
package example;
 
 
public class GreetingImpl implements GreetingAPI {
 
  private String _greeting = "Hello, world";
 
 
  public void setGreeting(String greeting)
 
  {
 
    _greeting = greeting;
 
  }
 
 
  public String greeting()
 
  {
 
    return _greeting;
 
  }
 
}
 
</example>
 
 
 
 
==Configuring the Service==
 
 
 
<p>URL-based services can use the servlet configuration to define the
 
service.  The service class can use
 
<a href="../../doc/resin-ioc.xtp">Resin IoC</a> to inject its
 
own dependencies.</p>
 
 
<example title="resin-web.xml">
 
&lt;web-app xmlns="http://caucho.com/ns/resin"
 
        xmlns:resin="urn:java:com.caucho.resin"
 
        xmlns:example="urn:java:example">
 
 
  &lt;example:GreetingImpl>
 
    &lt;resin:Unbound/>
 
    &lt;resin:HessianService urlPattern="/hessian/greeting"/>
 
   
 
    &lt;greeting>Hello from resin-web.xml&lt;/greeting>
 
  &lt;/example:GreetingImpl>
 
 
&lt;/web-app>
 
</example>
 
 
 
 
 
=Client=
 
 
<p>Configuring the client servlet with Dependency Injection
 
allows for a simpler and more general client.  The following client
 
can use any proxy/stub which implements the GreetingAPI without change,
 
for example:</p>
 
 
<ul>
 
  <li>Hessian proxy</li>
 
  <li>Burlap proxy</li>
 
  <li>EJB local object stub</li>
 
  <li>JMX proxy</li>
 
  <li>Java bean instance</li>
 
</ul>
 
 
<p>Using the Dependency Injection pattern, the servlet doesn't care
 
how the proxy is implemented, or how the greeting service is
 
discovered.</p>
 
 
====GreetingClientServlet.java====
 
<pre>
 
import javax.inject.Inject;
 
 
public class GreetingClientServlet extends GenericServlet {
 
  @Inject private GreetingAPI _greeting;
 
 
  public void service(ServletRequest req, ServletResponse res)
 
    throws IOException, ServletException
 
  {
 
    PrintWriter out = res.getWriter();
 
 
    out.println("Hello: " + _greeting.greeting());
 
  }
 
}
 
</pre>
 
 
==Hessian Client using Dependency Injection==
 
 
 
<p>The following code defines the client in the resin.web.xml.
 
The servlet defined above will inject the <code>GreetingAPI</code>
 
directly with the WebBeans <code>@In</code> annotation.  Because the
 
<code>GreetingAPI</code> is unique, there's no need to give it a name.</p>
 
 
<example title="resin-web.xml">
 
&lt;web-app xmlns="http://caucho.com/ns/resin"
 
        xmlns:resin="urn:java:com.caucho.resin"
 
        xmlns:example="urn:java:example">
 
 
  &lt;example:GreetingAPI>
 
    &lt;resin:HessianClient url="http:${webApp.url}/hessian/greeting"/>
 
  &lt;/example:GreetingAPI>
 
 
  &lt;servlet-mapping url-pattern="/client/greeting"
 
                  servlet-class="example.GreetingClientServlet"/>
 
 
&lt;/web-app>
 
</example>
 
 
 
 
 
      <p>Writing a Hessian service as a plain-old Java object (POJO)
 
eliminates protocol dependencies and simplifies service testing.
 
</p>
 
 
=Files in this tutorial=
 
<table>
 
<tr>
 
  <th>File</th>
 
  <th>Description</th>
 
</tr>
 
<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></table>
 
 
=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>
 
 
====LogService.java" language="java====
 
<pre>
 
package example;
 
 
public interface LogService {
 
  public void log(String message);
 
  public String getLog();
 
}
 
</pre>
 
 
====LogServiceImpl.java" language="java====
 
<pre>
 
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();
 
  }
 
}
 
</pre>
 
 
<p>
 
The configuration for this example is the following:
 
</p>
 
 
====web.xml" language="xml====
 
<pre>
 
<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>
 
</pre>
 
 
<p>
 
Finally, the service is called from a JSP page:
 
</p>
 
 
====demo.jsp====
 
<pre>
 
<%@ 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");
 
%>
 
</pre>
 
 
<p>
 
<a href="demo.jsp">Try the example</a>
 
</p>
 
 
 
 
      <p>Hessian 2.0 provides cross-language binary object serialization
 
with efficiencies better than java.io serialization.  The compaction
 
encodings added to Hessian 2.0 have improved an already-popular
 
cross-platform binary web services protocol.  With these changes, Hessian
 
2.0 now directly competes with java.io serialization in efficiency.</p>
 
 
=Files in this tutorial=
 
<table>
 
<tr>
 
  <th>File</th>
 
  <th>Description</th>
 
</tr>
 
<tr>
 
  <td><viewfile-link file="WEB-INF/classes/example/HessianSerializeServlet.java"/></td>
 
  <td>Serialization Servlet</td>
 
</tr>
 
<tr>
 
  <td><viewfile-link file="WEB-INF/classes/example/Car.java"/></td>
 
  <td>Serialized class</td>
 
</tr>
 
<tr>
 
  <td><viewfile-link file="WEB-INF/classes/example/Color.java"/></td>
 
  <td>Enumeration for the car color</td>
 
</tr>
 
<tr>
 
  <td><viewfile-link file="WEB-INF/classes/example/Model.java"/></td>
 
  <td>Enumeration for the car model</td>
 
</tr>
 
</table>
 
 
=Overview=
 
 
<p>In this simple example, we'll use Hessian 2.0 to serialize
 
three Car objects to a byte array.  The serialized data could be saved in
 
a persistent store, or sent as a message in a SOA or JMS application.
 
Because Hessian 2.0 is cross-language, the message could be deserialized by a
 
.NET or even a PHP application.</p>
 
 
<p>The efficiency of Hessian 2.0 is about twice that of java.io
 
serialization. This is a tiny example, of course, but does show that you can
 
send compact, cross-language messages without having to use bloated
 
XML solutions like SOAP.</p>
 
 
<table>
 
<tr>
 
  <th>Service</th>
 
  <th>Size</th>
 
</tr>
 
<tr>
 
  <td>Hessian 2.0</td>
 
  <td>139 bytes</td>
 
</tr>
 
<tr>
 
  <td>java.io</td>
 
  <td>287 bytes</td>
 
</tr>
 
<tr>
 
  <td>Hessian 2.0 with Deflation</td>
 
  <td>164 bytes</td>
 
</tr>
 
</table>
 
 
 
=Model=
 
 
<p>The example's model is a Car object with three fields: year, model, and
 
color.  The model and color are enumeration types.</p>
 
 
====Car.java====
 
<pre>
 
package example;
 
 
public class Car {
 
  private int year;
 
  private Model model;
 
  private Color color;
 
}
 
</pre>
 
 
====Car.java====
 
<pre>
 
package example;
 
 
public enum Model {
 
  CIVIC,
 
  EDSEL,
 
  MODEL_T,
 
}
 
</pre>
 
 
====Color.java====
 
<pre>
 
package example;
 
 
public enum Model {
 
  BLACK,
 
  GREEN,
 
  BLUE,
 
}
 
</pre>
 
 
 
=Hessian Serialization=
 
 
<p>The Hessian serialization API resembles
 
java.io <code>ObjectOutputStream</code> serialization.  The general steps
 
are to create a <code>Hessian2Output</code>
 
around any <code>OutputStream</code> and write data to the stream.
 
In this example, we've encapsulated the object in a Hessian 2.0 message
 
using <code>startMessage</code> and <code>completeMessage</code> to
 
show how you would create a message for an SOA or JMS application.</p>
 
 
====Serialization====
 
<pre>
 
ByteArrayOutputStream bos = new ByteArrayOutputStream();
 
 
HessianFactory factory = new HessianFactory();
 
 
Hessian2Output out = factory.createHessian2Output(bos);
 
 
out.startMessage();
 
     
 
out.writeInt(2);
 
 
Car car1 = new Car(Model.EDSEL, Color.GREEN, 1954);
 
 
out.writeObject(car1);
 
 
Car car2 = new Car(Model.MODEL_T, Color.BLACK, 1937);
 
 
out.writeObject(car2);
 
 
out.completeMessage();
 
 
out.close();
 
 
byte []data = bos.toByteArray();
 
</pre>
 
 
<p>The deserialization is the same as serialization.
 
Create an <code>Hessian2Input</code> around any <code>InputStream</code>
 
and read data from the stream.</p>
 
 
====Deserialization====
 
<pre>
 
ByteArrayInputStream bin = new ByteArrayInputStream(data);
 
 
HessianFactory factory = new HessianFactory();
 
 
Hessian2Input in = factory.createHessianHessian2Input(bin);
 
 
in.startMessage();
 
 
ArrayList list = new ArrayList();
 
 
int length = in.readInt();
 
 
for (int i = 0; i < length; i++) {
 
  list.add(in.readObject());
 
}
 
 
in.completeMessage();
 
 
in.close();
 
bin.close();
 
</pre>
 
 
 
=Hessian Compression=
 
 
<p>The <a href="http://caucho.com/resin-3.1/doc/hessian-2.0-spec.xtp">Hessian
 
2.0 draft specification</a> has added support for envelopes around Hessian
 
messages.  These envelopes can provide additional capabilities like
 
compression, encryption, and message signatures.  The envelope can also
 
be used to attach routing and reliability information to a message.  Since
 
envelopes are nestable, each envelope can be simple and provide powerful
 
capabilities when combined.  For example, a secure messaging system
 
might compress, encrypt and then securely sign a message.</p>
 
 
<p>The API for using envelopes is <code>wrap()</code> for writing a message
 
and <code>unwrap()</code> for reading a message.  The application
 
serialization code itself is identical, since the envelope just creates a
 
<code>Hessian2Input</code> or <code>Hessian2Output</code> wrapper around
 
the original stream.</p>
 
 
====Deflation====
 
<pre>
 
Deflation envelope = new Deflation();
 
 
ByteArrayOutputStream bos = new ByteArrayOutputStream();
 
 
HessianFactory factory = new HessianFactory();
 
Hessian2Output out = facotyr.createHessian2Output(bos);
 
 
out = out.wrap(out);
 
 
out.startMessage();
 
 
Car car1 = new Car(Model.EDSEL, Color.GREEN, 1954);
 
 
out.writeObject(car1);
 
 
out.completeMessage();
 
 
out.close();
 
 
byte []data = bos.toByteArray();
 
</pre>
 
 
====Inflation====
 
<pre>
 
Deflation envelope = new Deflation();
 
 
ByteArrayInputStream bin = new ByteArrayInputStream(data);
 
 
HessianFactory factory = new HessianFactory();
 
Hessian2Input in = factory.createHessian2Input(bin);
 
 
in = envelope.unwrap(in);
 
 
in.startMessage();
 
 
Object value = in.readObject();
 
 
in.completeMessage();
 
</pre>
 
 
 
 
   
 
   
 
       <p>Writing a Hessian service as a plain-old Java object (POJO)
 
       <p>Writing a Hessian service as a plain-old Java object (POJO)
Line 712: Line 4:
 
</p>
 
</p>
  
<p>The <a href="../hessian-add/">addition example</a> built the Hessian
+
<p>The [../hessian-add/ addition example] built the Hessian
 
service as an extension of HessianService for simplicity.  Most services
 
service as an extension of HessianService for simplicity.  Most services
 
will want to be independent of the Hessian protocol itself.</p>
 
will want to be independent of the Hessian protocol itself.</p>
Line 723: Line 15:
 
</tr>
 
</tr>
 
<tr>
 
<tr>
   <td><viewfile-link file="WEB-INF/classes/example/MathService.java"/></td>
+
   <td><code>WEB-INF/classes/example/MathService.java</code></td>
 
   <td>Interface for the math service.</td>
 
   <td>Interface for the math service.</td>
 
</tr>
 
</tr>
 
<tr>
 
<tr>
   <td><viewfile-link file="WEB-INF/classes/example/MathServiceImpl.java"/></td>
+
   <td><code>WEB-INF/classes/example/MathServiceImpl.java</code></td>
 
   <td>The main service implementation.</td>
 
   <td>The main service implementation.</td>
 
</tr>
 
</tr>
 
<tr>
 
<tr>
   <td><viewfile-link file="WEB-INF/resin-web.xml"/></td>
+
   <td><code>WEB-INF/resin-web.xml</code></td>
 
   <td>Configures the environment</td>
 
   <td>Configures the environment</td>
 
</tr>
 
</tr>
 
<tr>
 
<tr>
   <td><viewfile-link file="demo.jsp"/></td>
+
   <td><code>demo.jsp</code></td>
 
   <td>Client JSP</td>
 
   <td>Client JSP</td>
 
</tr>
 
</tr>
 
<tr>
 
<tr>
   <td><viewfile-link file="demo.php"/></td>
+
   <td><code>demo.php</code></td>
 
   <td>Client PHP</td>
 
   <td>Client PHP</td>
 
</tr>
 
</tr>
Line 765: Line 57:
  
 
<p>The Java interface describes the remote API.  This example has an
 
<p>The Java interface describes the remote API.  This example has an
addition method, <var>add()</var>.</p>
+
addition method, '''''add()'''''.</p>
  
 
<p>Resin's proxy client implementation uses the remote interface to
 
<p>Resin's proxy client implementation uses the remote interface to

Revision as of 00:00, 14 June 2012

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

The [../hessian-add/ addition example] built the Hessian service as an extension of HessianService for simplicity. Most services will want to be independent of the Hessian protocol itself.

Contents

Files in this tutorial

File Description
WEB-INF/classes/example/MathService.java Interface for the math service.
WEB-INF/classes/example/MathServiceImpl.java The main service implementation.
WEB-INF/resin-web.xml Configures the environment
demo.jsp Client JSP
demo.php Client PHP

Service Implementation

The MathService implementation is just a Java class that implements the MatchService API.

Example: MathServiceImpl.java

package example;

public class MathServiceImpl implements MathService {
  public int add(int a, int b)
  {
    return a + b;
  }
}


Remote Interface

The Java interface describes the remote API. This example has an addition method, add().

Resin's proxy client implementation uses the remote interface to expose the API to the proxy stub. Strictly speaking, though, the Java remote interface is not required for Hessian. A non-Java client will not use the Java interface, except possibly as documentation.

Example: MathService.java

package example;

public interface MathService {
  public int add(int a, int b);
}


Service configuration

Example: resin-web.xml

<web-app xmlns="http://caucho.com/ns/resin">

  <servlet-mapping url-pattern="/math/*"
                   servlet-class="example.MathService">
    <protocol uri="hessian:"/>
  </servlet-mapping>

  <remote-client name="math">
    <uri>hessian:url=${webApp.url}/math/</uri>
    <interface>example.MathService</interface>
  </remote-client>

</web-app>


Java Client

The client is identical to the basic example.

Example: demo.jsp

<%@ page import="javax.webbeans.In" %>
<%@ page import="example.MathService" %>
<%!
@In MathService math;
%>
<pre>
3 + 2 = <%= math.add(3, 2) %>
3 - 2 = <%= math.sub(3, 2) %>
3 * 2 = <%= math.mul(3, 2) %>
3 / 2 = <%= math.div(3, 2) %>

</pre>

3 + 2 = 5
3 - 2 = 1
3 * 2 = 6
3 / 2 = 1


PHP Client

The client is identical to the basic example.

Example: demo.php

<?php

$math = java_bean("math");
?>
<pre>
3 + 2 = <?= $math->add(3, 2) ?>
3 - 2 = <?= $math->sub(3, 2) ?>
3 * 2 = <?= $math->mul(3, 2) ?>
3 / 2 = <?= $math->div(3, 2) ?>

</pre>

3 + 2 = 5
3 - 2 = 1
3 * 2 = 6
3 / 2 = 1
Personal tools
TOOLBOX
LANGUAGES