目录
- 构建项目
- 创建服务端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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<dependency> <groupId>org.codehaus.xfire</groupId> <artifactId>xfire-java5</artifactId> <version>1.2.6</version> <type>jar</type> <scope>compile</scope> </dependency> <dependency> <groupId>org.mortbay.jetty</groupId> <artifactId>jetty</artifactId> <version>6.1.26</version> <type>jar</type> <scope>compile</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.4</version> <type>jar</type> <scope>compile</scope> </dependency> |
如果不是用maven构建项目,下面给出示例项目所有依赖的lib详细列表:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
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 2 3 4 5 6 7 8 9 10 11 12 |
package com.micmiu.xfire.demo.embedded; /** * * @blog http://www.micmiu.com * @author Michael */ public interface HelloEchoService { String echo(String echo); } |
接口的实现:HelloEchoServiceImpl.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package com.micmiu.xfire.demo.embedded; /** * * @blog http://www.micmiu.com * @author Michael */ public class HelloEchoServiceImpl implements HelloEchoService { public String echo(String echo) { return echo; } } |
[三]、嵌入式HTTP服务器发布服务
服务发布代码:ServiceStarter.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
package com.micmiu.xfire.demo.embedded; import org.codehaus.xfire.XFire; import org.codehaus.xfire.XFireFactory; import org.codehaus.xfire.aegis.AegisBindingProvider; import org.codehaus.xfire.aegis.type.Configuration; import org.codehaus.xfire.aegis.type.DefaultTypeMappingRegistry; import org.codehaus.xfire.annotations.AnnotationServiceFactory; import org.codehaus.xfire.server.http.XFireHttpServer; import org.codehaus.xfire.service.Service; import org.codehaus.xfire.service.binding.BindingProvider; import org.codehaus.xfire.service.binding.ObjectServiceFactory; import org.codehaus.xfire.service.invoker.ObjectInvoker; /** * * @blog http://www.micmiu.com * @author Michael */ public class ServiceStarter { XFireHttpServer server; public void start() throws Exception { XFire xfire = XFireFactory.newInstance().getXFire(); // Create an XFire Service ObjectServiceFactory serviceFactory = new ObjectServiceFactory( xfire.getTransportManager()); // changer default configuration // Configuration configuration = new Configuration(); // configuration.setDefaultMinOccurs(1); // configuration.setDefaultNillable(false); // DefaultTypeMappingRegistry tmr = new DefaultTypeMappingRegistry(null, // true, configuration); // AegisBindingProvider bind = new AegisBindingProvider(tmr); // ObjectServiceFactory serviceFactory = new ObjectServiceFactory( // xfire.getTransportManager(), bind); Service service = serviceFactory.create(HelloEchoService.class); // Service service = serviceFactory.create(HelloEchoService.class, // "HelloEchoService", null, null); // service.setInvoker(new BeanInvoker(new HelloEchoServiceImpl())); service.setProperty(ObjectInvoker.SERVICE_IMPL_CLASS, HelloEchoServiceImpl.class); // Register the service in the ServiceRegistry xfire.getServiceRegistry().register(service); // Start the HTTP server System.out.println(" ----> XFire embedded HTTP Server start <---- "); server = new XFireHttpServer(); server.setPort(8090); server.start(); } public void stop() throws Exception { server.stop(); } /** * @param args */ public static void main(String[] args) { try { System.out.println(" ----> 服务发布 。。。 <---- "); ServiceStarter service = new ServiceStarter(); service.start(); } catch (Exception e) { e.printStackTrace(); } } } |
运行日志:
1 2 3 4 5 |
----> 服务发布 。。。 <---- ----> 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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
package com.micmiu.xfire.demo.embedded; import org.codehaus.xfire.client.XFireProxyFactory; import org.codehaus.xfire.service.Service; import org.codehaus.xfire.service.binding.ObjectServiceFactory; /** * * @blog http://www.micmiu.com * @author Michael */ public class ClientTest { /** * @param args */ public static void main(String[] args) { try { System.out.println(" ----> 初始客户端 <---- "); // Create a service model for the client ObjectServiceFactory serviceFactory = new ObjectServiceFactory(); Service serviceModel = serviceFactory .create(HelloEchoService.class); // Create a client proxy XFireProxyFactory proxyFactory = new XFireProxyFactory(); HelloEchoService client = (HelloEchoService) proxyFactory.create( serviceModel, "http://localhost:8090/HelloEchoService"); System.out.println(client.echo("Hi,welcome to www.micmiu.com")); System.out.println(" ----> 客户端调用结束 <---- "); } catch (Exception e) { e.printStackTrace(); } } } |
运行日志:
1 2 3 |
----> 初始客户端 <---- Hi,welcome to www.micmiu.com ----> 客户端调用结束 <---- |
到此示例基本结束。
原创文章,转载请注明: 转载自micmiu – 软件开发+生活点滴[ http://www.micmiu.com/ ]
本文链接地址: http://www.micmiu.com/soa/webservice/xfire-embedded-http-server/
0 条评论。