Hessian Study Summary (i)--hessian introduction
First,
the fundamentals of remote communication Protocols
Network communication needs to do is to transfer the stream from one computer to another computer, based on the transport Protocol and network IO to achieve, where the transport protocol is more famous HTTP, TCP, UDP and so on, HTTP, TCP, UDP are based on the concept of Socket for a certain type of application Exhibited in the Transmission Protocol, network IO, mainly bio, NIO, Aio three ways, all distributed application communications are based on this principle, only for the ease of use of the application, various languages will often provide some more application-friendly application layer protocol.
Second,
application-level protocols
Binary-rpc
binary-RPC (remote Procedure call Protocol, remoting Protocol) is a and RMI (remote method invocation) Similar to the remote invocation of the protocol, it differs from RMI in that it defines the requested information in the standard binary format (the requested object, method, parameter, etc.), and what is the benefit of this is that it can also be used when communicating across languages.
A remote communication process for the BINARY-RPC protocol:
1, the client initiates the request, according to the BINARY-RPC protocol to fill the request information;
2. After filling, the binary format file is converted to stream and transmitted through the transmission protocol.
3, received after receiving the stream into a binary format file, according to the BINARY-RPC protocol to obtain the requested information and processing;
4. After processing, the results are written in a binary format file and returned in accordance with the BINARY-RPC protocol.
Summary of issues:
1, the standard format of transmission is : the standard format of binary files.
2. How do I convert a request to a streamed stream? Converts binary format files to streams.
3, how to receive and process the flow? Gets the requested stream through the listening port, translates into a binary file, obtains the requested information according to the protocol, processes it, and returns the result to the XML.
4. What is the transmission protocol? http
Iii.Introduction of Hessian
Hessian is a lightweight remoting on HTTP tool that uses binary RPC protocol, so it is ideal for sending binary data while having firewall penetration capability. Hessian is generally provided through web applications, so it is very similar to the WebService we use at ordinary times. It's just that it doesn't use the SOAP protocol, but it's simpler and faster than webservice.
Hessian official website: http://hessian.caucho.com/
Hessian can provide remote services through a servlet, and a request that matches a pattern needs to be mapped to the Hessian service. It is also possible to integrate the spring framework, which can be accomplished through its dispatcherservlet, and Dispatcherservlet can forward requests for matching patterns to the Hessian service. The server side of Hessian provides a servlet base class to handle sent requests, while Hessian's remote procedure call is implemented entirely using dynamic proxies, and it is recommended to use interface-oriented programming, Hessian services exposed through interfaces.
Hessian processing: Client-to-serialization writes to output stream--Remote method (server side)--serialization writes to output stream--client reads input stream--output result
Four,Hessian introductory Example 4.1 DownloadsHessian
Go to Hessian official website: http://hessian.caucho.com/Download the latest Hessian package as shown in:
After the download is complete, get the jar file as shown
4.2, build Hessian test Server Web project
Create a new Web project named Hessianserver. Put Hessian-4.0.37.jar into the Web-inf/lib folder as shown in:
When developing a project based on Hessian, the following points should be noted:
The Java server side must have the following points:
- The jar package containing the Hessian.
- Design an interface that is used to call the client.
- Implements the functionality of the interface.
- Configure Web. XML to match the appropriate servlet.
- The object must implement the Serializable interface.
- A method for complex pairs of images can be passed using a map.
The client must have the following points:
- The Java client contains Hessian.jar packages.
- Has the same interface as the server-side structure.
- Use Hessianproxyfactory to invoke the remote interface.
4.3. Design a service interface to call the client
The code for the IService interface is as follows:
1 package gacl.hessian.service; 2 3 import gacl.hessian.model.User; 4 5/** 6 * <p>ClassName:IService<p> 7 * <p> Description: Define interfaces for external Services <p> 8 * @author xudp 9 * @version 1.0 V10 * @createTime 2014-8-7 PM 05:29:2311
*/12 public interface IService { /**15 * methodname:getuser16 * Description: Get user * @author XUDP * @return User Object */20 public user GetUser (); 21}
A user class is used in the IService interface
The code for the user class is as follows:
1 package Gacl.hessian.model; 2 3 import java.io.Serializable; 4 5/** 6 * <p>ClassName:User<p> 7 * <p>description : Create a user class that implements the serialization interface, 8 * User can serialize after implementing the serialized interface, 9 * Then the serialized user can be transferred to another computer over the network <p>10 * @author XUDP11 * @version 1.0 V12 * @createTime 2014-8-7 pm 05:25:0913 */14 public class User implements serializable{ private static final long Serialversionuid = 1692800630322115854l;17 //name Property name;19 Public User () {}23 ( String name) { this.name = name;26 } The public string GetName () { name;30}31, public void SetName (string name) { THIS.name = name;34 }35 37}
4.4, write IService
the specific implementation class of the interface
The code for the Serviceimpl implementation class is as follows:
1 package Gacl.hessian.service.impl; 2 3 import gacl.hessian.model.User; 4 import gacl.hessian.service.IService; 5 6/** 7 * <p>classname: Serviceimpl<p> 8 * <p>description:iservice Interface specific implementation class <p> 9 * @author xudp10 * @version 1.0 V11 * @createTime 2014-8-7 pm 05:32:1512 */13 public class Serviceimpl implements IService {+ /* (Non-java DOC) * @MethodName getUser17 * @Description GetUser method for implementing the IService interface * @author xudp19 * @return Returns a User object * @see gacl.hessian.service.iservice#getuser () */22 public User GetUser () { return new User ("Aloof pale Wolf"); }25}
4.5. Configure Web. XML to add configuration to Hessianservlet
As shown in the red section below:
1 <?xml version= "1.0" encoding= "UTF-8"?> 2 <web-app version= "2.5" xmlns= "Http://java.sun.com/xml/ns/javaee" 3 Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee 5 Http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd "> 6 <welcome-file-list> 7 <welcome-file>ind Ex.jsp</welcome-file> 8 </welcome-file-list> 9<servlet>11 <!--Configure Hessianservlet,servlet's name to be configured randomly, for example serviceservlet-->12 <se Rvlet-name>serviceservlet</servlet-name>13 <servlet-class> COM.CAUCHO.HESSIAN.SERVER.HESSIANSERVLET</SERVLET-CLASS>14 <!--Configuration Interface specific implementation class-->16 &L T;init-param>17 <param-name>service-class</param-name>18 <param-value>gacl.he ssian.service.impl.serviceimpl</param-value>19 </init-param>20 </servlet>21 <!--map H Essianservlet's access URL address-->22 <servlet-mapping>23 <servlet-name>serviceservlet</servlet-name> ; <url-pattern>/serviceservlet</url-pattern>25 </servlet-mapping></web-app>
By this, Hessian's service-side writing is done.
After the configuration is complete, start the project, enter in the Address bar: http://localhost: Port number/project name /serviceservlet, if the display results are as follows, the configuration and interface are written correctly:
Hessian Requires POST
4.6, write the test client of Hessian
Create a new Hessiantestclient Java project as a test client, you need to introduce the above Hessian-4.0.37.jar, but also the server to export IService and user two class jar, put into lib. Or create a two-touch class, here is the process of packaging the server's IService and user two classes into a jar package provided to the client for invocation, as shown in the jar package:
Select the classes you want to package
This packages the IService class and the user class into a Hessian_common.jar package, as shown in:
The Hessiantestclient project is created as follows:
The Hessiantestclient project introduces the Hessian-4.0.37.jar and IService and user two-class jars, writes the Hessianclient client, and the code for the Hessianclient class is as follows:
1 package hessian.test.client; 2 3 Import java.net.MalformedURLException; 4 5 Import Gacl.hessian.model.User; 6 Import Gacl.hessian.service.IService; 7 8 Import Com.caucho.hessian.client.HessianProxyFactory; 9/**11 * <p>classname:hessianclient<p>12 * <p>description: Call Hessian client <p>13 * @author Xu DP14 * @version 1.0 V15 * @createTime 2014-8-7 PM 07:05:4216 */17 public class Hessianclient {$ public static VO ID Main (string[] args) throws malformedurlexception {*/* <servlet>22 <!- -Configure the name of the Hessianservlet,servlet to be arbitrarily configured, such as configured here as Serviceservlet-->23 <servlet-name>serviceservlet</se Rvlet-name>24 <servlet-class>com.caucho.hessian.server.hessianservlet</servlet-class>25 <!--configuration Interface specific implementation class-->27 <init-param>28 <p Aram-name>service-class</param-name>29 <param-value>gacl.hessian.service.impl.serviceimpl</param-value>30 </init-param >31 </servlet>32 <!--mapping hessianservlet access URL address-->33 <servlet-mappin G>34 <servlet-name>serviceservlet</servlet-name>35 <url-pattern>/serv iceservlet</url-pattern>36 </servlet-mapping>37 */38//Hessian configured in the server-side Web. xml file The Access URL address of the servlet map is a String url = "Http://192.168.1.144:8080/HessianServer/ServiceServlet"; hessianproxyf Actory factory = new Hessianproxyfactory (); IService service = (iservice) factory.create (iservice.class, URL);// Create an instance object of the IService interface for user user = Service.getuser ();//Call the GetUser method in Hessian server-side Serviceimpl class to get a user object Tem.out.println (User.getname ()); 44}45}
Deploy the Hessianserver project to the Tomcat server, start the Tomcat server, and then run the Hessianclient client with the following results:
As you can see from the running results, the user object that was transferred to the remote Hessianserver server was successfully obtained in the Hessianclient client, and then the name of the user object was printed out.
The above is an entry-level case for Hessian!
4.7. Test Project Download
Hessian server: Hessianserver,hessian Test client: hessiantestclient
Hessian Study Summary (i)--hessian introduction (from Network sharing)