hessian 通信,C#客户端调用java服务端服务

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hessian 通信,C#客户端调用java服务端服务相关的知识,希望对你有一定的参考价值。

  Hessian是一个轻量级的remoting onhttp工具,使用简单的方法提供了RMI的功能。 相比WebService,Hessian更简单、快捷。采用的是二进制RPC协议,因为采用的是二进制协议,所以它很适合于发送二进制数据(来自百度)

 

  本次通过hessian简单实现C#调用java服务,并返回java方法的结果,其中C#以及java方法的参数可以是Entity。

 

  1. 依赖 java包及DLL

    1.1 hessian-4.0.37.jar

    1.2 Hessiancsharp.dll

  2. java端: Entity,接口,实现类,配置servlet

    2.1 Entity

    

技术分享
  1 package com.mqq.core.hessian.entity;
  2 
  3 import java.io.Serializable;
  4 
  5 public class RequestEntity implements Serializable{
  6     
  7     private static final long serialVersionUID = -4149734030168349111L;
  8     
  9     private String sessionID;
 10     private String userID;
 11     private String userName;
 12     private String orgID;
 13     private String orgName;
 14     
 15     private String requestService; // 请求的服务名 Bean名
 16     private String requestMethod;  // 请求的方法名
 17     private String[] requestData; // JSON 格式字符串(方法参数值 一组或者多组数据)   每个字符串对应JSONArray(一组数据)
 18     private String requestParamType;       // JSON 格式字符串(方法参数类型)可多个
 19     private String requestParamValue;      // JSON 格式字符串(方法参数值)可多个
 20     
 21     private String returnValue;//返回值
 22     
 23     public String getSessionID() {
 24         return sessionID;
 25     }
 26 
 27     public void setSessionID(String sessionID) {
 28         this.sessionID = sessionID;
 29     }
 30 
 31     public String getUserID() {
 32         return userID;
 33     }
 34 
 35     public void setUserID(String userID) {
 36         this.userID = userID;
 37     }
 38 
 39     public String getUserName() {
 40         return userName;
 41     }
 42 
 43     public void setUserName(String userName) {
 44         this.userName = userName;
 45     }
 46 
 47     public String getOrgID() {
 48         return orgID;
 49     }
 50 
 51     public void setOrgID(String orgID) {
 52         this.orgID = orgID;
 53     }
 54 
 55     public String getOrgName() {
 56         return orgName;
 57     }
 58 
 59     public void setOrgName(String orgName) {
 60         this.orgName = orgName;
 61     }
 62 
 63     public String getRequestService() {
 64         return requestService;
 65     }
 66 
 67     public void setRequestService(String requestService) {
 68         this.requestService = requestService;
 69     }
 70 
 71     public String getRequestMethod() {
 72         return requestMethod;
 73     }
 74 
 75     public void setRequestMethod(String requestMethod) {
 76         this.requestMethod = requestMethod;
 77     }
 78 
 79     public String getRequestParamType() {
 80         return requestParamType;
 81     }
 82 
 83     public void setRequestParamType(String requestParamType) {
 84         this.requestParamType = requestParamType;
 85     }
 86 
 87     public String getRequestParamValue() {
 88         return requestParamValue;
 89     }
 90 
 91     public void setRequestParamValue(String requestParamValue) {
 92         this.requestParamValue = requestParamValue;
 93     }
 94 
 95     public String getReturnValue() {
 96         return returnValue;
 97     }
 98 
 99     public void setReturnValue(String returnValue) {
100         this.returnValue = returnValue;
101     }
102     
103     public String[] getRequestData() {
104         return requestData;
105     }
106 
107     public void setRequestData(String[] requestData) {
108         this.requestData = requestData;
109     }
110 }
Entity

    2.2 接口

      

 1 package com.mqq.core.hessian;
 2 
 3 import com.mqq.core.hessian.entity.RequestEntity;
 4 
 5 public interface IHessianService {
 6     
 7     public RequestEntity clientRequest(RequestEntity request);
 8     
 9     public String hessionRequest1(String requestJson);
10     
11     public String hessionRequest2(RequestEntity request);
12     
13     public RequestEntity hessionRequest3(String param);
14 }
View Code

    2.3 实现类

技术分享
 1 package com.mqq.core.hessian;
 2 
 3 import java.text.SimpleDateFormat;
 4 import java.util.Date;
 5 import java.util.Iterator;
 6 
 7 import net.sf.json.JSONObject;
 8 
 9 import com.mqq.core.hessian.entity.RequestEntity;
10 
11 public class HessianService implements IHessianService{
12 
13     public RequestEntity clientRequest(RequestEntity request){
14         
15         String service = request.getRequestService();
16         String method = request.getRequestMethod();
17         String[] arrayData= request.getRequestData();
18         String paramValue = request.getRequestParamValue();
19         
20         System.out.println(" 已收到请求 ");
21         System.out.println(" 服务名 " + service);
22         System.out.println(" 方法名 " + method);
23         
24         System.out.println("-------------");
25         System.out.println(" 数据 ");
26         System.out.println("-------------");
27         if(arrayData != null && arrayData.length>0){
28             for(int i = 0; i<arrayData.length; i++){
29                 System.out.println(arrayData[i]);
30             }
31         }
32         System.out.println("-------------");
33         System.out.println(" 参数 ");
34         System.out.println("-------------");
35         JSONObject joParam = JSONObject.fromObject(paramValue);
36         Iterator<String> itr = (Iterator<String>)joParam.keys();
37         while(itr.hasNext()){
38             System.out.println(joParam.get(itr.next()));
39         }
40         System.out.println("-------------");
41         
42         SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
43         
44         request.setReturnValue(dateFormat.format(new Date()).toString());
45         request.setSessionID("sesionid394340");
46         
47         return request;
48     }
49     
50     public String hessionRequest1(String requestJson){
51         
52         return "hello "+ requestJson;
53     }
54     
55     public String hessionRequest2(RequestEntity request){
56         
57         return request.getRequestParamValue();
58     }
59     
60     public RequestEntity hessionRequest3(String param){
61         RequestEntity request = new RequestEntity();
62         request.setRequestParamValue(param);
63         String[] data = new String[]{"3433","38439udjfiej","深圳"};
64         request.setRequestData(data);
65         return request;
66     }
67 }
View Code

    2.4 web.xml  配置servlet

技术分享
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  <display-name>WebDemo</display-name>
  
  
  <servlet>
      <servlet-name>hessian</servlet-name>
      <servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class>
      <init-param>
          <param-name>service-class</param-name>
          <param-value> com.mqq.core.hessian.HessianService</param-value>
      </init-param>
  </servlet>
  
  <servlet-mapping>
      <servlet-name>hessian</servlet-name>
      <url-pattern>/hessian</url-pattern>
  </servlet-mapping>
  
</web-app>
servlet 配置

 

3. C# 端: Entity,接口

  注意:C#端Entity的完整类别:命名空间.类名与java端Entity的完整类名:包.类名必须完全一致。 接口,服务可以不一致

  3.1 Entity

  

技术分享
 1 using System;
 2 using System.Collections;
 3 using System.Collections.Generic;
 4 using System.Linq;
 5 using System.Text;
 6 using System.Threading.Tasks;
 7 
 8 /// <summary>
 9 /// Bean的命名空间需要与java端一致
10 /// </summary>
11 namespace com.mqq.core.hessian.entity
12 {
13     public class RequestEntity
14     {
15         private String sessionID;
16         private String userID;
17         private String userName;
18         private String orgID;
19         private String orgName;
20 
21         private String requestService;
22         private String requestMethod;
23         private String[] requestData; // JSON 格式字符串 每个字符串对应一个DataTable数据集
24         private String requestParamType;  // JSON 格式字符串 可对应多个字符串参数
25         private String requestParamValue; // JSON 格式字符串 可对应多个字符串参数
26 
27         private String returnValue; // 返回值
28 
29         public String SessionID
30         {
31             set { sessionID = value; }
32             get { return sessionID; }
33         }
34 
35         public String UserID
36         {
37             set { userID = value; }
38             get { return userID; }
39         }
40 
41         public String UserName
42         {
43             set { userName = value; }
44             get { return userName; }
45         }
46 
47         public String OrgID
48         {
49             set { orgID = value; }
50             get { return orgID; }
51         }
52 
53         public String OrgName
54         {
55             set { orgName = value; }
56             get { return orgName; }
57         }
58 
59         public String RequestService
60         {
61             set { requestService = value; }
62             get { return requestService; }
63         }
64 
65         public String RequestMethod
66         {
67             set { requestMethod = value; }
68             get { return requestMethod; }
69         }
70 
71         
72         public String RequestParamType
73         {
74             set { requestParamType = value; }
75             get { return requestParamType; }
76         }
77 
78         public String RequestParamValue
79         {
80             set { requestParamValue = value; }
81             get { return requestParamValue; }
82         }
83 
84         public String[] RequestData
85         {
86             set { requestData = value; }
87             get { return requestData; }
88         }
89 
90         public String ReturnValue
91         {
92             set { returnValue = value; }
93             get { return returnValue; }
94         }
95     }
96 }
C# Entity

  3.2 接口

技术分享
 1 using com.mqq.core.hessian.entity;
 2 using System;
 3 using System.Collections.Generic;
 4 using System.Linq;
 5 using System.Text;
 6 using System.Threading.Tasks;
 7 
 8 namespace socketClient.SystemCommon
 9 {
10     interface IHessianService
11     {
12         RequestEntity clientRequest(RequestEntity request);
13 
14         String hessionRequest1(String requestJson);
15 
16         String hessionRequest2(RequestEntity request);
17 
18         RequestEntity hessionRequest3(String param);
19     }
20 }
接口 方法同java端

  3.3 通过hessian 调用java端方法

  

技术分享
  1 using com.mqq.core.hessian.entity;
  2 using hessiancsharp.client;
  3 using Newtonsoft.Json.Linq;
  4 using socketClient.Tool;
  5 using System;
  6 using System.Collections.Generic;
  7 using System.Data;
  8 using System.Linq;
  9 using System.Text;
 10 using System.Threading.Tasks;
 11 using System.Windows.Forms;
 12 
 13 namespace socketClient.SystemCommon
 14 {
 15     public class HessianService
 16     {
 17         private IHessianService service;
 18         private string url = @"http://localhost:8080/WebDemo/hessian";
 19 
 20         public HessianService()
 21         {
 22             CHessianProxyFactory factory = new CHessianProxyFactory();
 23             try {
 24                 service = (IHessianService)factory.Create(typeof(IHessianService), url);
 25             }catch(Exception e)
 26             {
 27                 MessageBox.Show("连接Hessian 通讯错误 "+ url +"\r\n"+e.Message);
 28             }
 29         }
 30 
 31         public object request(String service, String method, DataSet dataSet,params String[] param)
 32         {
 33             //object ret = null;
 34 
 35             RequestEntity request = new RequestEntity();
 36 
 37             request.RequestService = service;
 38             request.RequestMethod = method;
 39             
 40             if(dataSet != null && dataSet.Tables.Count > 0)
 41             {
 42                 String sJSON = "";
 43                 String[] sData = new String[dataSet.Tables.Count];
 44 
 45                 int i = 0;
 46                 foreach(DataTable dt in dataSet.Tables)
 47                 {
 48                     sJSON = JsonUtil.DataTable2Json(dt);
 49                     sData[i] = sJSON;
 50                     i++;
 51                 }
 52                 request.RequestData = sData;
 53             }
 54 
 55             JObject joParam = new JObject();
 56 
 57             //参数名用次序代替 1 开始
 58             int paramName = 1;
 59             foreach (String pram in param)
 60             {
 61                 joParam.Add(paramName.ToString(), pram);
 62                 paramName++;
 63             }
 64 
 65             request.RequestParamValue = joParam.ToString();
 66 
 67             RequestEntity retEntity = this.service.clientRequest(request);
 68 
 69             return retEntity;
 70         }
 71 
 72         public String request(String msg)
 73         {
 74             return service.hessionRequest1(msg);
 75         }
 76 
 77         public void test()
 78         {
 79             String name = "穆QQ";
 80             RequestEntity entity = new RequestEntity();
 81 
 82             //传字符串 返回字符串
 83             String ret = this.service.hessionRequest1(name);
 84 
 85             //传 entity 返回 String
 86             entity.RequestService = "cgddService";
 87             entity.RequestMethod = "getCgdd";
 88             String ret2 = this.service.hessionRequest2(entity);
 89 
 90             //传String 返回Entity
 91             entity = this.service.hessionRequest3("双创周.深圳");
 92     
 93             // 传Entity 返回Entity 可以将需要调用的java方法的服务名,方法名,参数放到Entity
 94             // java 端反射执行该方法(本文未实现) 后 以Entity 的形式返回结果
 95             String[] data = new string[] { "3434", "434" };
 96             entity.RequestData = data;
 97             entity.RequestService = "cgddService";
 98             entity.RequestMethod = "getCgdd";
 99             entity.RequestParamValue = "param1,param2";
100             entity.RequestParamType = "String,Int";
101             entity = this.service.clientRequest(entity);
102         }
103 
104     }
105 }
106     
通过hessian 调用 java端的方法

 


 

     

    

以上是关于hessian 通信,C#客户端调用java服务端服务的主要内容,如果未能解决你的问题,请参考以下文章

Hessian使用笔记

SpringMVC整合Hessian远程服务调用

SpringMVC整合Hessian远程服务调用

hessian原理解析一(客户端分析)

Hessian入门(包括与Spring集成)

Hessian 源码简单分析