Firstly, what is hessian first?
Hessian:hessian is a lightweight remotingonhttp tool that provides RMI functionality in a simple way, simpler and faster than Webservice,hessian. The use of the binary RPC protocol, because the use of binary protocol, it is very suitable for sending binary data, Hessian mainly for object-oriented message communication. Hessian's original intention is to support dynamic types, compact format, cross-language Hessian is the use of their own serialization mechanism to implement the marshalling and anti-marshalling, its supporting data types are limited, not support complex objects, can penetrate the firewall, I have to say here. Rmi:rmi is a set of APIs for users to develop distributed applications. He uses the Java serialization mechanism to implement calls and to group the return values in an anti-marshalling. It uses the Java language interface to define the remote object, which aggregates the Java serialization and Java Remote Method Protocol (Javaremote Methodprotocol). He can be seen as the Java version of RPC, because traditional RPC does not apply well to distributed object systems. Javarmi, however, supports communication between program-level objects stored in different address spaces, enabling seamless remote calls between remote objects. He also has its shortcomings, he can only through the RMI protocol access can not be accessed through the HTTP protocol, unable to penetrate the firewall.
There is also a remote invocation method that is Httpinvoker: He also groups and deserializes parameters and return values through the Java serialization mechanism, which has the benefits of RMI supporting all serializable objects. The use of the HTTP protocol to transmit binary streams, while having Hessian, Burlap (transfer XML text) advantages.
Second, write a simple Hessian communication need to know what to write?
Hessian: Write a question that Hessian need to be aware of:
1. The Java server side must have the following points:
Jar Package with Hessian
Design an interface that is used to call the client
Realize the kinetic energy of this interface
Configure Web. XML to configure the appropriate servlet
Object must implement Serializable interface
For complex objects, you can use map methods to pass
2, the client must have the following points:
Java Client contains Hessian.jar package
Has the same interface and entity classes as the server-side structure. including namespaces are best. Invoking the remote interface with Hessianproxyfactory
Three, simple Hessian example:
1, the interface on the server side:
Public Interface Ihello { String SayHello (); }
2, on the service side of the implementation class:
Public class Ihelloimpl extends Hessianservlet implements Ihello { @Override public String SayHello () { // TODO auto-generated method stub return" hello,i from Hessianservice " ; }}
3. In the client class:
Public classClienttest { Public StaticString URL ="Http://127.0.0.1:8080/HessianService/Hello"; Public Static voidMain (string[] args) {Hessianproxyfactory factory=Newhessianproxyfactory (); Try{Ihello Ihello= (Ihello) factory.create (Ihello.class, URL); System. out. println (Ihello.sayhello ()); } Catch(malformedurlexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } }}
3, first the server-side class link to the client, or server-side packaging to the client,
4. Configure in Web. xml:
Server-side:
<servlet> <servlet-name>Hello</servlet-name> <servlet-class> com.caucho.hessian.server.hessianservlet</servlet-class> <init-param> < param-name>home-class</param-name> <param-value> com.kcpt.hessian.service.ihelloimpl</param-value> </init-param> <init-param> <param-name>home-api</param-name> <param-value>com.kcpt.hessian.service.ihello</ param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Hello</servlet-name> <url-pattern>/Hello</url-pattern> </servlet-mapping>
Execution success: Output hello,i from Hessianservice, which is a simple Hessian implementation, looks relatively simple
Four, Hessian and spring combine.
In the actual application, we do not just use Hessian to communicate, if the method is more than the words, it is better to write directly on the client to call, however: when Hessian and spring combined, greatly reduce these operations, the DAO layer operations are all placed on the Hessian server, Put all the business logic in the Hessian client, so that our Hessian client and server completely separate, so our business logic and the DAO layer is really the separation, can be placed on different servers, of course, Hessian communication is not only the role of these.
The interface and implementation are the same as the top: it's just a bit cumbersome to configure in Web. xml:
Example:
1. Server side: Add remoting-servlet.xml config file: Used to configure bean and export bean as Hessian service:
<?xml Version ="1.0"encoding ="UTF-8"? ><beans xmlns ="Http://www.springframework.org/schema/beans"Xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"XMLNS:AOP="HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"Xmlns:tx="Http://www.springframework.org/schema/tx"xsi:schemalocation="Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsdhttp//WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOPhttp://www.springframework.org/schema/aop/spring-aop-2.0.xsdhttp//Www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.0.xsd"><!--defining common bean instances--<bean id="Hello" class="Com.kcpt.hessian.service.IHelloImpl"/> <!--use hessianserviceexporter to export normal beans to Hessian service--<bean name="/remoting" class="Org.springframework.remoting.caucho.HessianServiceExporter"> <!--the target to be exported bean--> <property name="Service" ref="Hello"/> <!--Hessian Service interface--<property name="Serviceinterface"Value="Com.kcpt.hessian.service.IHello"/> </bean> </beans>
2. Web. xml file Configuration:
The first is the listener: Spring's listener
<listener><listener-class>org.springframework.web.context.contextloaderlistener</listener-class> <!--Add listener--></listener><!--Specify where the spring configuration file is, and the Hessian service is exported in this configuration file--><context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/remoting-servlet.xml< /param-value> </context-param><!--Hessian provides remote services through a servlet and needs to map a matching pattern to the Hessian service. Spring's Dispatcherservlet can do this, Dispatcherservlet can forward requests for matching patterns to the Hessian service, and Web. XML simply defines the "request Forwarder" that will match the/remoting/*the request is intercepted and forwarded to the context bean for processing. And Hessianserviceexporter provides the bean service. --><servlet> <servlet-name>remoting</servlet-name> <servlet-class> Org.springframework.web.servlet.dispatcherservlet</servlet-class> <load-on-startup>1</load-on-st Artup> </servlet><servlet-mapping><servlet-name>remoting</servlet-name>< Url-pattern>/remoting/*</url-pattern></servlet-mapping>
3, on the client:
Also add spring listeners and Context-param to specify the bean's file
Declare the bean's XML file:
<?xml Version ="1.0"encoding ="UTF-8"? ><beans xmlns ="Http://www.springframework.org/schema/beans"Xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"XMLNS:AOP="HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"Xmlns:tx="Http://www.springframework.org/schema/tx"xsi:schemalocation="Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsdhttp//WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOPhttp://www.springframework.org/schema/aop/spring-aop-2.0.xsdhttp//Www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.0.xsd"><bean id="myserviceclient" class="Org.springframework.remoting.caucho.HessianProxyFactoryBean"><property name="serviceurl">//name of Hessian address and name request forwarding<value>http://127.0.0.1:8080/hessianservice/remoting</value></property> <property name="Serviceinterface">//the interface to be called by the Hessian<value>com.kcpt.hessian.service.IHello</value></property></bean></beans>
4, the client program to write:
New Classpathxmlapplicationcontext ("com/kcpt/hessian/client/remoting-client.xml"); // This is just the path to the XML file where you declared the bean. Ihello B = (Ihello) context.getbean ("myserviceclient");
To get to the Ihello interface, so that it can invoke the method in this interface to operate
Java Learning Hessian Communication Basics