运用XFire的嵌入式HTTP服务器发布服务

目录

  1. 构建项目
  2. 创建服务端API
  3. 嵌入式HTTP服务器发布服务
  4. 创建客户端

[一]、构建项目

maven构建项目:

1 mvn archetype:create -DgroupId=com.micmiu.xfire.demo -DartifactId=xfire-embedded-demo -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

再执行:mvn eclipse:eclipse ,转化为eclipse项目并导入。

修改POM.xml 文件增加依赖lib:

1 <dependency>
2     <groupId>org.codehaus.xfire</groupId>
3     <artifactId>xfire-java5</artifactId>
4     <version>1.2.6</version>
5     <type>jar</type>
6     <scope>compile</scope>
7 </dependency>
8 <dependency>
9     <groupId>org.mortbay.jetty</groupId>
10     <artifactId>jetty</artifactId>
11     <version>6.1.26</version>
12     <type>jar</type>
13     <scope>compile</scope>
14 </dependency>
15 <dependency>
16     <groupId>javax.servlet</groupId>
17     <artifactId>servlet-api</artifactId>
18     <version>2.4</version>
19     <type>jar</type>
20     <scope>compile</scope>
21 </dependency>

如果不是用maven构建项目,下面给出示例项目所有依赖的lib详细列表:

 activation-1.1.jar
 ant-1.5.jar
 commons-attributes-api-2.1.jar
 commons-beanutils-1.7.0.jar
 commons-codec-1.3.jar
 commons-httpclient-3.0.jar
 commons-logging-1.0.4.jar
 jaxen-1.1-beta-9.jar
 jdom-1.0.jar
 jetty-6.1.26.jar
 jetty-util-6.1.26.jar
 junit-3.8.1.jar
 mail-1.4.jar
 qdox-1.5.jar
 servlet-api-2.4.jar
 servlet-api-2.5-20081211.jar
 stax-api-1.0.1.jar
 stax-utils-20040917.jar
 wsdl4j-1.6.1.jar
 wstx-asl-3.2.0.jar
 xercesImpl-2.6.2.jar
 xfire-aegis-1.2.6.jar
 xfire-annotations-1.2.6.jar
 xfire-core-1.2.6.jar
 xfire-java5-1.2.6.jar
 xfire-jsr181-api-1.0-M1.jar
 xmlParserAPIs-2.6.2.jar
 XmlSchema-1.1.jar

[二]、创建服务端API

编码服务接口:HelloEchoService.java

1 package com.micmiu.xfire.demo.embedded;
2  
3 /**
4  *
5  * @blog http://www.micmiu.com
6  * @author Michael
7  */
8 public interface HelloEchoService {
9  
10     String echo(String echo);
11  
12 }

接口的实现:HelloEchoServiceImpl.java

1 package com.micmiu.xfire.demo.embedded;
2  
3 /**
4  *
5  * @blog http://www.micmiu.com
6  * @author Michael
7  */
8 public class HelloEchoServiceImpl implements HelloEchoService {
9  
10     public String echo(String echo) {
11         return echo;
12     }
13  
14 }

[三]、嵌入式HTTP服务器发布服务

服务发布代码:ServiceStarter.java

1 package com.micmiu.xfire.demo.embedded;
2  
3 import org.codehaus.xfire.XFire;
4 import org.codehaus.xfire.XFireFactory;
5 import org.codehaus.xfire.aegis.AegisBindingProvider;
6 import org.codehaus.xfire.aegis.type.Configuration;
7 import org.codehaus.xfire.aegis.type.DefaultTypeMappingRegistry;
8 import org.codehaus.xfire.annotations.AnnotationServiceFactory;
9 import org.codehaus.xfire.server.http.XFireHttpServer;
10 import org.codehaus.xfire.service.Service;
11 import org.codehaus.xfire.service.binding.BindingProvider;
12 import org.codehaus.xfire.service.binding.ObjectServiceFactory;
13 import org.codehaus.xfire.service.invoker.ObjectInvoker;
14  
15 /**
16  *
17  * @blog http://www.micmiu.com
18  * @author Michael
19  */
20 public class ServiceStarter {
21  
22     XFireHttpServer server;
23  
24     public void start() throws Exception {
25  
26         XFire xfire = XFireFactory.newInstance().getXFire();
27  
28         // Create an XFire Service
29         ObjectServiceFactory serviceFactory = new ObjectServiceFactory(
30                 xfire.getTransportManager());
31  
32         // changer default configuration
33 //      Configuration configuration = new Configuration();
34 //      configuration.setDefaultMinOccurs(1);
35 //      configuration.setDefaultNillable(false);
36 //      DefaultTypeMappingRegistry tmr = new DefaultTypeMappingRegistry(null,
37 //              true, configuration);
38 //      AegisBindingProvider bind = new AegisBindingProvider(tmr);
39 //      ObjectServiceFactory serviceFactory = new ObjectServiceFactory(
40 //              xfire.getTransportManager(), bind);
41  
42         Service service = serviceFactory.create(HelloEchoService.class);
43 //      Service service = serviceFactory.create(HelloEchoService.class,
44 //              "HelloEchoService", null, null);
45  
46         // service.setInvoker(new BeanInvoker(new HelloEchoServiceImpl()));
47         service.setProperty(ObjectInvoker.SERVICE_IMPL_CLASS,
48                 HelloEchoServiceImpl.class);
49  
50         // Register the service in the ServiceRegistry
51         xfire.getServiceRegistry().register(service);
52  
53         // Start the HTTP server
54         System.out.println(" ----> XFire embedded HTTP Server start  <---- ");
55         server = new XFireHttpServer();
56         server.setPort(8090);
57         server.start();
58     }
59  
60     public void stop() throws Exception {
61         server.stop();
62     }
63  
64     /**
65      * @param args
66      */
67     public static void main(String[] args) {
68         try {
69             System.out.println(" ----> 服务发布 。。。 <---- ");
70             ServiceStarter service = new ServiceStarter();
71             service.start();
72         catch (Exception e) {
73             e.printStackTrace();
74         }
75     }
76  
77 }

运行日志:

 ----> 服务发布 。。。 <----
 ----> XFire embedded HTTP Server start  <----
2012-07-30 23:26:00.502:INFO::Logging to STDERR via org.mortbay.log.StdErrLog
2012-07-30 23:26:00.549:INFO::jetty-6.1.26
2012-07-30 23:26:00.623:INFO::Started [email protected]:8090

浏览器输入:http://localhost:8090 和 http://localhost:8090/HelloEchoService?wsdl 回车:

运用XFire的嵌入式HTTP服务器发布服务_第1张图片

[四]、创建客户端

编写客户端测试代码:ClientTest.java

1 package com.micmiu.xfire.demo.embedded;
2  
3 import org.codehaus.xfire.client.XFireProxyFactory;
4 import org.codehaus.xfire.service.Service;
5 import org.codehaus.xfire.service.binding.ObjectServiceFactory;
6  
7 /**
8  *
9  * @blog http://www.micmiu.com
10  * @author Michael
11  */
12 public class ClientTest {
13  
14     /**
15      * @param args
16      */
17     public static void main(String[] args) {
18         try {
19             System.out.println(" ----> 初始客户端 <---- ");
20             // Create a service model for the client
21             ObjectServiceFactory serviceFactory = new ObjectServiceFactory();
22             Service serviceModel = serviceFactory
23                     .create(HelloEchoService.class);
24  
25             // Create a client proxy
26             XFireProxyFactory proxyFactory = new XFireProxyFactory();
27             HelloEchoService client = (HelloEchoService) proxyFactory.create(
28                     serviceModel, "http://localhost:8090/HelloEchoService");
29  
30             System.out.println(client.echo("Hi,welcome to www.micmiu.com"));
31             System.out.println(" ----> 客户端调用结束 <---- ");
32         catch (Exception e) {
33             e.printStackTrace();
34         }
35  
36     }
37 }

运行日志:

 ----> 初始客户端 <----
Hi,welcome to www.micmiu.com
 ----> 客户端调用结束 <----

到此示例基本结束。

你可能感兴趣的:(maven,xfire,WebServie)