hessian的java示例

Hessian:hessian是一个轻量级的remoting onhttp工具。
[b][size=large]一、hessian简单示例:[/size][/b]
[size=large]1.服务端接口:[/size]
public interface IAlarm {
public String saveAlarm(String alarmStr);

public String firstAlarm(String alarmStr);
}
[size=large]2.服务端实现类[/size]
public class AlarmImpl extends HessianServlet implements IAlarm{

public String saveAlarm(String alarmStr) {
return alarmStr+",保存成功。";
}

public String firstAlarm(String alarmStr) {
return alarmStr+",通知。";
}

}
[size=large]3.服务端web.xml需要添加的配置[/size]

Alarm
com.caucho.hessian.server.HessianServlet

home-class
my.hessian.service.impl.AlarmImpl


home-api
my.hessian.service.IAlarm

1


Alarm
/Alarm

[size=large]4.客户端接口(跟服务端接口一致,接口所在包路径可不同)[/size]
public interface IAlarm {
public String saveAlarm(String alarmStr);

public String firstAlarm(String alarmStr);
}
[size=large]5.客户端调用[/size]
HessianProxyFactory factory = new HessianProxyFactory();
String url="http://192.168.2.243/knowledgePoints/Alarm";
IAlarm alarm=(IAlarm)factory.create(IAlarm.class, url);
System.out.println(alarm.saveAlarm("超速报警"));
[size=large]6.示例说明[/size]
服务端和客户端都需要引入hessian-4.0.7.jar包。该示例可运行。

[b][size=large]二、hessian与spring整合示例:[/size][/b]
[size=large] 1.服务端接口[/size]
public interface ITripAgent {

public String saveTrip(String tripStr);
}
[size=large] 2.服务端实现类[/size]
public class TripAgent extends HessianServlet implements ITripAgent{

public String saveTrip(String tripStr){
return tripStr+",保存成功";
};
}

[size=large]3.服务端WEB-INF下加一个remoting-servlet.xml文件[/size]

xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop = "http://www.springframework.org/schema/aop"
xmlns:tx = "http://www.springframework.org/schema/tx"
xsi:schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd" >











[size=large]4.服务端web.xml添加如下配置[/size]

org.springframework.web.context.ContextLoaderListener



contextConfigLocation
classpath:my/hessian/config/spring/bean/remoting-servlet.xml



remoting
org.springframework.web.servlet.DispatcherServlet
1


remoting
/remoting/*


[size=large]5.客户端接口(跟服务端接口一致,接口所在包路径可不同)[/size]
public interface ITripAgent {

public String saveTrip(String tripStr);
}

[size=large]6.客户端添加一个remoting-client.xml文件[/size]

xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop = "http://www.springframework.org/schema/aop"
xmlns:tx = "http://www.springframework.org/schema/tx"
xsi:schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd" >



http://192.168.2.243/knowledgePoints/remoting


my.hessianClient.ITripAgent




[size=large]7.客户端web.xml中添加一下代码[/size]

org.springframework.web.context.ContextLoaderListener



contextConfigLocation
/WEB-INF/remoting-client.xml



[size=large]8.客户端调用[/size]
ApplicationContext context = new ClassPathXmlApplicationContext("my/hessianClient/remoting-client.xml");
ITripAgent trip = (ITripAgent) context.getBean("myServiceClient");
System.out.println(trip.saveTrip("白石洲到茂华大厦的行程"));

[size=large] 9.示例说明[/size]
服务端和客户端都需要引入spring的jar包(多个,不知道需要哪些,就把spring的jar全引入) ,服务端需要hessain的jar包(1个)。该示例可运行。
[b][size=x-large]三、其他(在包下找文件的两种写法)[/size][/b]

contextConfigLocation
/WEB-INF/classes/my/hessianClient/remoting-client.xml

中/WEB-INF/classes/my/hessian/config/spring/bean/remoting-servlet.xml
与 classpath:my/hessian/config/spring/bean/remoting-servlet.xml 是等价的

[b][size=x-large]四、遇到的奇怪问题[/size][/b]
hessian与spring整合的示例中, remoting-servlet.xml放在包路径my.hessian.config.spring.bean下时,二(7) 中配置 classpath:my/hessian/config/spring/bean/remoting-servlet.xml会报找不到 WEB_INF/remoting-servlet.xml文件的错误,报的路径找不到与我配置的路径是不一样的,这个错报的很离谱,将remoting-servlet.xml放到WEB-INF下时,就不会报错了,有知道原因的,请回复我,3q。

你可能感兴趣的:(hessian)