程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> java和c#應用hessian通訊的辦法

java和c#應用hessian通訊的辦法

編輯:C#入門知識

java和c#應用hessian通訊的辦法。本站提示廣大學習愛好者:(java和c#應用hessian通訊的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是java和c#應用hessian通訊的辦法正文


本文實例講述了java和c#應用hessian通訊的辦法,長短常適用的技能。分享給年夜家供年夜家參考。詳細剖析以下:

起首,hessian主頁為:http://hessian.caucho.com/

上面經由過程一個簡略的例子進修hessian辦事:辦事端為Java,客戶端為C#。

先要預備好C#和Java的第三方類庫,下載地址:http://hessian.caucho.com/

下載 Hssiancharp.dll及hessian-4.0.37.jar

Hessian辦事端(java):

翻開eclipse創立一個Dynamic Web Project,將hessian-4.0.37.jar放到lib下,年夜概如圖所示:

創立一個通訊接口IHello:

package hessian.test.server;
import java.util.ArrayList;
public interface IHello {
  String sayHello(String msg);  
  void sayHello2(int bean);
  void print(String msg); 
  HelloBean getData(HelloBean bean);
  ArrayList<HelloBean> getBeanList();
  ComplexData getComplexData();
  
}

IHello接口的一個完成:HelloImpl.java

package hessian.test.server;
import java.util.ArrayList;
public class HelloImpl implements IHello{
  public String sayHello(String msg){
    return "Hello " + msg;
  }
  public void sayHello2(int bean){
    System.out.println("Hello " + bean);
  }
  public void print(String msg){
    System.out.println(msg);
  }
  public HelloBean getData(HelloBean bean){
    HelloBean result = new HelloBean();
    result.setName("lu xiaoxun a new name");
    result.setAge(26);
    System.out.print(bean.getName());
    return result;
  }
  public ArrayList<HelloBean> getBeanList(){
    ArrayList<HelloBean> beans = new ArrayList<HelloBean>();
    
    HelloBean b1 = new HelloBean();
    b1.setName("lu1");
    b1.setAge(26);
    beans.add(b1);
    
    HelloBean b2 = new HelloBean();
    b2.setName("lu2");
    b2.setAge(27);
    beans.add(b2);
    
    return beans;
  }
  public ComplexData getComplexData(){
    ComplexData data = new ComplexData();
    ArrayList<HelloBean> beans = getBeanList();
    data.setData(beans, beans.size());
    return data;
  }
}

界說用來停止數據傳輸的類,兩個類都必需完成Serializable接口:

HelloBean.java

package hessian.test.server;
import java.io.Serializable;
public class HelloBean implements Serializable {
  private static final long serialVersionUID = 570423789882653763L;
  private String name;
  private int age;
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public int getAge(){
    return age;
  }
  public void setAge(int age){
    this.age = age;
  }
}

ComplexData.java

package hessian.test.server;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class ComplexData implements Serializable{
  private static final long serialVersionUID = 1L;
  private ArrayList<HelloBean> helloBeans;
  //private Map<String, HelloBean> helloBeanMap;
  private int number;
  public int getNumber(){
    return number;
  }
  public ArrayList<HelloBean> getHelloBeans(){
    return helloBeans;
  }
  public void setData(ArrayList<HelloBean> beans, int num){
    this.number = num;
    this.helloBeans = beans;
//    helloBeanMap = new HashMap<String, HelloBean>();
//    for (HelloBean helloBean : beans) {
//      if(!helloBeanMap.containsKey(helloBean.getName()))
//      {
//        helloBeanMap.put(helloBean.getName(), helloBean);
//      }
//    }
  }
}

web.xml內容:

<?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" 
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  id="WebApp_ID" version="3.0">
  <display-name>hessian server</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>hessian.test.server.HelloImpl</param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>hessian</servlet-name>
    <url-pattern>/hessian</url-pattern>
  </servlet-mapping>
</web-app>

Hessian客戶端(c#):

界說一個與辦事端對應的IHello接口:IHello.cs

  public interface IHello
  {
    String sayHello(String msg);
    void sayHello2(int bean);
    void print(String msg);
    HelloBean getData(HelloBean bean);
    HelloBean[] getBeanList();
    ComplexData getComplexData();
  }

界說與辦事器端分歧的的通訊數據類:

HelloBean.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace hessian.test.server
{
  public class HelloBean
  {
    public String Name 
    {
      set { name = value; }
      get { return name; }
    }

    private String name; //類型和稱號須要和辦事器端分歧

    public int Age
    {
      set { age = value; }
      get { return age; }
    }

    private int age; //類型和稱號須要和辦事器端分歧

    public override String ToString()
    {
      return "Name: "+ name + " Age: " + age;
    }

  }
}

ComplexData.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace hessian.test.server
{
  public class ComplexData
  {
    private HelloBean[] helloBeans;

    //private Dictionary<String, HelloBean> helloBeanMap;

    private int number;

    public int GetNumber()
    {
      return number;
    }

    public HelloBean[] GetBeans()
    {
      return helloBeans;
    }

    //public Dictionary<String, HelloBean> GetBeansDic()
    //{
    //  return helloBeanMap;
    //}
  }
}

在主項目中添加Hessiancsharp.dll援用。

測試代碼:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using hessiancsharp.client;
using hessian.test.server;

namespace HessianClientTest
{
  class Program
  {
    static void Main(string[] args)
    {
      string url = @"http://localhost:8080/HessianServerTest/hessian";
      CHessianProxyFactory factory = new CHessianProxyFactory();

      IHello test = (IHello)factory.Create(typeof(IHello), url);

      //Test function
      Console.WriteLine(test.sayHello("lu"));  //打印從辦事器端獲得的字符串
      test.sayHello2(12);            //在辦事器端掌握台打印 "Hello 12"  
      test.print("hessian");          //在辦事器端掌握台打印 "hessian" 

      //Test Object
      HelloBean bean = new HelloBean();
      //bean.setName("lu xiaoxun");
      bean.Name = "luxiaoxun";
      HelloBean result = test.getData(bean);
      Console.WriteLine(result.Name);
      Console.WriteLine(result.Age);
      Console.WriteLine(result);

      //Test Object Array
      HelloBean[] beans = test.getBeanList();
      if (beans != null)
      {
        foreach (HelloBean data in beans)
        {
          Console.WriteLine(data.ToString());
        }
      }

      //Test complex data
      ComplexData complexData = test.getComplexData();
      if (complexData != null)
      {
        Console.WriteLine("Array number: " + complexData.GetNumber());

        HelloBean[] comArray = complexData.GetBeans();
        if (comArray != null)
        {
          foreach (HelloBean data in comArray)
          {
            Console.WriteLine(data.ToString());
          }
        }

        //Dictionary<String, HelloBean> helloBeanMap = complexData.GetBeansDic();
        //if (helloBeanMap != null)
        //{
        //  foreach (String key in helloBeanMap.Keys)
        //  {
        //    Console.WriteLine(helloBeanMap[key].GetHelloBeanInfo());
        //  }
        //}
      }

      Console.ReadKey();
    }
  }
}

測試成果以下圖所示:

留意事項:

1、辦事端和客戶端用於數據傳遞的對象的定名空間要分歧

IHello接口地點定名空間辦事端和客戶端可以紛歧致,然則IHello頂用到的HelloBean和ComplexData在Java辦事端和C#客戶端中兩個HelloBean類地點的定名空間要分歧。

2、類的字段要分歧

用於數據傳輸的類的字段名和字段類型要分歧(潤飾類型可以紛歧致)。

3、辦事真個類要序列化

4、盡可能應用根本的數據類型
從下面的測試可以看出,傳遞根本的類型沒有成績,傳遞通俗的類對象沒有成績,傳遞ArrayList的時刻也沒有成績(C#客戶端應用Array數組),然則傳遞HashMap字典的時刻會有成績,C#這邊應用Dictionary沒法對應分歧,能夠是因為hash函數外部完成紛歧致招致的,詳細緣由不明。

感興致的同伙可以測試一下本文實例,源碼點擊此處本站下載。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved