⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hessian-overview.xtp

📁 RESIN 3.2 最新源码
💻 XTP
字号:
<document>  <header>    <product>resin</product>    <title>Hessian</title>    <description>      <p>Hessian is a simple binary protocol for connecting web      services.  The com.caucho.hessian.client and com.caucho.hessian.server      packages do not require any other Resin classes, so can be used in      smaller clients, like applets.</p>      <p>Because Hessian is a small protocol, J2ME devices like cell-phones can use      it to connect to Resin servers.  Because it's powerful, it can be used      for EJB services.</p>      <p>The <a href="hessian-1.0-spec.xtp">Hessian specification</a>itself is a short and interesting description.</p>    </description>  </header>  <body>    <localtoc/><s1 title="Hessian Client"><p>Using a Hessian service from a Java client is like calling a method.The HessianProxyFactory creates proxies which act like normal Javaobjects, with possibility that the method might throw a protocol exceptionif the remote connection fails.  Using HessianProxyFactory requiresJDK&#160;1.3.</p><p>Each service will have a normal Java interface describing theservice.  The trivial hello, world example just returns a string.Because the Hessian services support Java serialization, any Java typecan be used.</p><example title="API for Basic service">package example;public interface Basic {  public String hello();}</example><p>The following is an example of a standalone Hessian client.  Theclient creates a HessianProxyFactory.  The client uses the factory tocreate client stubs with the given target URL and a Java interface forthe API.  The returned object is a stub implementing the API.</p><example title="Hessian Client for Basic service">package example;import com.caucho.hessian.client.HessianProxyFactory;public class BasicClient {  public static void main(String []args)    throws Exception  {    String url = "http://www.caucho.com/hessian/test/basic";    HessianProxyFactory factory = new HessianProxyFactory();    Basic basic = (Basic) factory.create(Basic.class, url);    System.out.println("Hello: " + basic.hello());  }}</example><p>That's it!  There are no more complications to using the client.  Theservice can add methods and use any Java type for parameters and results.</p></s1><s1 title="Hessian Service"><p>While most Hessian services will use Resin-CMP or Resin-EJB, to takeadvantage of the benefits of EJB, the Hessian library makes it possibleto write services by extending HessianServlet.</p><p>Any public method is treated as a service method.  So adding newmethods is as easy as writing a normal Java class.</p><p>Because the service is implemented as a Servlet, it can use allthe familiar servlet data in the ServletContext, just like anormal servlet.</p><example title="Hello Service">package example;public class BasicService implements Basic {  private String _greeting = "Hello, world";  public void setGreeting(String greeting)  {    _greeting = greeting;  }  public String hello()  {    return _greeting;  }}</example><s2 title="Configuration with Dependency Injection in Resin 3.0"><example title="resin-web.xml">&lt;web-app xmlns="http://caucho.com/ns/resin"&gt;  &lt;servlet servlet-name="hello"           servlet-class="com.caucho.hessian.server.HessianServlet"&gt;    &lt;init&gt;      &lt;home resin:type="example.BasicService"&gt;        &lt;greeting&gt;Hello, world&lt;/greeting&gt;      &lt;/home&gt;      &lt;home-api&gt;example.Basic&lt;/home-api&gt;    &lt;/init&gt;  &lt;/servlet&gt;  &lt;servlet-mapping url-pattern="/hello"                   servlet-name="hello"/&gt;&lt;/web-app&gt;</example></s2><s2 title="Configuration for standard web.xml"><p>Since the HessianServlet is a standard servlet, it can also beconfigured in the standard servlet configuration.</p><example title="web.xml">&lt;web-app&gt;  &lt;servlet&gt;   &lt;servlet-name&gt;hello&lt;/servlet-name&gt;   &lt;servlet-class&gt;com.caucho.hessian.server.HessianServlet&lt;/servlet-class&gt;    &lt;init-param&gt;      &lt;param-name&gt;home-class&lt;/param-name&gt;      &lt;param-value&gt;example.BasicService&lt;/param-value&gt;    &lt;/init-param&gt;    &lt;init-param&gt;      &lt;param-name&gt;home-api&lt;/param-name&gt;      &lt;param-value&gt;example.Basic&lt;/param-value&gt;    &lt;/init-param&gt;  &lt;/servlet&gt;  &lt;servlet-mapping&gt;    &lt;url-pattern&gt;/hello&lt;/url-pattern&gt;    &lt;servlet-name&gt;hello&lt;/servlet-name&gt;  &lt;/servlet-mapping&gt;&lt;/web-app&gt;</example></s2></s1><s1 title="Hessian Serialization"><p>The Hessian classes can be used for serialization and deserialization.Hessian's serialization forms the basis for the protocol and takingcontrol of the serialization lets application use Hessian more efficientlythan the proxy interface for specialized application protocols.</p><example title="Serialization">Object obj = ...;OutputStream os = new FileOutputStream("test.xml");Hessian2Output out = new Hessian2Output(os);out.writeObject(obj);os.close();</example><example title="Deserialization">InputStream is = new FileInputStream("test.xml");Hessian2Input in = new Hessian2Input(is);Object obj = in.readObject(null);is.close();</example><p>When serializing Java objects more complex than primitivesor Strings, make sure the classes for those objects implement java.io.Serializable.</p></s1><s1 title="Hessian with large binary data"><p>When a distributed application needs to send large amounts ofbinary data, it can be more efficient touse <code>InputStream</code> to avoid allocating large byte arrays.Only the final argument of the method may be an <code>InputStream</code>,since the data is read during invocation.  For example, a filedownloading service could be implemented efficiently using Hessian.</p><p>In this example, the client needs to take control of theHessian protocol directly, because the proxy interface would requirebuffering the entire file before the call returns.</p><example title="file upload API">package example;public interface Upload {  public void upload(String filename, InputStream data);}</example><p>If the result is an InputStream, it is very important that the<code>InputStream.close()</code> be put in an <code>finally</code>block, because Hessian will not close the underlying HTTP stream untilall the data is read and the input stream is closed.</p><example title="file download API">package example;public interface Download {  public InputStream download(String filename, InputStream data);}</example><example title="Download Java Code">InputStream is = fileProxy.download("test.xml");try {  ... // read data here} finally {  is.close();}</example></s1><s1 title="Hessian Client for a cell-phone"><p>Hessian can be used for even small Java devices.  The following classesfrom com.caucho.hessian.client can be extracted into a J2ME jar:</p><ul><li>MicroHessianInput</li><li>MicroHessianOutput</li><li>HessianRemote</li><li>HessianServiceException</li><li>HessianProtocolException</li></ul><p>The following example shows the code for using a cell phone as aclient.  It's a bit more complicated than using the proxy, since theclient is responsible for creating the connection and writing thedata.</p><example title="Hello, world">import javax.microedition.io.Connector;import javax.microedition.io.HttpConnection;...MicroHessianInput in = new MicroHessianInput();String url = "http://www.caucho.com/hessian/test/basic";HttpConnection c = (HttpConnection) Connector.open(url);c.setRequestMethod(HttpConnection.POST);OutputStream os = c.openOutputStream();MicroHessianOutput out = new MicroHessianOutput(os);out.call("hello", null);os.flush();is = c.openInputStream();MicroHessianInput in = new MicroHessianInput(is);Object value = in.readReply(null);</example></s1>  </body></document>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -