.Net使用Hessian调用JAVA的函数

本文介绍Hessian二进制协议的工作原理及其在Java与.NET间的应用实例。Hessian类似于WebService,但采用自定义的二进制协议,提供更高效的数据传输方式。文中详细展示了如何在Java端实现服务接口及.NET端调用这些服务的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

        Hessian其实很像web service,只不过它的协议不是SOAP,而是它自己规定的binary协议。Hessian的server端提供一个servlet基类,client端获得一个service接口(也就是stub)之后调用上面的方法,stub将方法调用marshal之后通过HTTP传到server,server借助reflection调用service方法。
   先到 http://www.hessiancsharp.org上去下载一个DLL文件(或者直接下载我的Demo,里面有dll和源代码文件), 具体源码

第一步:java的设置,简单列举几个类(至于要怎么实现可以看这篇 文章

1  服务
public   class  HessionServiceImpl implements IHessionService  {
    
    
private ICmsService cmsService;
    
    
public String getHelloWorld() {
       
return "Hello World";
    }


    
/**//* (non-Javadoc)
     * @see com.digitalchina.dcis.domain.service.IHessionService#getStringList()
     
*/

    
public List getStringList() {
        List list 
= new ArrayList();
        list.add(
"duck");
        list.add(
"fish");
        
return list;
    }

    
    
public List getCmsContentListByLabel(String label,int num){
       
return this.cmsService.getCmsContentListByLabel(label, num);
    }


    
/**//**
     * @param cmsService the cmsService to set
     
*/

    
public void setCmsService(ICmsService cmsService) {
        
this.cmsService = cmsService;
    }


}


2  实体类 CmsContent

public   class  CmsContent  implements Serializable  {

    
public static String PROP_TITLE = "title";
    
public static String PROP_CONTENT = "content";
    
public static String PROP_ID = "id";


    
// constructors
    public CmsContent () {
        initialize();
    }


    
protected void initialize () {}


    
private int hashCode = Integer.MIN_VALUE;

    
// primary key
    private java.lang.String id;

    
// fields

    
private java.lang.String title;

    
private java.lang.String content;
      
    

    
/**//**
     * Return the unique identifier of this class
     * @hibernate.id
     *  generator-class="uuid.hex"
     *  column="ID"
     
*/

    
public java.lang.String getId () {
        
return id;
    }


    
/**//**
     * Set the unique identifier of this class
     * @param id the new ID
     
*/

    
public void setId (java.lang.String id) {
        
this.id = id;
        
this.hashCode = Integer.MIN_VALUE;
    }





    
/**//**
     * Return the value associated with the column: TITLE
     
*/

    
public java.lang.String getTitle () {
        
return title;
    }


    
/**//**
     * Set the value related to the column: TITLE
     * @param title the TITLE value
     
*/

    
public void setTitle (java.lang.String title) {
        
this.title = title;
    }




    
/**//**
     * Return the value associated with the column: CONTENT
     
*/

    
public java.lang.String getContent () {
        
return content;
    }


    
/**//**
     * Set the value related to the column: CONTENT
     * @param content the CONTENT value
     
*/

    
public void setContent (java.lang.String content) {
        
this.content = content;
    }



    
public int hashCode () {
        
if (Integer.MIN_VALUE == this.hashCode) {
            
if (null == this.getId()) return super.hashCode();
            
else {
                String hashStr 
= this.getClass().getName() + ":" + this.getId().hashCode();
                
this.hashCode = hashStr.hashCode();
            }

        }

        
return this.hashCode;
    }



    
public String toString () {
        
return super.toString();
    }


}

第二步:dotnet的设置

       1 打开vs新建一个控制台程序,引用 Hessiancsharp.dll,

       2 在本地先建一个代理接口,对应java中的IHessionService 接口
    public   interface  ITestService
    
{
        
string getHelloWorld();
        ArrayList getStringList();

        List
<Content> getCmsContentListByLabel(string label, int num);

    }

       3 看到上面有个Content的类型,这个其实对应java中的那个CmsContent实体对象,我们也要在本地建立这样一个代理类
    public   class  Content
    
{
       
private string title;
       
public string getTitle {
           
get return title; }
       }


       
private string id;
       
public string getId {
           
get return id; }
       }

    }
            说明一点:这两个代理类ITestService和Content的名称是可以随便取的,不一定和java中的一样,但是里面的方法签名必须要和java中的一样,注意这个Content类的属性好像和往常有点不一样,那是因为这个要跟java中的实体对应起来

        4 下面就可以在程序里调用了
using  System;
using  System.Collections.Generic;
using  System.Text;
using  hessiancsharp.client;

namespace  Hessiancsharp_Test
{
    
class Program
    
{
        
static void Main(string[] args)
        
{

            CHessianProxyFactory factory 
= new CHessianProxyFactory();
            
string url = "http://yyk:9091/remote/hessionService";

            ITestService test 
= factory.Create(typeof(ITestService), url) as ITestService;

            
foreach (Content item in test.getCmsContentListByLabel("companynews",10))
            
{
                Console.WriteLine(item.getId
+" "+item.getTitle);
            }


            Console.Read();
        }

    }

}


        最后说明一下:这个第一次在博客园写blog,如有不当还请多多谅解~  2008年6月11日14:11:36

 

转载于:https://www.cnblogs.com/qiba78/archive/2008/06/11/1217353.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值