Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

XFire: The easy and simple way to develop Web services

Expose your POJO methods as Web services

  • Print
  • Feedback

Page 4 of 6

But this test is not enough. Situations may arise where you could see the WSDL, yet the service may not be accessible from clients. So to verify whether the service is up, we must go for a real test using a client program that makes an actual call to the service.

Developing a client

You can create clients with any SOAP tool, such as .Net or Apache Axis, in a variety of ways: using stubs generated from WSDL, using dynamic proxy, etc. In our example, we use a dynamic proxy in a simple servlet named WsClient.java. To keep coding efforts minimum, all screen building elements are put inside the doGet() method. The actual call to the Web service is made in the callWebService() method, which is pretty simple. It looks like this:

 /* Call the Web service
    *
    */
    public String callWebService(
        String fromAccount, String toAccount, double amount, String currency) 
        throws MalformedURLException, Exception {
        
        //Create a metadata of the service      
        Service serviceModel = new ObjectServiceFactory().create(IBankingService.class);        
        log.debug("callSoapServiceLocal(): got service model." );
   
        //Create a proxy for the deployed service
        XFire xfire = XFireFactory.newInstance().getXFire();
        XFireProxyFactory factory = new XFireProxyFactory(xfire);      
    
        String serviceUrl = "http://localhost:8080/websvc/services/Banking";
        
        IBankingService client = null;
        try {
            client = (IBankingService) factory.create(serviceModel, serviceUrl);
        } catch (MalformedURLException e) {
            log.error("WsClient.callWebService(): EXCEPTION: " + e.toString());
        }    
               
        //Invoke the service
        String serviceResponse = "";
        try { 
            serviceResponse = client.transferFunds(fromAccount, toAccount, amount, currency);
       } catch (Exception e){
            log.error("WsClient.callWebService(): EXCEPTION: " + e.toString());                 
            serviceResponse = e.toString();
        }        
        log.debug("WsClient.callWebService(): status=" + serviceResponse);            

//Return the response return serviceResponse; }



What's going on in this code? Let me explain: First, we create a service model, which contains the service specifications—in other words, the service's metadata. We create this model from the interface IBankingService.class using XFire's ObjectServiceFactory.

The next step is to get a proxy factory object for XFire, which involves routine code, and is pretty simple and straightforward. There is nothing application-specific in this step. From this proxyFactory, using the service model and the service endpoint URL (used to get the WSDL), we get a local proxy for the service.

That's it. This proxy is the actual client. We can now invoke its transferFunds() method to get the Web service we want.

Once the example application is deployed and started, try the servlet URL: http://localhost:8080/websvc/ws.

The servlet uses default parameters to call the Web service and displays the response received. The last two lines of the page should read:

 Response Received
COMPLETED: CDN$ 500.00 was successfully transferred from A/C# 11111-01234 to A/C# 99999-05678



Now you can be sure that the Web service is up and running.

  • Print
  • Feedback

Resources