- ecsoftcn
- 等级:


- 性别:

- 文章: 53
- 积分: 147
- 来自: 杭州

|
时间:2007-03-18 关键字: Spring xfire webservice soa httpinvoker hessian pool
服务代理生成器( ServiceProxyGenerator )也是一个值得一提的地方,我们先看一下它的接口:
-
-
-
-
-
- public interface ServiceProxyGenerator {
-
-
-
-
-
-
-
-
-
-
-
-
-
- Object getService(Class serviceClass, String serviceUrl, Properties props) throws Exception;
- }
-
-
- public void registService(ServiceModel serviceModel) throws ServiceException {
- ······
- String key = serviceModel.getServiceId() + KEY_SPAR
- + serviceModel.getServiceClass().getName();
- if ( serviceNames .contains(key)) {
- throw new ServiceRegistException( "service is exist!" );
- }
- Object proxy = null ;
- try {
- ServiceProxyGenerator proxyGenerator = (ServiceProxyGenerator) beanFactory
- .getBean(serviceModel.getServiceType() + PROXY_GENERATOR_END );
- proxy = proxyGenerator.getService(serviceModel.getServiceClass(), serviceModel
- .getServiceUrl(), serviceModel.getProps());
- } catch (Exception e) {
- throw new ServiceRegistException( "can't regist service !" , e);
- }
- if (proxy != null ) {
- serviceNames .add(key);
- serviceContainer .put(key, proxy);
- } else {
- throw new ServiceRegistException( "fail to regist service !" );
- }
- }
上面做特殊标记的代码就是应用服务代理生成器的地方,这里我们用到了 Spring 的 bean 工厂,根据注册服务的类型( xfire,httpinvoker,hessian 等)到 Spring 容器里查找相应的生成器,并生成指定类型的服务。看下面配置的几个服务代理生成器:
xml 代码
-
- < bean id = "xfire_generator" class = "com.tonysoft.common.service.repository.generator.XFireServiceProxyGenerator" lazy-init = "true" >
- < property name = "serviceFactory" >
- < ref bean = "xfire.serviceFactory" />
- property >
- bean >
-
-
- < bean id = "hessian_generator" class = "com.tonysoft.common.service.repository.generator.HessianServiceProxyGenerator" lazy-init = "true" >
- bean >
-
-
- < bean id = "httpinvoker_generator" class = "com.tonysoft.common.service.repository.generator.HttpInvokeServiceProxyGenerator" lazy-init = "true" >
- bean >
-
-
- < bean id = "custom_generator" class = "com.tonysoft.common.service.repository.generator.CustomServiceProxyGenerator" lazy-init = "true" >
- bean >
-
-
- < bean id = "serviceRepository" class = "com.tonysoft.common.service.repository.DefaultServiceRepository" >
- bean >
-
-
- < bean id = "serviceIdProvider" class = "com.tonysoft.common.service.repository.provider.DefaultServiceIdProvider" >
- bean >
-
- < bean id = "abstractServiceProxyFactory" class = "com.tonysoft.common.service.repository.ServiceProxyFactory" abstract = "true" >
- bean >
简单看一下 HttpInvoker 类型服务代理生成器的代码:
java 代码
- public class HttpInvokeServiceProxyGenerator implements ServiceProxyGenerator {
-
-
- private HttpInvokerProxyFactoryBean httpInvokerFactory = new HttpInvokerProxyFactoryBean();
-
-
-
-
- public Object getService(Class serviceClass, String serviceUrl, Properties props) {
-
- httpInvokerFactory .setServiceInterface(serviceClass);
-
- httpInvokerFactory .setServiceUrl(serviceUrl);
-
-
- httpInvokerFactory .afterPropertiesSet();
-
- return httpInvokerFactory .getObject();
-
- }
-
- }
是的,正如你所看到的一样,我们这里把真正生成服务代理的任务交给了 Spring 的 HttpInvokerProxyFactoryBean 来完成。
提供在初始化时注册的静态服务功能,配制如下:
-
- < bean id = "bootupServiceRegister" class = "com.tonysoft.common.service.repository.register.BootupServiceRegister" lazy-init = "false" >
- < property name = "services" >
- < list >
- < bean class = "com.tonysoft.common.service.repository.ServiceModel" >
- < property name = "serviceClass" >< value > com.tonysoft.common.service.repository.example.HelloHttpInvoker value > property >
- < property name = "serviceId" >< value > default value > property >
- < property name = "serviceType" >< value > httpinvoker value > property >
- < property name = "serviceUrl" >< value > http://localhost:8080/serviceRepositoryApplication/service/httpInvoker/helloHttpInvoker.service value > property >
- < property name = "props" >
- < props > props >
- property >
- bean >
- < bean class = "com.tonysoft.common.service.repository.ServiceModel" >
- < property name = "serviceClass" >< value > com.tonysoft.common.service.repository.example.HelloXFire value > property >
- < property name = "serviceId" >< value > default value > property >
- < property name = "serviceType" >< value > xfire value > property >
- < property name = "serviceUrl" >< value > http://localhost:8080/serviceRepositoryApplication/service/xfire/helloXFire.service?WSDL value > property >
- < property name = "props" >
- < props > props >
- property >
- bean >
- list >
- property >
- bean >
具体内容可以参看附件中的资源:
一、 ServiceRepository 的源代码( Eclipse 工程)
二、 一个示例应用
三、 打包部署的 ANT 脚本
把项目导入 Eclipse 中,直接运行 Ant 脚本,在 target 目录下会生成服务中心的 jar 包,同时生成示例应用的 war 包,把 war 包放到任意服务器( Server )上并启动服务器并确保应用正常启动。 运行 ServiceRepositoryTest .java 执行完整的单元测试,观测结果。其他的自己看源码吧。
声明:JavaEye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
|
| 返回顶楼 |
|
|
- shaucle
- 等级:


- 性别:

- 文章: 414
- 积分: 413
- 来自: 上海

|
用spring 封闭了传统的ws获取方式.使得用起来更方便,也可能更高效. 好文.
|
| 返回顶楼 |
|
|
- XMLDB
- 等级: 初级会员

- 文章: 505
- 积分: 55

|
只有代码,随想呢?
|
| 返回顶楼 |
|
|
- 生命火花
- 等级: 初级会员

- 文章: 42
- 积分: 78

|
为什么要把spring和xfire这样绑在一起!
我习惯于把ws物理独立出来!
|
| 返回顶楼 |
|
|
- ecsoftcn
- 等级:


- 性别:

- 文章: 53
- 积分: 147
- 来自: 杭州

|
生命火花 写道 为什么要把spring和xfire这样绑在一起!
我习惯于把ws物理独立出来!
不是把XFire和Spring绑在一起,而是通过一个高层的抽象,统一管理各种形式的远程服务。
把ws物理独立出来是什么意思?是专门为ws开一个服务器么?你这里的ws指的是标准的WS,还是指XFire?
|
| 返回顶楼 |
|
|
- dovecat
- 等级:


- 文章: 663
- 积分: 339

|
恩,确实不错!实际上是服务定位器模式的应用!
ps:我咋没看到附件呢?
|
| 返回顶楼 |
|
|
- ecsoftcn
- 等级:


- 性别:

- 文章: 53
- 积分: 147
- 来自: 杭州

|
dovecat 写道 恩,确实不错!实际上是服务定位器模式的应用!
ps:我咋没看到附件呢?
附件在第一部分:http://www.javaeye.com/topic/60681
|
| 返回顶楼 |
|
|
- jianfeng008cn
- 等级:


- 性别:

- 文章: 555
- 积分: 613
- 来自: 湖州

|
没有Rest的ws吗?
|
| 返回顶楼 |
|
|
- ecsoftcn
- 等级:


- 性别:

- 文章: 53
- 积分: 147
- 来自: 杭州

|
jianfeng008cn 写道 没有Rest的ws吗?
这个自己很容易就可以扩展进来,只需要实现"ServiceProxyGenerator"接口提供获取服务.
|
| 返回顶楼 |
|
|