Defining a one or two way web service

This recipe explains how to define a one or two way web service. You might think that the title is misguided since all methods are by default exposed as two way web services. With that in mind, we will focus more on defining a one way web service.

How to do it…

We will define the OutputTransactions method as a one way operation of a web service. Open the CCGatePortType class in JDeveloper. Put the @OneWay annotation next to the OutputTransaction method:

@WebMethod
@Oneway
public void OutputTransactions() throws RemoteException;

That is it. With this annotation, the web service operation will become a one way operation, meaning the client will not wait for the response.

How it works…

The best way to see how the one way annotation works is through the WSDL document web service using expose operations. If we have default behavior (that is, two way web service operations), there is the following signature of operation in the WSDL document:

<operation name="OutputTransactions">
  <input message="tns:OutputTransactions"/>
  <output message="tns:OutputTransactionsResponse"/>
</operation>

We see that in both request and response message definitions, even the method itself does not return anything, as in two-way mode, the client waits for a web service response. We modified the code of the OutputTransactions method so that it sleeps for 5 seconds before printing the transaction. This is for testing purposes so that we can show how one way invocation works. Let us invoke the OutputTransaction operation and observe the web server console window. The client window is seen as follows:

How it works…

The output from the server console shows:

-----------------------------
Performed transactions:  Mon Jul 01 16:44:56 CEST 2013
-----------------------------

We can see that the response time took 5042 ms, which means that the client waited the while whole time the web service was processing a request.

Now let us try the same with the @OneWay annotated web service. The execution of the OutputTransaction operation shows:

How it works…

The server console output now shows the following:

-----------------------------
Performed transactions:  Mon Jul 01 16:51:49 CEST 2013
-----------------------------

The client request now lasts 12 ms and the processing of the client is continued.

Note

Note that the @OneWay annotation should not be mixed with the asynchronous operation of a web service. Clients calling @OneWay annotated web service operations do not expect any response. On the other hand, clients calling asynchronous web service operations do expect a response in some part of the code. The request is either queued or somehow stored for later reference. This usually happens with long-running calls and on systems with heavy loads.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset