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/WebApp/controls/webservice/polling/asyncWebService/

Sample Source Code


001 package controls.webservice.polling.asyncWebService;
002 
003 /**
004  * This originally asynchronous web service (copied and modified from 
005  * SamplesApp/WebServices/async/HelloWorldAsych.jws) has been supplemented with a polling
006  * interface. If the client specifies that it cannot hear callbacks (via the 
007  * useCallback parameter on the HelloAsync() method), the result is not sent 
008  * in a callback, instead it is stored in the member variable m_message.
009  
010  * Two methods have been added to the original web service:
011  
012  * checkStatus()
013  * getMessageResponse()
014  
015  * Three member variables have been added:
016  
017  * m_useCallbacks
018  * m_messageIsReceived
019  * m_message
020  
021  * A parameter has been added to the requestMessage() method:
022  
023  * useCallbacks.
024  
025  * A web service that uses a TimerControl to delay sending a response back to the client.
026  *
027  * The timer simulates waiting for a slow back end service to complete work for us.  We
028  * use a callback to asynchronously notify the client when the simulated operation is
029  * complete.
030  */
031 
032 public class HelloWorldAsync implements com.bea.jws.WebService
033 
034     /**
035      * @jc:timer timeout="2 seconds"
036      * @common:control
037      */
038     private com.bea.control.TimerControl helloDelay;
039 
040     /*
041      * This member variable stores the client choice to be sent a callback or not.
042      */
043     public boolean m_useCallback;
044 
045     /*
046      * When the callback handler is fired, this boolean is set to true.
047      * Clients that don't want callbacks check this boolean to see if their result is ready.
048      */
049     public boolean m_messageIsReceived;
050     
051     /*
052      * This member variable stores the result for clients that don't want callbacks
053      */
054     public String m_message = "";
055 
056   
057     /**
058      <p>callback is the variable that represents the client connection.  It's used 
059      * to enable our service to send messages back to the client asynchronously.
060      * callback is type Callback, which is defined next;</p>
061      */
062     public Callback callback; 
063 
064     /**
065      <p>the Callback interface is the definition of which messages the client will 
066      * accept from us via the callback variable.</p>
067      */
068     public interface Callback extends com.bea.control.ServiceControl.Callback
069     
070         /**
071          <p>HelloResult is the message we will send back to the client some time after
072          * the client initiates the operation.</p>
073          
074          <p>We mark the callback as finishing the conversation because once we
075          * invoke the callback the interaction between client and this service is
076          * complete.  If we didn't use the @common:conversation tag here to finish
077          * the conversation, we would have to call finishConversation() on this
078          * service's context object.</p>
079          
080          * @jws:conversation phase="finish"
081          */ 
082         public void onHelloResult(String hello);
083     
084 
085     /**
086      <p>The client starts the interaction by calling HelloAsync.</p>
087      *
088      <p>The client sends this, and some time later our web service replies by calling
089      * callback.onHelloResult.</p>
090      
091      <p>Because we need to remember which client called us, this is a "conversation start"
092      * method.  This means the system will automatically track which clients have called
093      * us, and where to send the result for each.  This is known as <i>correlation</i>.</p>
094      
095      <p>Because it uses a TimerControl method that might throw an Exception, the
096      * method must declare that it might throw an Exception.</p>
097      *
098      * @common:operation
099      * @jws:conversation phase="start"
100      */
101     public void requestMessage(boolean useCallback)
102     {
103         m_useCallback = useCallback;    
104       
105         // Start the delay timer.  
106         helloDelay.start();
107         return;
108     }
109     
110     /**
111      * Clients that don't want callbacks call this method to see if the results are ready
112      * to be retrieved.
113      
114      * @common:operation
115      * @jws:conversation phase="continue"
116      */
117     public boolean checkStatus()
118     {
119         return m_messageIsReceived;
120     }
121 
122     /**
123      * Clients that don't want callbacks call this method to get the result
124      
125      * @common:operation
126      * @jws:conversation phase="finish"
127      */
128     public String getMessageResponse()
129     {
130         return m_message;
131     }
132     
133     
134 
135     /**
136      <p>The handler for helloTimer's onTimeout event.</p>
137      *
138      <p>When the timer expires, this method will be invoked.  When that
139      * happens, we'll send the result back to the client who asked for it (if the 
140      * client specifies they want a callback).
141      
142      * If the client has specified no callbacks, then we store the result in the 
143      * member variable m_message.</p>
144      */
145     private void helloDelay_onTimeout(long time)
146     {
147         String hello = "Hello, World!";
148       
149         /*
150          * If the client doesn't want callbacks, store the result in the member variable
151          * m_messageIsReceived.
152          */
153         if(m_useCallback == false)
154         {
155             m_messageIsReceived = true;
156             
157             m_message = hello;
158         }
159         else
160         {   
161         // If the client wants a callback, send the client a hello message.
162         callback.onHelloResult(hello);
163         }
164         
165         // we don't want any more timer events for this conversation.
166         helloDelay.stop();
167         
168         return;
169     }
170   
171