目录
- 构建项目
- 创建服务端API
- 嵌入式HTTP服务器发布服务
- 创建客户端
[一]、构建项目
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:
2 | < groupId >org.codehaus.xfire</ groupId > |
3 | < artifactId >xfire-java5</ artifactId > |
4 | < version >1.2.6</ version > |
9 | < groupId >org.mortbay.jetty</ groupId > |
10 | < artifactId >jetty</ artifactId > |
11 | < version >6.1.26</ version > |
13 | < scope >compile</ scope > |
16 | < groupId >javax.servlet</ groupId > |
17 | < artifactId >servlet-api</ artifactId > |
18 | < version >2.4</ version > |
20 | < scope >compile</ scope > |
如果不是用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; |
8 | public interface HelloEchoService { |
10 | String echo(String echo); |
接口的实现:HelloEchoServiceImpl.java
1 | package com.micmiu.xfire.demo.embedded; |
8 | public class HelloEchoServiceImpl implements HelloEchoService { |
10 | public String echo(String echo) { |
[三]、嵌入式HTTP服务器发布服务
服务发布代码:ServiceStarter.java
1 | package com.micmiu.xfire.demo.embedded; |
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; |
20 | public class ServiceStarter { |
22 | XFireHttpServer server; |
24 | public void start() throws Exception { |
26 | XFire xfire = XFireFactory.newInstance().getXFire(); |
29 | ObjectServiceFactory serviceFactory = new ObjectServiceFactory( |
30 | xfire.getTransportManager()); |
42 | Service service = serviceFactory.create(HelloEchoService. class ); |
47 | service.setProperty(ObjectInvoker.SERVICE_IMPL_CLASS, |
48 | HelloEchoServiceImpl. class ); |
51 | xfire.getServiceRegistry().register(service); |
54 | System.out.println( " ----> XFire embedded HTTP Server start <---- " ); |
55 | server = new XFireHttpServer(); |
60 | public void stop() throws Exception { |
67 | public static void main(String[] args) { |
69 | System.out.println( " ----> 服务发布 。。。 <---- " ); |
70 | ServiceStarter service = new ServiceStarter(); |
72 | } catch (Exception e) { |
运行日志:
----> 服务发布 。。。 <----
----> 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 SocketConnector@0.0.0.0:8090
浏览器输入:http://localhost:8090 和 http://localhost:8090/HelloEchoService?wsdl 回车:
[四]、创建客户端
编写客户端测试代码:ClientTest.java
1 | package com.micmiu.xfire.demo.embedded; |
3 | import org.codehaus.xfire.client.XFireProxyFactory; |
4 | import org.codehaus.xfire.service.Service; |
5 | import org.codehaus.xfire.service.binding.ObjectServiceFactory; |
12 | public class ClientTest { |
17 | public static void main(String[] args) { |
19 | System.out.println( " ----> 初始客户端 <---- " ); |
21 | ObjectServiceFactory serviceFactory = new ObjectServiceFactory(); |
22 | Service serviceModel = serviceFactory |
23 | .create(HelloEchoService. class ); |
26 | XFireProxyFactory proxyFactory = new XFireProxyFactory(); |
27 | HelloEchoService client = (HelloEchoService) proxyFactory.create( |
30 | System.out.println(client.echo( "Hi,welcome to www.micmiu.com" )); |
31 | System.out.println( " ----> 客户端调用结束 <---- " ); |
32 | } catch (Exception e) { |
运行日志:
----> 初始客户端 <----
Hi,welcome to www.micmiu.com
----> 客户端调用结束 <----
到此示例基本结束。