bboss 发布和使用hessian服务方法介绍

hessian是一款性能非常不错的RPC通讯组件,最近抽空将bboss和hessian做了个整合,可以简单方便地将bboss ioc管理的组件直接发布为hessian服务,本文详细介绍之。
[size=large]一、bboss hessian属性[/size]
bboss ioc为hessian组件定义了一组扩展属性,说明如下:
hessian:api 服务接口
hessian:servicePort 指定服务标识
hessian:serializable xml|bin 序列化类型,默认为bin
hessian:debug default false used by serializable="bin".
hessian:sendCollectionType used by serializable="bin". default true Set whether to send the Java collection type for each serialized collection.
hessian:serializerFactory used by serializable="bin".default com.caucho.hessian.io.SerializerFactory


服务定义示例:

<property name="tokenservice" hessian:servicePort="tokenService"
class="com.demo.common.action.TokenController" />

<property name="tokenservicebin" hessian:servicePort="tokenService"
hessian:debug="true" hessian:sendCollectionType="true"
hessian:serializerFactory="com.caucho.hessian.io.SerializerFactory"
class="com.demo.common.action.TokenController" />

<property name="tokenserviceforxml" hessian:servicePort="tokenService"
hessian:serializable="xml"
class="com.demo.common.action.TokenController" />


这些服务定义只需要放置在ioc配置文件中即可,通过[url=http://yin-bp.iteye.com/blog/1153798]bboss ioc容器装载和实例化[/url]。

[size=large]二、hessian服务部署[/size]
依托bboss ioc模块,hessian服务发布非常简单,首先配置hessian 服务dispatchservlet用来接收hessian服务调用请求:
[size=large][b]服务名称来自请求参数的配置方法[/b][/size]:
在web.xml文件中配置HessionDispatchServlet
<servlet>
<servlet-name>HessionRemote</servlet-name>
<servlet-class>org.frameworkset.spi.remote.hession.HessionDispatchServlet</servlet-class>

</servlet>
<servlet-mapping>
<servlet-name>HessionRemote</servlet-name>
<url-pattern>/hessian</url-pattern>
</servlet-mapping>

客户端通过以下方式传递服务名称:
String url = "http://10.25.192.142:8081/context/hessian?service=tokenService";
CommonUserManagerInf tokenService = (CommonUserManagerInf) factory.create(CommonUserManagerInf.class, url);
Result result = tokenService.getUserByUserAccount("yinbp");


[size=large][b]服务名称来自请求地址(restful)的配置方法[/b][/size]
<servlet>
<servlet-name>HessionRemote</servlet-name>
<servlet-class>org.frameworkset.spi.remote.hession.HessionDispatchServlet</servlet-class>
<init-param>
<param-name>restful</param-name>
<param-value>true</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>HessionRemote</servlet-name>
<url-pattern>/hessian/*</url-pattern>
</servlet-mapping>


客户端通过以下方式传递服务名称:
String url = "http://localhost/hessian/commonuserService";
CommonUserManagerInf tokenService = (CommonUserManagerInf) factory.create(CommonUserManagerInf.class, url);
Result result = tokenService.getUserByUserAccount("yinbp");


这样所有的bboss ioc容器中的组件(这些组件需要实现特定的服务接口)即可作为hessian服务接收客户端调用了。

[size=large]三、客户端调用hessian服务[/size]
[b]定义服务url[/b]
String url = "http://localhost:8080/context/hessian?container=bboss.hessian.mvc&containertype=mvc&service=basicservice";//指定容器标识和容器类型及服务标识
url = "http://localhost:8080/context/hessian?service=basicservice";//默认获取mvc容器中的组件
[b]url参数说明[/b]:
container:服务端ioc容器标识,一般是ioc容器根xml文件对应的类包路径,
例如:[url=https://github.com/bbossgroups/bbossgroups-3.5/blob/bboss3.6.2/bboss-hession/test/org/frameworkset/spi/remote/hession/server/hessian-service.xml]org/frameworkset/spi/remote/hession/server/hessian-service.xml[/url]
mvc类型容器值为bboss.hessian.mvc(也是container的默认值)
containertype: 容器类型
mvc mvc容器
simple 对应ioc容器类型为DefaultApplicationContext
其他值 对应ioc容器类型为ApplicationContext
service:服务标识,ioc组件的名称
[b]创建bin模式客户端代理[/b]
HessianProxyFactory factory = new HessianProxyFactory();
ServiceInf basic = (ServiceInf) factory.create(org.frameworkset.spi.remote.hession.server.ServiceInf.class, url);

System.out.println("Hello: " + basic.hello("John"));

[b]创建xml模式客户端代理[/b]
BurlapProxyFactory factory = new BurlapProxyFactory();
ServiceInf basic = (ServiceInf) factory.create(org.frameworkset.spi.remote.hession.server.ServiceInf.class, url);

System.out.println("Hello: " + basic.hello("John"));

[b]通过bboss-ioc配置和获取客户端[/b]
<property name="clientservice" factory-class="com.caucho.hessian.client.HessianProxyFactory" factory-method="create">
<construction>
<property value="org.frameworkset.spi.remote.hession.server.ServiceInf"/>
<property value="http://localhost:8080/context/hessian?service=basicservice"/>
</construction>
</property>

DefaultApplicationContext context = DefaultApplicationContext.getApplicationContext("org/frameworkset/spi/remote/hession/client/hessian-client.xml");
//获取客户端组件实例
ServiceInf basic = context.getTBeanObject("clientservice", ServiceInf.class);

使用bboss工厂模式,调用HessianProxyFactory的create方法创建hessian服务客户端调用组件,同时我们可以采用bboss ioc依赖注入特征,将hessian客户端的相关参数(connectionTimeout,readTimeout等)设置到HessianProxyFactory中.
[url=https://github.com/bbossgroups/bbossgroups-3.5/blob/bboss3.6.2/bboss-hession/test/org/frameworkset/spi/remote/hession/client/hessian-client.xml]org/frameworkset/spi/remote/hession/client/hessian-client.xml[/url]

ok,bboss 发布和使用hessian服务方法就介绍到此,欢迎大家留言交流。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值