通常在Spring发布Hession,RMI等

通常在Spring发布Hession,RMI等,是非常方便的,

但是要发布SOAP类型的WebService则要依赖一个独立的Servlet容器(如Tomcat+Axis),

这种Webservice一般还有别的配置文件,比如web.xml,wsdd文件等等
。有时侯,你可能想一台机器上只部署一个Http Soap Service
,这种场合你可能不希望安装一个类似Tomcat的容器,
你更希望发布的时候就是一个服务程序,该程序启动则提供WebService.这篇文章描述一种解决方案。
开发环境:
Spring 1.2.6
XFire 1.0
Jetty 4.2.1
方案描述:我们可以通过XFire的编程接口来创建WebService并嵌入一个HttpServer,
从而来做到在一个独立应用程序中发布Http Service
 
 1 //  Create an XFire Service
 2         ObjectServiceFactory serviceFactory  =   new  ObjectServiceFactory();
 3         Service service  =  serviceFactory.create(Echo. class );
 4         service.setInvoker( new  BeanInvoker( new  EchoImpl()));
 5          //  Register the service in the ServiceRegistry
 6         XFire xfire  =  XFireFactory.newInstance().getXFire();
 7         xfire.getServiceRegistry().register(service);
 8          //  Start the HTTP server
 9         XFireHttpServer server  =   new  XFireHttpServer();
10         server.setPort( 8191 );
11         server.start();

这样的话,如果发布多个WebSerice则要依次显式的创建 XFire Service,然后再一一注册,

这样显然是不够优雅的。

我们想要让开发者在Spring配置文件中指定要发布为WebService的POJOs,

然后载入Spring环境就自动发布为webservice,而不需要跟 XFire API打交道。

首先,我们想要一个BeanFacory,能把一个pojo装配成XFire Service

 
 1  /**
 2   * 
 3    */
 4  package  com.yovn.ws.xfire.example;
 5 
 6  import  org.codehaus.xfire.service.Service;
 7  import  org.codehaus.xfire.service.binding.BeanInvoker;
 8  import  org.codehaus.xfire.service.binding.ObjectServiceFactory;
 9  import  org.springframework.beans.factory.FactoryBean;
10 
11  /**
12   *  @author  new
13   *
14    */
15  public   class  XFireServiceFactoryBean  implements  FactoryBean
16  {
17 
18      
19      
20      
21       private  Class serviceClass;
22      
23      
24       private  Object target;
25      
26      
27       private  Service service;
28      
29      
30       private   final  ObjectServiceFactory sf = new  ObjectServiceFactory();
31      
32       /**
33       * 
34        */
35       public  XFireServiceFactoryBean()
36      {
37          
38      }
39 
40       /*  (non-Javadoc)
41       * @see org.springframework.beans.factory.FactoryBean#getObject()
42        */
43       public  Object getObject()  throws  Exception
44      {
45           if (service == null )
46          {
47              service = sf.create(serviceClass);
48              service.setInvoker( new  BeanInvoker(target));
49          }
50           return  service;
51      }
52 
53       /*  (non-Javadoc)
54       * @see org.springframework.beans.factory.FactoryBean#getObjectType()
55        */
56       public  Class getObjectType()
57      {
58          
59           return  Service. class ;
60      }
61 
62       /*  (non-Javadoc)
63       * @see org.springframework.beans.factory.FactoryBean#isSingleton()
64        */
65       public   boolean  isSingleton()
66      {
67           return   true ;
68      }
69 
70       public   void  setServiceClass(Class serviceClass)
71      {
72           this .serviceClass  =  serviceClass;
73      }
74 
75       public   void  setTarget(Object target)
76      {
77           this .target  =  target;
78      }
79 
80  }
81 

这样我们可以通过Spring来装配一个pojo,

下一步我们要在Spring容器载入时候注册XFire Service,

并启动一个嵌入的Http Server,我们可以借助Spring的ApplicationListener来实现

 
 1  /**
 2   * 
 3    */
 4  package  com.yovn.ws.xfire.example;
 5 
 6 
 7  import  org.apache.commons.logging.Log;
 8  import  org.apache.commons.logging.LogFactory;
 9  import  org.codehaus.xfire.XFire;
10  import  org.codehaus.xfire.XFireFactory;
11  import  org.codehaus.xfire.server.http.XFireHttpServer;
12  import  org.codehaus.xfire.service.Service;
13  import  org.codehaus.xfire.service.ServiceRegistry;
14  import  org.springframework.context.ApplicationContext;
15  import  org.springframework.context.ApplicationEvent;
16  import  org.springframework.context.ApplicationListener;
17  import  org.springframework.context.event.ContextClosedEvent;
18  import  org.springframework.context.event.ContextRefreshedEvent;
19 
20  /**
21   *  @author  new
22   * 
23    */
24  public   class  XFireServiceStarter  implements  ApplicationListener
25  {
26 
27       private   int  port  =   80 ;
28 
29       private  XFireHttpServer server;
30       private   final  Log logger = LogFactory.getLog(getClass().getName());
31 
32       public   void  setPort( int  port)
33      {
34           this .port  =  port;
35      }
36 
37       public   void  onApplicationEvent(ApplicationEvent event)
38      {
39           try
40          {
41               if  (event  instanceof  ContextRefreshedEvent)
42              {
43 
44                   if  (server  !=   null )
45                  {
46 
47                      server.stop();
48                      logger.info( " xfire server stopped " );
49 
50                  }
51                  registerService((ApplicationContext)event.getSource());
52                  server  =   new  XFireHttpServer();
53                  server.setPort(port);
54                  server.start();
55                  logger.info( " xfire server started " );
56 
57              }  else   if  (event  instanceof  ContextClosedEvent)
58              {
59                    if (server != null )
60                   {
61                       server.stop();
62                       logger.info( " xfire server stopped " );
63                   }
64                   
65              }
66 
67          }  catch  (Exception e)
68          {
69              logger.error( " process event  " + event + "  error " ,e);
70          }
71 
72      }
73 
74       private   void  registerService(ApplicationContext context)
75      {
76          XFire xfire = XFireFactory.newInstance().getXFire();
77          ServiceRegistry registry = xfire.getServiceRegistry();
78          String names[] = context.getBeanNamesForType(Service. class );
79          
80           for ( int  i = 0 ;i < names.length;i ++ )
81          {
82              Service service = (Service)context.getBean(names[i]);
83              registry.register(service);
84              logger.info( " register service: " + service.getName());
85          }
86          
87      }
88 
89  }
90 

Ok,我们完成基础的代码,下面试着发布一个简单的WebServie
 
1  package  com.yovn.ws.xfire.example;
2 
3  public   interface  Add
4  {
5       int  add( int  a, int  b);
6 
7  }
该接口的实现如下
 1  package  com.yovn.ws.xfire.example;
 2 
 3  public   class  AddImpl  implements  Add
 4  {
 5 
 6       public   int  add( int  a,  int  b)
 7      {
 8          
 9           return  a + b;
10      }
11 
12  }

这是一个简单功能的POJO,下面我们在Spring中装配起来
 1  <? xml version="1.0" encoding="UTF-8" ?>
 2  <! DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd" >
 3  < beans >
 4       < bean  id ="addService"  class ="com.yovn.ws.xfire.example.XFireServiceFactoryBean" >
 5           < property  name ="serviceClass"  value ="com.yovn.ws.xfire.example.Add" />
 6           < property  name ="target"  ref ="adder" />
 7       </ bean >
 8      
 9       < bean  id ="adder"  class ="com.yovn.ws.xfire.example.AddImpl" />
10       < bean  id ="xfireStarter"  class ="com.yovn.ws.xfire.example.XFireServiceStarter" >
11          < property  name ="port"  value ="80" />
12       </ bean >
13  </ beans >

好了,我们完成了,只要载入这个xml初始化一个Spring ApplicationContext,一个名叫Add的webservice就发布了。你可以通过访问 http://localhost/Add?wsdl来获得webservice的详细描述!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值