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 4

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

 
Figure 4: Creating the client package.

The Client's Code
First, create a new package for the client. From the menu, choose the File -> New -> Package. In the New Package dialog enter the package name com.systinet.simplechat.client, and press the finish button.

WASP Developer requires the WSDL file to be present in the project in order to generate the client classes. Download the Chat Service's WSDL document into the project. Select the com.systinet.simplechat.client package, and from the menu choose File -> New -> Other.... The New dialog now opens. In the left hand bar, choose Systinet Web Services. A set of all available wizards is displayed in the right hand bar. Choose the WSDL from Internet item and proceed to the next window.



In the second window fill in the location of the WSDL file and chose Next. The ChatService's WSDL file is located at http://localhost:6060/ChatService/wsdl.

In the last window, fill in the name of the downloaded WSDL file. Set it to ChatService and click finish.

The WSDL file is now in your project. In order to generate the client classes chose the Generate Client... item from its context menu.

When the Generate Client from WSDL wizard opens, leave all values set to their defaults and finish the wizard.

No you have created several files in two new packages; com.systinet.simplechat.client.iface and com.systinet.simplechat.client.iface.struct. The former package contains the interface definition, ChatService.java, and a support file used by the WASP framework ChatService.xmap. The latter package contains additional structures that are used in the ChatService.java interface—ChatMessage.java and ChatPerson.java. If you inspect the generated classes, they should be familiar to you. They are not exact copies of the original ChatService interface classes, but they exactly represent its API.

There's one more class that WASP Developer has created from the WSDL file. Look at the com.systinet.simplechat.client package. There's one new file there, ChatServiceClient.java. This is a generated skeleton of the client application. Listing 1 shows the class in it's entirety.

First, a couple of WASP classes are imported, then you see the imports for the ChatService interface and structures that were generated from the WSDL file. The interesting part of the code is in the main() method:


        ServiceClient serviceClient = ServiceClient.create
(wsdlURI, ChatService.class);
        serviceClient.setServiceURL(serviceURI);
        serviceClient.setWSDLServiceName(new QName
("http://systinet.com/wsdl/com/systinet/simplechat/server/impl/", "ChatService"));
        serviceClient.setWSDLPortName("ChatService");
        service = (ChatService) Registry.lookup(serviceClient);
These lines create the proxy that the client uses for remote invocation of the Chat Service. WASP Developer generates a little more code than the simple client requires. Because WSDL files can contain information about multiple Web services, the generated code is a little bit more complicated so that it works with any WSDL file. However, the following single line of code would work the same way for our simple case:


service = (ChatService) Registry.lookup
("http://localhost:6060/ChatService/wsdl/", ChatService);
The service object is now ready for calls. Every call on its methods results in remote invocation of the chat service. Finish the client with calls to the chat service. Listing 2 shows the extended, auto-generated code.

 
Figure 5: Here's the chat client's output.

Build and Run the Client
Now that the client is implemented you need to build and run it. To build the code, run Project -> Build All command from the menu.

To run the client, you'll need its Web Service Runner (see above for details).

Select the ChatServiceClient class in the project and from the menu chose Run -> Run.... Double click on the WASP Java Application item. A new runner called ChatServiceClient is created and ready for use with the client. Always use this runner for running the client. Press the Run button to start the client.

The client connects to the Chat Service and calls two of its methods. The output into the console after two executions of the client should look like Figure 5.

Until Next Time
In this first tutorial, you've gotten an explanation of the standards that underpin Web services, and learned how to expose an existing Java application as a Web service. In the next tutorial, you'll examine Web services security using the same application.



Petr Dvorak is the Director of Product Development at Systinet Corporation. Reach him at petr.dvorak@systinet.com.
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