.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 服务
第二步:dotnet的设置
1 打开vs新建一个控制台程序,引用Hessiancsharp.dll,
2 在本地先建一个代理接口,对应java中的IHessionService 接口
3 看到上面有个Content的类型,这个其实对应java中的那个CmsContent实体对象,我们也要在本地建立这样一个代理类
4 下面就可以在程序里调用了
最后说明一下:这个第一次在博客园写blog,如有不当还请多多谅解~ 2008年6月11日14:11:36
先到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;
}
}
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();
}
}
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);
}
{
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中的实体对应起来{
private string title;
public string getTitle {
get { return title; }
}
private string id;
public string getId {
get { return id; }
}
}
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();
}
}
}
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