Defining a web service returning no value

This recipe explains how to define a web service that will return no value. We distinguish between two types of web service operations that return no value. The first one is synchronous and is where a client waits for a web service operation to finish; however, no content result is expected. This is also called one-way. The second one is a web service operation that is performed in an asynchronous way, where a client does not wait for the response. The content result is either not expected or is handled in a different part of the client code. This type of communication is called asynchronous pooling or callback. The JAX-WS also supports request-response communication as message exchange protocol (MEP). However, this type of communication is used for the methods that return a value.

Getting ready

In this recipe, we will amend the example from the Creating literal and encoded web services recipe.

How to do it…

We will prepare one method (reserveBook) that can be used in asynchronous communication with the client where a book is reserved but the client does not expect the response or checks the status of the book in a different part of the code.

Open the BookLibraryImpl class and browse to the reserveBook method. Annotate the method with the @Oneway annotation as shown in the following code:

@Oneway
@WebMethod
public void reserveBook(int member_id,
                        int book_id) throws BookAlreadyBorrowed {

How it works…

We already have methods in the code that return no value, such as borrowBook and giveBackBook.

For the borrowBook method, let's inspect the WSDL document. The operation is defined as follows:

<operation name="borrowBook">
  <input message="tns:borrowBook"/>
  <output message="tns:borrowBookResponse"/>
  <fault message="tns:BookAlreadyBorrowed" name="BookAlreadyBorrowed"/>
</operation>

The operation has a definition for the input message, output message, and fault message. The definition for the output message is as shown in the following code:

<xs:complexType name="borrowBookResponse">
  <xs:sequence/>
</xs:complexType>

The output message is empty, meaning that we will receive no data from the web service operation on the response. The borrowBook method is already prepared for synchronous communication where a client will wait for the web service call to finish; however, no response data is expected. We can use such a design for the scenarios, where the web service finishes quickly and to also check that the web service was executed successfully.

Let us now inspect the reserveBook web service operation. The WSDL definition of the operations is as shown in the following code:

<operation name="reserveBook">
  <input message="tns:reserveBook"/>
  
</operation>

In this case, only the input is defined and no output messages. This web service operation is prepared for asynchronous communication where the client will not wait for a web service operation to finish or the response is handled elsewhere in the code. In any case, the client does not expect any data from the web service operation.

See also

To learn more about cases where data from a web service operation call is utilized, refer to our next recipe.

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

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