Spring发布多协议的远程服务其实比较简单,感觉Spring这方面已经做的好了,自己没必要再去搞些什么服务池。
[b]1.ServiceExporter[/b]
[code]public class ServiceExporter extends RemoteExporter
implements Controller, InitializingBean {
/**
*访问协议属性
*/
public static final String REMOTE_EXPORTER = "RemoteExporter";
/**
*访问协议hession
*/
public static final String HESSIAN_SERVICE_EXPORTER = "HessianServiceExporter";
HessianServiceExporter hessian=new HessianServiceExporter();
/**
*访问协议HttpInvoker
*/
public static final String HTTP_INVOKER_SERVICE_EXPORTER = "HttpInvokerServiceExporter";
HttpInvokerServiceExporter httpInvoker=new HttpInvokerServiceExporter();
/**
*访问协议Burlap
*/
public static final String BURLAP_SERVICE_EXPORTER = "BurlapServiceExporter";
BurlapServiceExporter burlap=new BurlapServiceExporter();
public void setService(Object service) {
super.setService(service);
hessian.setService(service);
httpInvoker.setService(service);
burlap.setService(service);
}
public void setServiceInterface(Class serviceInterface) {
super.setServiceInterface(serviceInterface);
hessian.setServiceInterface(serviceInterface);
httpInvoker.setServiceInterface(serviceInterface);
burlap.setServiceInterface(serviceInterface);
}
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception{
//检查访问协议属性
String remoteExporter = request.getHeader(ServiceExporter.REMOTE_EXPORTER);
// 访问协议Hessian
if(remoteExporter==null) {
hessian.handleRequest(request,response);
return null;
}
//访问协议HttpInvoker
if(remoteExporter.equalsIgnoreCase(ServiceExporter.HTTP_INVOKER_SERVICE_EXPORTER))
httpInvoker.handleRequest(request,response);
//访问协议Burlap
else if(remoteExporter.equalsIgnoreCase(ServiceExporter.BURLAP_SERVICE_EXPORTER))
burlap.handleRequest(request,response);
//访问协议Hessian
else
hessian.handleRequest(request,response);
return null;
}
public void afterPropertiesSet() throws Exception {
hessian.afterPropertiesSet();
burlap.afterPropertiesSet();
httpInvoker.afterPropertiesSet();
}[/code]
[b]2.把接口发布为多协议远程服务[/b]
[code]<bean name="/log/BusinessLogMessageService" class="bpo.ServiceExporter">
<property name="service" ref="log.BusinessLogMessageBPOTx"/>
<property name="serviceInterface" value="log.ILogMessageService"/>
</bean> [/code]
[b]1.ServiceExporter[/b]
[code]public class ServiceExporter extends RemoteExporter
implements Controller, InitializingBean {
/**
*访问协议属性
*/
public static final String REMOTE_EXPORTER = "RemoteExporter";
/**
*访问协议hession
*/
public static final String HESSIAN_SERVICE_EXPORTER = "HessianServiceExporter";
HessianServiceExporter hessian=new HessianServiceExporter();
/**
*访问协议HttpInvoker
*/
public static final String HTTP_INVOKER_SERVICE_EXPORTER = "HttpInvokerServiceExporter";
HttpInvokerServiceExporter httpInvoker=new HttpInvokerServiceExporter();
/**
*访问协议Burlap
*/
public static final String BURLAP_SERVICE_EXPORTER = "BurlapServiceExporter";
BurlapServiceExporter burlap=new BurlapServiceExporter();
public void setService(Object service) {
super.setService(service);
hessian.setService(service);
httpInvoker.setService(service);
burlap.setService(service);
}
public void setServiceInterface(Class serviceInterface) {
super.setServiceInterface(serviceInterface);
hessian.setServiceInterface(serviceInterface);
httpInvoker.setServiceInterface(serviceInterface);
burlap.setServiceInterface(serviceInterface);
}
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception{
//检查访问协议属性
String remoteExporter = request.getHeader(ServiceExporter.REMOTE_EXPORTER);
// 访问协议Hessian
if(remoteExporter==null) {
hessian.handleRequest(request,response);
return null;
}
//访问协议HttpInvoker
if(remoteExporter.equalsIgnoreCase(ServiceExporter.HTTP_INVOKER_SERVICE_EXPORTER))
httpInvoker.handleRequest(request,response);
//访问协议Burlap
else if(remoteExporter.equalsIgnoreCase(ServiceExporter.BURLAP_SERVICE_EXPORTER))
burlap.handleRequest(request,response);
//访问协议Hessian
else
hessian.handleRequest(request,response);
return null;
}
public void afterPropertiesSet() throws Exception {
hessian.afterPropertiesSet();
burlap.afterPropertiesSet();
httpInvoker.afterPropertiesSet();
}[/code]
[b]2.把接口发布为多协议远程服务[/b]
[code]<bean name="/log/BusinessLogMessageService" class="bpo.ServiceExporter">
<property name="service" ref="log.BusinessLogMessageBPOTx"/>
<property name="serviceInterface" value="log.ILogMessageService"/>
</bean> [/code]
本文介绍如何使用Spring轻松发布支持多种协议(如Hessian、HttpInvoker和Burlap)的远程服务。通过一个具体示例展示了配置和服务导出的过程。
35

被折叠的 条评论
为什么被折叠?



