Java Learning Hessian Communication Basics

Source: Internet
Author: User

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 {

Stringsayhello ();

}
2, on the service side of the implementation class:
public class Ihelloimpl extends Hessianservlet implements ihello{

@Override
Publicstring SayHello () {
todoauto-generated Method Stub
Return "hello,i from Hessianservice";
}

}
3. In the client class:
public class Clienttest {

publicstatic String url = "Http://127.0.0.1:8080/HessianService/Hello";
publicstatic void Main (string[] args) {
Hessianproxyfactory factory = new Hessianproxyfactory ();
try {
Ihelloihello = (Ihello) factory.create (ihello.class, URL);
System.out.println (Ihello.sayhello ());
} catch (Malformedurlexception e) {
Todoauto-generated Catch block
E.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/beanshttp://www.springframework.org/schema/beans/ Spring-beans-2.0.xsd
Http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.0.xsd
Http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.0.xsd ">

<!--define normal bean instances--
<bean id= "Hello" class= "Com.kcpt.hessian.service.IHelloImpl"/>
<!--use Hessianserviceexporter to export ordinary beans to hessian service--
<bean name= "/remoting" class= "Org.springframework.remoting.caucho.HessianServiceExporter" >
<!--the target bean--> to be exported
<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><!--Adding listeners -
</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, and spring's dispatcherservlet can do this. Dispatcherservlet can forward requests for matching patterns to the Hessian service, and Web. XML simply defines a "request Forwarder" that will intercept the request that matches the/remoting/*, and forward the bean processing to the context. 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-startup>
</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/beanshttp://www.springframework.org/schema/beans/ Spring-beans-2.0.xsd
Http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.0.xsd
Http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.0.xsd ">
<bean id= "myserviceclient" class= "Org.springframework.remoting.caucho.HessianProxyFactoryBean" >
<propertyname= "serviceurl" >//hessian name of the address and name of the request forwarding
<value>http://127.0.0.1:8080/HessianService/remoting</value>
</property>
<propertyname= "Serviceinterface" >//hessian the interface to invoke
<value>com.kcpt.hessian.service.IHello</value>
</property>
</bean>
</beans>
4, the client program to write:
ApplicationContext context = Newclasspathxmlapplicationcontext ("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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.