HelloWorldAsync.jws Sample

This topic inludes the source code for the HelloWorldAsync.jws Sample.

Sample Location

This sample is located in the following directory in your WebLogic Workshop installation:

BEA_HOME/weblogic81/samples/workshop/SamplesApp/WebServices/async/

Sample Source Code


01 package async;
02 
03 /**
04  *<p> A web service that uses a TimerControl to delay sending a response back to the client.</p>
05  *
06  <p>The timer simulates waiting for a slow back end service to complete work for us.  We
07  * use a callback to asynchronously notify the client when the simulated operation is
08  * complete.</p>
09  * @common:target-namespace namespace="http://workshop.bea.com/HelloWorldAsync"
10  */
11 public class HelloWorldAsync implements com.bea.jws.WebService
12 
13     /**
14      * @jc:timer timeout="10 seconds"
15      * @common:control
16      */
17     private com.bea.control.TimerControl helloDelay;
18 
19     /**
20      <p>callback is the variable that represents the client connection.  It's used 
21      * to enable our service to send messages back to the client asynchronously.
22      * callback is type Callback, which is defined next;</p>
23      */
24     public Callback callback; 
25 
26     /**
27      <p>the Callback interface is the definition of which messages the client will 
28      * accept from us via the callback variable.</p>
29      */
30     public interface Callback 
31     
32         /**
33          <p>HelloResult is the message we will send back to the client some time after
34          * the client initiates the operation.</p>
35          
36          <p>We mark the callback as finishing the conversation because once we
37          * invoke the callback the interaction between client and this service is
38          * complete.  If we didn't use the @common:conversation tag here to finish
39          * the conversation, we would have to call finishConversation() on this
40          * service's context object.</p>
41          
42          * @jws:conversation phase="finish"
43          */ 
44         public void onHelloResult(String hello);
45     
46 
47     /**
48      <p>The client starts the interaction by calling HelloAsync.</p>
49      *
50      <p>The client sends this, and some time later our service replies by calling
51      * callback.onHelloResult.</p>
52      
53      <p>Because we need to remember which client called us, this is a "conversation start"
54      * method.  This means the system will automatically track which clients have called
55      * us, and where to send the result for each.  This is known as <i>correlation</i>.</p>
56      
57      * @common:operation
58      * @jws:conversation phase="start"
59      */
60     public void HelloAsync()
61     {
62         // all we do here is start the timer.  
63         helloDelay.start();
64         return;
65     }
66 
67     /**
68      <p>The handler for helloTimer's onTimeout event.</p>
69      *
70      <p>When the timer expires, this method will be invoked.  When that
71      * happens, we'll send the result back to the client who asked for it.</p>
72      */
73     private void helloDelay_onTimeout(long time)
74     {
75         // send the client a hello message.
76         callback.onHelloResult("Hello, asynchronous world");
77         
78         // we don't want any more timer events for this conversation.
79         helloDelay.stop();
80         
81         return;
82     }
83   
84