Herssian, lightweight Java Remoting solution

zhaozj2021-02-08  518

I encountered a problem in the project: Due to security needs, a program must be specifically responsible for querying a database, and then passing the query results to the security area by secure channel (eg HTTP). In order to solve this small Remoting problem, we almost use EJB. But fortunately, I have recommended this Lightweight Remoting On HTTP tool.

Hessian is actually very similar to Web Service, but its agreement is not SOAP, but it is its own binary protocol. The HESSIAN's Server end provides a servlet class. After getting a service interface (which is STUB), the STUB passes the method to the server after passing the method, and Server calls the service method with the Reflection.

Simple, lightweight. HesSian is enough to deal with ordinary transing problems. We can forget the EJB and RMI temporarily.

------------

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.

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.

The Hessian Home Page Contains The Latest Information About Hessian Including The Hessian Specification.

Hessian Client Hessian Service Hessian Client for a Cell-Phone Hessian Serialization

Hessian ClientUsing a Hessian service from a Java client is like calling a method. The HessianProxyFactory creates proxies which act like normal Java objects, with possibility that the method might throw a protocol exception if the remote connection fails. Using HessianProxyFactory requires JDK 1.3.

Each Service Will Have A Normal Java Interface Describing The Service. The Trivial Hello, World Example Just Returns A String. Because The Hessian Services Support Java Serialization, Any Java Type Can Be..

API for Basic Service

Package hessian.test;

Public interface Basic {

Public string hello ();

}

The following is an example of a standalone Hessian client. The client creates a HessianProxyFactory. The client uses the factory to create client stubs with the given target URL and a Java interface for the API. The returned object is a stub implementing the API.Hessian Client for Basic Service

Package hessian.test;

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";

HerssianProxyFactory Factory = New HessianProxyFactory ();

Basic Basic = (Basic) Factory.create (Basic.class, URL);

System.out.println ("Hello:" Basic.Hello ());

}

}

That's it! There...................

Hessian ServiceWhile Most Hessian Services Will Use Resin-Cmp or Resin-EJB, To Take Advantage of The Benefits of EJB, The Hessian Library Makes It Possible To Write Services by Extending HessianServlet.

Any Public Method Is Treated as a service method.

Because The Service Is Implement As a Servlet, IT CAN Use All The Familiar Servlet Data In The ServletContext, Just Like a Normal Servlet.

Hello Service

Package hessian.test;

Import com.caucho.hessian.server.hessianservlet;

Public Class BasicService Extends HessianServlet Implements Basic {

Public string hello ()

{

Return "Hello, WORLD";

}

}

Hessian Client for a Cell-Phonessian Can Be Used for Even Small Java Devices. The following class from com.caucho.hersian.client Can Be extracted Into a J2ME JAR:

MicroHessianInput MicroHessianOutput HessianRemote HessianServiceException HessianProtocolException The following example shows the code for using a cell phone as a client. It's a bit more complicated than using the proxy, since the client is responsible for creating the connection and writing the data.Hello, world

Import javax.microedition.io.connector;

Import javax.microedition.io.httpConnection;

...

Microhesianinput in = New MicrohesianInput ();

String Url = "http://www.caucho.com/hessian/test/basic";

HTTPCONNECTION C = (httpConnection) Connector.open (URL);

C.SetRequestMethod (httpConnection.post);

OutputStream Os = C.OpenOutputStream ();

Microhesianoutput out = new microhessianoutput (OS);

Out.call ("Hello", NULL);

Os.flush ();

IS = C.OpenInputStream ();

Microhesianinput in = New MicrohesianInput (IS);

Object value = in.readreply (null);

Herssian SerializationThe Hessian classes can be used for serialization and deserialization.

Serialization

Object obj = ...

OutputStream OS = New FileoutputStream ("Test.xml");

Herssianoutput out = new Hessianoutput (OS);

Out.writeObject (OBJ);

Os.Close ();

DeSerialization

InputStream IS = New FileInputStream ("Test.xml");

Herssianinput in = New HessianInput (IS);

Object obj = in.readObject (null);

Is.close ();

转载请注明原文地址:https://www.9cbs.com/read-729.html

New Post(0)