Codehaus XFire
DocumentationQuicklinksDevelopers
Sponsors |
This examples highlights how to
You can also use XFire with Servlets or Spring Remoting, this is just one of your many options with XFire! Creating the ServiceThe first task is to write our Service. Further down we'll be showing how to create a Client. Java Proxies require an interface, and since that is how the Client API works, we'll be writing our interface first. For this example, we're going to create a simple class which echoes back a String: public interface Echo { String echo(String echo); } We'll also need to write an implementation: public class EchoImpl implements Echo { public String echo(String echo) { return echo; } } Next we're going to write a class called ServiceStarter. This class creates a Service from the ServiceFactory and also starts up an instance of the XFireHttpServer. package org.codehaus.xfire.example; import org.codehaus.xfire.XFire; import org.codehaus.xfire.XFireFactory; import org.codehaus.xfire.server.http.XFireHttpServer; import org.codehaus.xfire.service.Service; import org.codehaus.xfire.service.binding.ObjectServiceFactory; import org.codehaus.xfire.service.invoker.BeanInvoker; import org.codehaus.xfire.test.Echo; import org.codehaus.xfire.test.EchoImpl; /** * Creates an Echo service and exposes it via HTTP. */ public class ServiceStarter { XFireHttpServer server; public void start() throws Exception { // Create an XFire Service ObjectServiceFactory serviceFactory = new ObjectServiceFactory(); Service service = serviceFactory.create(Echo.class); service.setInvoker(new BeanInvoker(new EchoImpl())); // Register the service in the ServiceRegistry XFire xfire = XFireFactory.newInstance().getXFire(); xfire.getServiceRegistry().register(service); // Start the HTTP server server = new XFireHttpServer(); server.setPort(8191); server.start(); } public void stop() throws Exception { server.stop(); } } ServiceFactorys are responsible for creating Services and configuring them in XFire. You'll see in other sections that there is an AnnotationServiceFactory as well and you can use that with JSR 181 Annotations. The XFireHttpServer uses Jetty underneath so make sure that you have it on your classpath. If you use maven, here are all the dependencies you'll need: <dependencies> <dependency> <groupId>org.codehaus.xfire</groupId> <artifactId>xfire-java5</artifactId> <version>1.2.2</version> </dependency> <dependency> <groupId>jetty</groupId> <artifactId>org.mortbay.jetty</artifactId> <version>5.1.10</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.4</version> </dependency> </dependencies> Starting the service and the ClientUsing the Client is very similar. Once again we'll are creating a Service from an ObjectServiceFactory. This is because the Service object holds metadata about our Service. Our client needs this metadata to understand how the Service works. (NOTE: we also support WSDL code generation and dynamic clients if you didn't write the service or don't have the interface on your classpath). The XFireProxyFactory is then responsible for creating a proxied interface which will invoke this service. ServiceStarter starter = new ServiceStarter(); starter.start(); // Create a service model for the client ObjectServiceFactory serviceFactory = new ObjectServiceFactory(); Service serviceModel = serviceFactory.create(Echo.class); // Create a client proxy XFireProxyFactory proxyFactory = new XFireProxyFactory(); Echo echo = (Echo) proxyFactory.create(serviceModel, "http://localhost:8191/Echo"); System.out.println(echo.echo("Hello World")); starter.stop(); At the end, all we need to do is invoke "echo.echo("Hello World")" and "Hello World" should be sent right back to you! If you were going to create a client from .NET you would be able to find the WSDL at http://localhost:8191/Echo?wsdl. |