.Net使用Hessian调用JAVA的函数
        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 {
public class HessionServiceImpl implements IHessionService {
![]() 
    
![]() private ICmsService cmsService;
    private ICmsService cmsService;
![]() 
    
![]() public String getHelloWorld() {
    public String getHelloWorld() {
![]() return "Hello World";
       return "Hello World";
![]() }
    }
![]()
![]() /* (non-Javadoc)
    /* (non-Javadoc)
![]() * @see com.digitalchina.dcis.domain.service.IHessionService#getStringList()
     * @see com.digitalchina.dcis.domain.service.IHessionService#getStringList()
![]() */
     */
![]() public List getStringList() {
    public List getStringList() {
![]() List list = new ArrayList();
        List list = new ArrayList();
![]() list.add("duck");
        list.add("duck");
![]() list.add("fish");
        list.add("fish");
![]() return list;
        return list;
![]() }
    }
![]() 
    
![]() public List getCmsContentListByLabel(String label,int num){
    public List getCmsContentListByLabel(String label,int num){
![]() return this.cmsService.getCmsContentListByLabel(label, num);
       return this.cmsService.getCmsContentListByLabel(label, num);
![]() }
    }
![]()
![]() /**
    /**
![]() * @param cmsService the cmsService to set
     * @param cmsService the cmsService to set
![]() */
     */
![]() public void setCmsService(ICmsService cmsService) {
    public void setCmsService(ICmsService cmsService) {
![]() this.cmsService = cmsService;
        this.cmsService = cmsService;
![]() }
    }
![]()
![]() }
}
![]()
![]() public class CmsContent  implements Serializable {
public class CmsContent  implements Serializable {
![]()
![]() public static String PROP_TITLE = "title";
    public static String PROP_TITLE = "title";
![]() public static String PROP_CONTENT = "content";
    public static String PROP_CONTENT = "content";
![]() public static String PROP_ID = "id";
    public static String PROP_ID = "id";
![]()
![]()
![]() // constructors
    // constructors
![]() public CmsContent () {
    public CmsContent () {
![]() initialize();
        initialize();
![]() }
    }
![]()
![]() protected void initialize () {}
    protected void initialize () {}
![]()
![]()
![]() private int hashCode = Integer.MIN_VALUE;
    private int hashCode = Integer.MIN_VALUE;
![]()
![]() // primary key
    // primary key
![]() private java.lang.String id;
    private java.lang.String id;
![]()
![]() // fields
    // fields
![]()
![]() private java.lang.String title;
    private java.lang.String title;
![]()
![]() private java.lang.String content;
    private java.lang.String content;
![]() 
      
![]() 
    
![]()
![]() /**
    /**
![]() * Return the unique identifier of this class
     * Return the unique identifier of this class
![]() * @hibernate.id
     * @hibernate.id
![]() *  generator-class="uuid.hex"
     *  generator-class="uuid.hex"
![]() *  column="ID"
     *  column="ID"
![]() */
     */
![]() public java.lang.String getId () {
    public java.lang.String getId () {
![]() return id;
        return id;
![]() }
    }
![]()
![]() /**
    /**
![]() * Set the unique identifier of this class
     * Set the unique identifier of this class
![]() * @param id the new ID
     * @param id the new ID
![]() */
     */
![]() public void setId (java.lang.String id) {
    public void setId (java.lang.String id) {
![]() this.id = id;
        this.id = id;
![]() this.hashCode = Integer.MIN_VALUE;
        this.hashCode = Integer.MIN_VALUE;
![]() }
    }
![]()
![]()
![]()
![]()
![]() /**
    /**
![]() * Return the value associated with the column: TITLE
     * Return the value associated with the column: TITLE
![]() */
     */
![]() public java.lang.String getTitle () {
    public java.lang.String getTitle () {
![]() return title;
        return title;
![]() }
    }
![]()
![]() /**
    /**
![]() * Set the value related to the column: TITLE
     * Set the value related to the column: TITLE
![]() * @param title the TITLE value
     * @param title the TITLE value
![]() */
     */
![]() public void setTitle (java.lang.String title) {
    public void setTitle (java.lang.String title) {
![]() this.title = title;
        this.title = title;
![]() }
    }
![]()
![]()
![]()
![]() /**
    /**
![]() * Return the value associated with the column: CONTENT
     * Return the value associated with the column: CONTENT
![]() */
     */
![]() public java.lang.String getContent () {
    public java.lang.String getContent () {
![]() return content;
        return content;
![]() }
    }
![]()
![]() /**
    /**
![]() * Set the value related to the column: CONTENT
     * Set the value related to the column: CONTENT
![]() * @param content the CONTENT value
     * @param content the CONTENT value
![]() */
     */
![]() public void setContent (java.lang.String content) {
    public void setContent (java.lang.String content) {
![]() this.content = content;
        this.content = content;
![]() }
    }
![]()
![]()
![]() public int hashCode () {
    public int hashCode () {
![]() if (Integer.MIN_VALUE == this.hashCode) {
        if (Integer.MIN_VALUE == this.hashCode) {
![]() if (null == this.getId()) return super.hashCode();
            if (null == this.getId()) return super.hashCode();
![]() else {
            else {
![]() String hashStr = this.getClass().getName() + ":" + this.getId().hashCode();
                String hashStr = this.getClass().getName() + ":" + this.getId().hashCode();
![]() this.hashCode = hashStr.hashCode();
                this.hashCode = hashStr.hashCode();
![]() }
            }
![]() }
        }
![]() return this.hashCode;
        return this.hashCode;
![]() }
    }
![]()
![]()
![]() public String toString () {
    public String toString () {
![]() return super.toString();
        return super.toString();
![]() }
    }
![]()
![]() }
}
第二步:dotnet的设置
1 打开vs新建一个控制台程序,引用Hessiancsharp.dll,
2 在本地先建一个代理接口,对应java中的IHessionService 接口
![]() public interface ITestService
   public interface ITestService
![]() {
    {
![]() string getHelloWorld();
        string getHelloWorld();
![]() ArrayList getStringList();
        ArrayList getStringList();
![]()
![]() List<Content> getCmsContentListByLabel(string label, int num);
        List<Content> getCmsContentListByLabel(string label, int num);
![]()
![]() }
    }
3 看到上面有个Content的类型,这个其实对应java中的那个CmsContent实体对象,我们也要在本地建立这样一个代理类
![]() public class Content
   public class Content
![]() {
    {
![]() private string title;
       private string title;
![]() public string getTitle {
       public string getTitle {
![]() get { return title; }
           get { return title; }
![]() }
       }
![]()
![]() private string id;
       private string id;
![]() public string getId {
       public string getId {
![]() get { return id; }
           get { return id; }
![]() }
       }
![]() }           说明一点:这两个代理类ITestService和Content的名称是可以随便取的,不一定和java中的一样,但是里面的方法签名必须要和java中的一样,注意这个Content类的属性好像和往常有点不一样,那是因为这个要跟java中的实体对应起来
    }           说明一点:这两个代理类ITestService和Content的名称是可以随便取的,不一定和java中的一样,但是里面的方法签名必须要和java中的一样,注意这个Content类的属性好像和往常有点不一样,那是因为这个要跟java中的实体对应起来
4 下面就可以在程序里调用了![]() using System;
using System;
![]() using System.Collections.Generic;
using System.Collections.Generic;
![]() using System.Text;
using System.Text;
![]() using hessiancsharp.client;
using hessiancsharp.client;
![]()
![]() namespace Hessiancsharp_Test
namespace Hessiancsharp_Test
![]() {
{
![]() class Program
    class Program
![]() {
    {
![]() static void Main(string[] args)
        static void Main(string[] args)
![]() {
        {
![]()
![]() CHessianProxyFactory factory = new CHessianProxyFactory();
            CHessianProxyFactory factory = new CHessianProxyFactory();
![]() string url = "http://yyk:9091/remote/hessionService";
            string url = "http://yyk:9091/remote/hessionService";
![]()
![]() ITestService test = factory.Create(typeof(ITestService), url) as ITestService;
            ITestService test = factory.Create(typeof(ITestService), url) as ITestService;
![]()
![]() foreach (Content item in test.getCmsContentListByLabel("companynews",10))
            foreach (Content item in test.getCmsContentListByLabel("companynews",10))
![]() {
            {
![]() Console.WriteLine(item.getId+" "+item.getTitle);
                Console.WriteLine(item.getId+" "+item.getTitle);
![]() }
            }
![]()
![]() Console.Read();
            Console.Read();
![]() }
        }
![]() }
    }
![]() }
}
![]()
最后说明一下:这个第一次在博客园写blog,如有不当还请多多谅解~ 2008年6月11日14:11:36
先到http://www.hessiancsharp.org上去下载一个DLL文件(或者直接下载我的Demo,里面有dll和源代码文件),具体源码
第一步:java的设置,简单列举几个类(至于要怎么实现可以看这篇文章)
1 服务
 public class HessionServiceImpl implements IHessionService {
public class HessionServiceImpl implements IHessionService { 
     private ICmsService cmsService;
    private ICmsService cmsService; 
     public String getHelloWorld() {
    public String getHelloWorld() { return "Hello World";
       return "Hello World"; }
    }
 /* (non-Javadoc)
    /* (non-Javadoc) * @see com.digitalchina.dcis.domain.service.IHessionService#getStringList()
     * @see com.digitalchina.dcis.domain.service.IHessionService#getStringList() */
     */ public List getStringList() {
    public List getStringList() { List list = new ArrayList();
        List list = new ArrayList(); list.add("duck");
        list.add("duck"); list.add("fish");
        list.add("fish"); return list;
        return list; }
    } 
     public List getCmsContentListByLabel(String label,int num){
    public List getCmsContentListByLabel(String label,int num){ return this.cmsService.getCmsContentListByLabel(label, num);
       return this.cmsService.getCmsContentListByLabel(label, num); }
    }
 /**
    /** * @param cmsService the cmsService to set
     * @param cmsService the cmsService to set */
     */ public void setCmsService(ICmsService cmsService) {
    public void setCmsService(ICmsService cmsService) { this.cmsService = cmsService;
        this.cmsService = cmsService; }
    }
 }
}
2 实体类 CmsContent
 public class CmsContent  implements Serializable {
public class CmsContent  implements Serializable {
 public static String PROP_TITLE = "title";
    public static String PROP_TITLE = "title"; public static String PROP_CONTENT = "content";
    public static String PROP_CONTENT = "content"; public static String PROP_ID = "id";
    public static String PROP_ID = "id";

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

 private int hashCode = Integer.MIN_VALUE;
    private int hashCode = Integer.MIN_VALUE;
 // primary key
    // primary key private java.lang.String id;
    private java.lang.String id;
 // fields
    // fields
 private java.lang.String title;
    private java.lang.String title;
 private java.lang.String content;
    private java.lang.String content; 
       
    
 /**
    /** * Return the unique identifier of this class
     * Return the unique identifier of this class * @hibernate.id
     * @hibernate.id *  generator-class="uuid.hex"
     *  generator-class="uuid.hex" *  column="ID"
     *  column="ID" */
     */ public java.lang.String getId () {
    public java.lang.String getId () { return id;
        return id; }
    }
 /**
    /** * Set the unique identifier of this class
     * Set the unique identifier of this class * @param id the new ID
     * @param id the new ID */
     */ public void setId (java.lang.String id) {
    public void setId (java.lang.String id) { this.id = id;
        this.id = id; this.hashCode = Integer.MIN_VALUE;
        this.hashCode = Integer.MIN_VALUE; }
    }



 /**
    /** * Return the value associated with the column: TITLE
     * Return the value associated with the column: TITLE */
     */ public java.lang.String getTitle () {
    public java.lang.String getTitle () { return title;
        return title; }
    }
 /**
    /** * Set the value related to the column: TITLE
     * Set the value related to the column: TITLE * @param title the TITLE value
     * @param title the TITLE value */
     */ public void setTitle (java.lang.String title) {
    public void setTitle (java.lang.String title) { this.title = title;
        this.title = title; }
    }


 /**
    /** * Return the value associated with the column: CONTENT
     * Return the value associated with the column: CONTENT */
     */ public java.lang.String getContent () {
    public java.lang.String getContent () { return content;
        return content; }
    }
 /**
    /** * Set the value related to the column: CONTENT
     * Set the value related to the column: CONTENT * @param content the CONTENT value
     * @param content the CONTENT value */
     */ public void setContent (java.lang.String content) {
    public void setContent (java.lang.String content) { this.content = content;
        this.content = content; }
    }

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

 public String toString () {
    public String toString () { return super.toString();
        return super.toString(); }
    }
 }
}第二步:dotnet的设置
1 打开vs新建一个控制台程序,引用Hessiancsharp.dll,
2 在本地先建一个代理接口,对应java中的IHessionService 接口
 public interface ITestService
   public interface ITestService {
    { string getHelloWorld();
        string getHelloWorld(); ArrayList getStringList();
        ArrayList getStringList();
 List<Content> getCmsContentListByLabel(string label, int num);
        List<Content> getCmsContentListByLabel(string label, int num);
 }
    }3 看到上面有个Content的类型,这个其实对应java中的那个CmsContent实体对象,我们也要在本地建立这样一个代理类
 public class Content
   public class Content {
    { private string title;
       private string title; public string getTitle {
       public string getTitle { get { return title; }
           get { return title; } }
       }
 private string id;
       private string id; public string getId {
       public string getId { get { return id; }
           get { return id; } }
       } }
    }4 下面就可以在程序里调用了
 using System;
using System; using System.Collections.Generic;
using System.Collections.Generic; using System.Text;
using System.Text; using hessiancsharp.client;
using hessiancsharp.client;
 namespace Hessiancsharp_Test
namespace Hessiancsharp_Test {
{ class Program
    class Program {
    { static void Main(string[] args)
        static void Main(string[] args) {
        {
 CHessianProxyFactory factory = new CHessianProxyFactory();
            CHessianProxyFactory factory = new CHessianProxyFactory(); string url = "http://yyk:9091/remote/hessionService";
            string url = "http://yyk:9091/remote/hessionService";
 ITestService test = factory.Create(typeof(ITestService), url) as ITestService;
            ITestService test = factory.Create(typeof(ITestService), url) as ITestService;
 foreach (Content item in test.getCmsContentListByLabel("companynews",10))
            foreach (Content item in test.getCmsContentListByLabel("companynews",10)) {
            { Console.WriteLine(item.getId+" "+item.getTitle);
                Console.WriteLine(item.getId+" "+item.getTitle); }
            }
 Console.Read();
            Console.Read(); }
        } }
    } }
}
最后说明一下:这个第一次在博客园写blog,如有不当还请多多谅解~ 2008年6月11日14:11:36
 
                    
                     
                    
                 
                    
                
 
 
    

 
     
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号