Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Build a SOAP-based Chat Application with Java Web Services : Page 3

This introduction to Web services eschews the canonical "stockQuote" service in favor of a more complex and more informative SOAP-based chat application. Learn how to develop a fully-functional application using the Eclipse IDE and free Web services software from Systinet.


advertisement
SOAP Enabling the Chat Server
Now you have a working implementation of a simple chat server. To make it talk SOAP, deploy it to the Web services server using the runtime publishing method.

Start the server at the specified address and register the service under a path on the server. The server address + service path make up the complete URI of the Web service.

Here's the code of the application class that starts the SOAP server and registers the chat service as a Web service. This source file should (obviously) be saved as ChatServerApp.java.



package com.systinet.simplechat.server.impl; import org.systinet.wasp.*; import org.systinet.wasp.webservice.Registry; public class ChatServerApp { public static void main(String[] args) throws Exception { Wasp.startServer("http://localhost:6060"); Registry.publish("/ChatService/", ChatService.class); System.out.println("The Chat server is up and running."); } }

It's that simple. Just two lines of code:

Wasp.startServer("http://localhost:6060"); Registry.publish("/ChatService/", ChatService.class);

The WASP.startServer method starts the server inside a Java application. The Registry.publish method programmatically deploys the ChatService class as a Web service. The URI of this service is composed of the address of the server and of the service's path on the server. WASP also automatically creates the WSDL document describing the service and publishes it at SERVICE_URI/wsdl.

 
Figure 2: Running the server.

Build and Run the Chat Server
The chat server is ready for running now. After the build is complete (Project -> Build Add), you can start it. First, create a Web service runner. This runner ensures that the server has all the required libraries and that it is properly configured. To create the runner, select the ChatServerApp from the com.systinet.simplechat.server.impl package and choose Run -> Run... from the IDE menu.

The dialog that opens is the manager for all runners in the project. Double click on the WASP Java Application runner template. A new runner called ChatServerApp is created. Always use this runner to run the ChatServerApp class. Press the Run button to start the chat server.

The server is up and running, ready to speak SOAP. Its location is http://localhost:6060/ChatService/. The location of the dynamically generated WSDL document is http://localhost:6060/ChatService/wsdl.

The business logic is now deployed to the server programmatically using the Runtime API.

 
Figure 3: Here's the output of the running server.

The Client
Now, you need a client that consumes the ChatService.

Pretend that all you know about the service is the location of its WSDL description: http://localhost:6060/ChatService/wsdl. You do not have access to the original Web service's source code. You don't even know in what language it is implemented. You also don't know the exact location of the ChatService, but you do know that every Web service is completely described in its WSDL document.

The server creates a proxy which hides all the SOAP and transports plumbing from developers, and lets them invoke Web services with simple method calls. In order to work, this proxy has to know the location (and therefore the content) of the Web service's WSDL description, the URI of the Web Service, and its Java interface. You know the WSDL location of the ChatService and, as it happens, the URI of the service is contained in this WSDL. Now all you need is to get the service's Java interface. Creating it manually means understanding the WSDL and XML Schema very well, and even then it would still be a daunting task. Fortunately, WASP Developer contains a tool that generates the Java interface of a Web service from its WSDL. This tool, called WSDL2Java, is available both as a command line utility and as an ANT task, so it is very easy to integrate with builds that use ANT.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date