http://wiki4.caucho.com/index.php?title=Hessian_Java_Dependency_Injection_Example&feed=atom&action=historyHessian Java Dependency Injection Example - Revision history2024-03-28T15:53:52ZRevision history for this page on the wikiMediaWiki 1.18.0http://wiki4.caucho.com/index.php?title=Hessian_Java_Dependency_Injection_Example&diff=628&oldid=prevRick: Created page with "=Hessian Addition= <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>Hessia..."2012-06-15T00:00:00Z<p>Created page with "=Hessian Addition= <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>Hessia..."</p>
<p><b>New page</b></p><div>=Hessian Addition=<br />
<p>The addition example creates a Hessian web services<br />
with a servlet and uses that web service from a<br />
JSP client and a Python client.</p><br />
<br />
<p>Hessian is a lightweight binary RPC protocol. Transferring binary objects<br />
like files, images, or mp3s can avoid the protocol overhead<br />
that XML-based protocols require. Since it's simple, its performance<br />
should be usable for almost all sites. Hessian is designed to be<br />
self-describing, eliminating the requirement for external IDLs or WSDL files.<br />
Because it is as small as possible and language-independent,<br />
non-Java Hessian implementations are can easily develop comprehensive<br />
test suites.</p><br />
<br />
<p>This tutorial only requires the open source Java implementation of<br />
the Hessian client and server. It can be downloaded<br />
from [http://caucho.com/hessian/ http://www.caucho.com/hessian/]<br />
for non-Resin clients and servers.</p><br />
<br />
<p>Because Resin's EJB implementation uses Hessian as its primary<br />
remote procedure call protocol, EJB developers can easily expose<br />
services to clients from other languages.</p><br />
<br />
<p>Because EJB clients and servers are written without knowledge of the<br />
underlying protocol, even if you intend to deploy with another protocol,<br />
like RMI/IIOP, you can develop using Resin's Hessian.</p><br />
<br />
<p>The [doc|hessian-1.0-spec.xtp Hessian 1.0 spec]<br />
and [doc|hessian-2.0-spec.xtp Hessian 2.0 spec] describe<br />
the full Hessian protocol.</p><br />
<br />
==Files in this tutorial==<br />
<table><br />
<tr><br />
<th>File</th><br />
<th>Description</th><br />
</tr><br />
<tr><td><code>WEB-INF/classes/example/MathService.java</code><br />
</td><td>Interface for the math service.<br />
</td></tr><tr><td><code>WEB-INF/classes/example/HessianMathService.java</code><br />
</td><td>The main service implementation.<br />
</td></tr><tr><td><code>WEB-INF/web.xml</code><br />
</td><td>Configures the environment<br />
</td></tr><tr><td><code>demo.jsp</code><br />
</td><td>Client JSP<br />
</td></tr></table><br />
<br />
==The Hessian Protocol==<br />
<br />
<p>A Hessian call is just an HTTP POST to a URL. The arguments are<br />
serialized into the Hessian binary format and passed to the server.</p><br />
<br />
<p>Most applications will never need to look at the Hessian<br />
protocol, but it's simple enough that a basic example can help show<br />
what's happening underneath the API.</p><br />
<br />
====Hessian call====<br />
<pre><br />
c x01 x00<br />
m x00 x03 add<br />
I x00 x00 x00 x02<br />
I x00 x00 x00 x03<br />
z<br />
</pre><br />
<br />
====Hessian reply====<br />
<pre><br />
r x01 x00<br />
I x00 x00 x00 x05<br />
z<br />
</pre><br />
<br />
<p>The call does not need to specify the service name because the<br />
service is uniquely specified by the URL.</p><br />
<br />
<p>The following Addition example shows how to create a basic<br />
server so you can test Hessian.</p><br />
<br />
<br />
==A Hessian Example==<br />
<p>Using Hessian generally uses three components:</p><br />
<ol><br />
<li>A remote interface<br />
</li><li>The server implementation<br />
</li><li>The client (JSP or servlet)<br />
</li></ol><br />
<br />
<p>The remote interface is used by the Hessian proxy<br />
factory to create a proxy stub implementing the service's interface.</p><br />
<br />
<br />
==Service Implementation==<br />
<br />
<p>Resin's Hessian provides a simple way of creating a server. Just extend<br />
<code>HessianServlet</code> with your remote methods. The Hessian call will just<br />
be a POST to that servlet. HessianServlet will introspect the<br />
service and expose the methods.</p><br />
<br />
====HessianMathService.java====<br />
<pre><br />
package example;<br />
<br />
import com.caucho.hessian.server.HessianServlet;<br />
<br />
public class HessianMathService extends HessianServlet {<br />
public int add(int a, int b)<br />
{<br />
return a + b;<br />
}<br />
}<br />
</pre><br />
<br />
<br />
==Remote Interface==<br />
<br />
<p>The Java interface describes the remote API. This example has an<br />
addition method, '''''add()'''''.</p><br />
<br />
<p>Resin's proxy client implementation uses the remote interface to<br />
expose the API to the proxy stub. Strictly speaking, though,<br />
the Java remote interface is not required for Hessian. A non-Java client<br />
will not use the Java interface, except possibly as documentation.</p><br />
<br />
====MathService.java====<br />
<pre><br />
package example;<br />
<br />
public interface MathService {<br />
public int add(int a, int b);<br />
}<br />
</pre><br />
<br />
<br />
==Java Client==<br />
<br />
<p>RPC clients follow the following steps in using a remote object:</p><br />
<ol><br />
<li>Determine the URL of the remote object.<br />
</li><li>Obtain a proxy stub from a proxy factory.<br />
</li><li>Call methods on the proxy stub.<br />
</li></ol><br />
<br />
====client.jsp====<br />
<pre><br />
<%@ page import="com.caucho.hessian.client.HessianProxyFactory" %><br />
<%@ page import="example.MathService" %><br />
<%<br />
HessianProxyFactory factory = new HessianProxyFactory();<br />
<br />
// http://localhost:8080/resin-doc/protocols/tutorial/hessian-add/hessian/math<br />
<br />
String url = ("http://" +<br />
request.getServerName() + ":" + request.getServerPort() +<br />
request.getContextPath() + "/hessian/math");<br />
<br />
MathService math = (MathService) factory.create(MathService.class, url);<br />
<br />
out.println("3 + 2 = " + math.add(3, 2));<br />
%><br />
</pre><br />
====results====<br />
<pre><br />
3 + 2 = 5<br />
</pre><br />
<br />
<br />
==Python Client==<br />
<br />
<p>The [http://www.caucho.com/hessian Hessian site] has<br />
a basic Python library for Hessian.</p><br />
<br />
====client.py====<br />
<pre><br />
from hessianlib import Hessian<br />
<br />
site = "http://localhost:8080/resin-doc/protocols/tutorial/hessian-add"<br />
url = site + "/hessian/math"<br />
<br />
proxy = Hessian(url);<br />
<br />
print "3 + 2 =", proxy.add(2, 3)<br />
</pre><br />
====results====<br />
<pre><br />
3 + 2 = 5<br />
</pre></div>Rick