Browse DevX
Sign up for e-mail newsletters from DevX


Build a SOAP-based Chat Application with Java Web Services, Part 2 : Page 4

In Part 1, you built a fully-functional chat application using the Eclipse IDE and free Web services software from Systinet. In Part 2, use WASP Developer for Eclipse 4.6 to extend your original application with asynchronous messaging.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

The Client
Now that the server side is up and running, it's time for the client. Instead of making a huge jump and having the asynchronous client talking to your server instantly, proceed with smaller steps. Start with a simple synchronous client, that will later evolve into a fully asynchronous beast.

Figure 4. The Web service wizard.

Step I: The Synchronous Client
Use WASP Developer's capabilities to generate working client code from the Web service's WSDL file. The wizard requires only that you specify the service's WSDL description.

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.asyncchat.client, and press the Finish button. Now it's time for the Web Service Wizard. Go to the newly created 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 "Web Service Client from Internet WSDL" item and proceed to the next window.

In the next window, make sure that the com.systinet.asyncchat.client package is selected, and proceed to the next one. Now, fill-in the address of the async chat service's WSDL file. It's:


Press the Next button.

The WSDL compiler utility lets you base your client on any of the service ports found in the WSDL file. You now have the chance to choose one of the services described in this WSDL file. Since the Chat Service has just one, simply press the Finish button. If the wizard asks you to overwrite an existing class, please say No (only the exception class should be existing in your project).

If you get an error message that the Web Service client cannot be created, you probably do not have the server running. Please return to the server, start it, and repeat the client generation steps. You now have created several files in two new packages:

  • com.systinet.asyncchat.client.iface
  • com.systinet.asyncchat.client.iface.struct.
The first package contains the interface definition, ChatService.java, and a support file used by the WASP framework ChatService.xmap. The second package contains additional structures that are used in the ChatService.java interfaceChatMessage.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. The ChatService.java files contains a handful of previously unseen methods, like beginPostMessage or endPostMessage. These methods are used for the asynchronous invocation of the Web service. This step, however, focuses on the synchronous methods instead.

WASP Developer has generated a simple client template. This template is ready for extensions that will do the actual invocation of Web services. If you look at it, the main method contains this code:

ChatService service; String wsdlURI = "http://localhost:6060/ChatServiceAsync/wsdl"; String serviceURI = "http://localhost:6060/ChatServiceAsync/"; // lookup service ServiceClient serviceClient = ServiceClient.create(wsdlURI); serviceClient.setServiceURL(serviceURI); service = ChatService)serviceClient.createProxy(ChatService.class); // now, call the methods on your Web Service interface //eg. service.getMessage();

This code creates a proxy that remotely calls the Web service and assigns it to the service object. For details, please check Part 1 of the tutorial. Let's make the client call our new web service's method synchronously. The source of the extended autogenerated code is in Listing 4.

Figure 5. The runner for the client.

Now it's time to run the initial client. First, create a new runner. Select the ChatServiceClient class, and from the Menu, click the "Run—>Run .." item. 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.

What has just happened? The client first sends a message to the server. Then it reads all messages from the server. In the last step, it waits for new messages to arrive within ten seconds. As there are no new messages on the server, the timeout exception is thrown at the server side and transparently transferred and rethrown on the client side. If you manage to run the client again and run another one within ten seconds, the first one should print the message that the second one sends instead of throwing an exception.

Comment and Contribute






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



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