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]
<servlet>
<servlet-name>Alarm</servlet-name>
<servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class>
<init-param>
<param-name>home-class</param-name>
<param-value>my.hessian.service.impl.AlarmImpl</param-value>
</init-param>
<init-param>
<param-name>home-api</param-name>
<param-value>my.hessian.service.IAlarm</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Alarm</servlet-name>
<url-pattern>/Alarm</url-pattern>
</servlet-mapping>
[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]
<?xml version = "1.0" encoding = "UTF-8" ?>
<beans xmlns = "http://www.springframework.org/schema/beans"
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" >

<!-- 定义普通的bean实例 -->
<bean id="Trip" class="my.hessian.service.impl.TripAgent"/>
<!-- 使用HessianServiceExporter 将普通bean导出成Hessian服务-->
<bean name="/remoting" class="org.springframework.remoting.caucho.HessianServiceExporter">
<!-- 需要导出的目标bean-->
<property name="service" ref="Trip"/>
<!-- Hess;ian服务的接口-->
<property name="serviceInterface" value="my.hessian.service.ITripAgent"/>
</bean>
</beans>
[size=large]4.服务端web.xml添加如下配置[/size]
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> <!--添加监听器 -->
</listener>
<!-- 指定spring的配置文件在哪里,在这个配置文件中导出了Hessian服务 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:my/hessian/config/spring/bean/remoting-servlet.xml</param-value>
</context-param>
<!-- Hessian通过Servlet提供远程服务,需要将某个匹配的模式映射到hessian服务中,spring的dispatcherServlet能完成此功能,DispatcherServlet可将匹配模式的请求转发到Hessian服务,web.xml只是定义了“请求转发器”,该转发器将匹配/remoting/*的请求截获,转发给context的bean处理。而HessianServiceExporter提供bean服务。 -->
<servlet>
<servlet-name>remoting</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>remoting</servlet-name>
<url-pattern>/remoting/*</url-pattern>
</servlet-mapping>

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

public String saveTrip(String tripStr);
}

[size=large]6.客户端添加一个remoting-client.xml文件[/size]
<?xml version = "1.0" encoding = "UTF-8" ?>
<beans xmlns = "http://www.springframework.org/schema/beans"
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" >
<bean id="myServiceClient" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
<property name="serviceUrl">
<!--下面的value是客户端要访问服务端的请求 knowledgePoints是服务端的项目访问名 ,remoting与服务端的一致,客户端请求服务端,匹配的请求到达服务端后,对于能匹配的请求,会通过spring的DispatcherServlet转发hessian服务-->
<value>http://192.168.2.243/knowledgePoints/remoting</value>
</property>
<property name="serviceInterface">
<value>my.hessianClient.ITripAgent</value>
</property>
</bean>
</beans>

[size=large]7.客户端web.xml中添加一下代码[/size]
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> <!--添加监听器 -->
</listener>
<!-- 指定spring的配置文件在哪里,在这个配置文件中导出了Hessian服务 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/remoting-client.xml</param-value>
</context-param>
</web-app>

[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]
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/my/hessianClient/remoting-client.xml</param- value>
</context-param>
中/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。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值