Hessian Communications Case (Java) __hessian

Source: Internet
Author: User
Tags serialization

personal blog: poke me, poke me Preface

As a result of the work, the project needs to do the message conversion between Hessain and XML because of contact with the Hessain. But for Hessian is something confused. So the next time we understand the serialization rules of the Hessain protocol and the way the Hessian protocol communicates. This article was recorded for a long time (about 3 months) after completing the module. This time there is nothing, and then have been planning to record the process of learning, and then shelved. Therefore, this article makes a brief introduction to the Hessain protocol, then constructs a Java version Hessian client and the service end, realizes the Hessian communication. What is Hessian What is RPC?

RPC (remote Procedure call Protocol)-The remoting Protocol, a protocol that requests services over a network from a remote computer program without having to understand the underlying network technology. The RPC protocol assumes the presence of certain transport protocols, such as TCP or UDP, that carry information data between communication programs. In the OSI network communication model, RPC spans both the transport layer and the application layer. RPC makes it easier to develop applications that include network distributed multiple programs. RMI is the standard RPC service in Java EE, the perfect encapsulation of RPC is the business logic can be distributed deployment, the operation of the operations of the flow of streaming (but the request is still synchronized), in the middle of the use of the process of encapsulation implementation can be implemented at the time of the client call completely transparent ( Need to be encapsulated in the RMI client invocation mode). What is Hessian?

Hessian is a lightweight, custom-described binary RPC protocol. Hessian is primarily used as Object oriented message communication. Hessian Serialization Rules

Hessian serialization method currently has two versions 1.0 and 2.0, you can view the official website http://hessian.caucho.com/doc/hessian-serialization.html. In addition, I uploaded in my seven cattle space Hessian Agreement 2.0 Chinese version, there is a need to download, to prevent the official website all English can not understand. In addition, Hessian official online provides a variety of versions of the Hessian protocol implementation, including Java,c++,c#,python,.net,ruby, above actually has a more detailed demo. can refer to, at that time I also referred to. Hessian Communication Column (Java) Environmental Preparedness


Eclipse && Tomcat. Resource Downloads

Download the Java version of the Hessian class library: Hessian-4.0.37.jar Hessian Server

New dynamic Web Project in eclipse

Here you can see that you need to select Target runtime and you need to install Tomcat first.

Import downloaded Hessian-4.0-37.jar, right key build Path->add to build path
Import complete as shown:

Development steps

The development of the Hessian service side involves 3 parts. Defining interfaces && interface implementations &&web.xml defining interfaces

New Ibasic.java, casually named.

Package test;

Public interface Ibasic {public  
String hello ();   
}

Here's a demo that defines just one of the simplest hello functions that can be added to your interface function in this file. interface functions are the services exposed by the service side, can serve the actual content of the customer. Therefore, the interface file also needs to be used in the development of the client. Interface Implementation

New Basicservice.java, casually named.

Package test;

public class Basicservice implements Ibasic {

private String hello= ' Hello, world,my name is nick! ';   

Public String Hello () {return
  hello;
}
}

This file is the concrete implementation of the interface function, which is only intended for the interface functions defined in Ibasic.java. for small white, in simple terms, is the service side provides some services, or expose some services, that is, interface functions, these services for the client remote invocation, just as the client is called locally, but the server with the client's communication with some binary protocol. Web.xml
The server implements these two files and is basically complete, but if you need to publish to Tomcat, you need to configure the servlet in Web.xml.

Xml:

<?xml version= "1.0" encoding= "UTF-8"?> <web-app version= "2.5" xmlns= "Http://java.sun.com/xml/ns/javaee" Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http ://java.sun.com/xml/ns/javaee/web-app_2_5.xsd "> <welcome-file-list> <welcome-file>index.jsp</ welcome-file> </welcome-file-list> <servlet> <servlet-name>servermachine</servlet-nam   
         E> <servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class> <init-param> <param-name>home-class</param-name> <param-value>test. basicservice</param-value> </init-param> <init-param> <param-name>home-a Pi</param-name> <param-value>test. ibasic</param-value> </init-param> </servlet> <servlet-mapping> <servle T-name>servermachine</servlet-name> <url-pattern>/ServerMachine</url-pattern> </servlet-mapping> </w Eb-app>

The need to pay attention to the figure is servlet-name, you can modify your favorite name, as for the specific web.xml inside the meaning of each field, I am not really clear, I do not understand Java, I have been engaged in C + + development. I tried it many times at that time. If you are not able to launch the deployment to Tomcat when you configure it, please revise it again and again.

Note also that you need to have a index.jsp this script, as follows:

INDEX.JSP:

<%@ page language= "java" import= "java.util.*" pageencoding= "iso-8859-1"%> <%
String path = Request.getcontextpath ();
String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/"; %> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" > 

The placement of these two files is also critical, if not, there will be errors. See figure below:

4 released to Tomcat, start hessian_server

This step is premised on the need to create a new server in Eclipse and configure it well. process slightly. After all, now not Java is not too much, I was for this small problem also toss for a long time.

After the success of the following:
Note here that the above url:http://localhost:8080/hessian_server, this address is the Hessian server address, the actual situation will localhost replaced by the server's IP address, this address client needs to use.

The Hessian service is over here. Here I just use the simplest way to set, actually more complex circumstances can use spring and so on, specifically I have not studied.

The next step is the development of the Hessian client. Hessain Client

New Java Project in eclipse

Import downloaded Hessian-4.0-37.jar, right key build Path->add to build path
Import complete as shown:

Development steps

The development of Hessian clients involves 2 parts. Defining Interfaces && Client main program definition interfaces

The interface file defined on the above server is Ibasic.java to the project. Client Main Program

New Hessianclient.java, casually named.

Package test;

Import com.caucho.hessian.client.HessianProxyFactory;

public class hessianclient{public
static void main (string []args)   
        throws Exception   
      {   
        String url = "http ://localhost:8080/hessian_server/servermachinetest ";   
        Hessianproxyfactory factory = new Hessianproxyfactory ();   
        Ibasic basic = (ibasic) factory.create (ibasic.class, url); 

        String Helloreturn = Basic.hello ();
        System.out.println (Helloreturn);


      }    

Note the URL address above.

The main steps define the address URL of the service side, and then a new agent factory Hessainproxyfactory, which is responsible for remote invocation of the proxy class factory. You can then call the interface function defined in Ibasic.java, like the local function, hello ().

4 Starting the Client

Right-click Run As->java application to start the client on the project.
After the success of the results are as follows: summary

At this point, the communication between the Hessian client and the server is completed. Now looking back, it may feel simpler. At that time was still tossing for a good while. In simple terms, the service side defines and implements the service, publishes it to Tomcat, and the client creates a new agent, calling the function provided by the remote server side as called the local function to complete the communication.

So how does the bottom of the Hessian exactly work? How the client and server end up serialization and deserialize it. How the client's proxy class factory is implemented. I'll probably leave it to another blog for analysis. finished writing

Writing this article is back to write, probably in the interval of nearly 4 months time, in order to write this blog, the use of some of the pictures, I have built a server and the client, or encountered a number of small problems, indicating that after a period of time is unfamiliar. The best time to finish the project on Hessain should be to record the process, can save a lot of time.

Persist in writing a blog is very difficult, there is time to write analysis under the Hessian source and C + + version of the Hessain, as well as other projects involved in some things.

Blog:rebootcat.com ( default )

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.