PollingController.jpf Sample

This topic inludes the source code for the PollingController.jpf 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/

Sample Source Code


001 package controls.webservice.polling;
002 
003 import com.bea.wlw.netui.pageflow.FormData;
004 import com.bea.wlw.netui.pageflow.Forward;
005 import com.bea.wlw.netui.pageflow.PageFlowController;
006 
007 /**
008  * PollingController.jpf demonstrates how a Page Flow can communicate with a
009  * asynchronous web service by polling the web service.
010  
011  * Polling a web service involves three steps:
012  
013  * (1) Invoking the asynchronous method on the web service.
014  * (2) Waiting a period of time for the web service to process the request.
015  * (3) Invoking another method on the web service to collect the result. 
016  
017  * These three stages are encapsulated in the method requestMessage() below.
018  
019  * This sample also shows how to modify a web service, originally designed to send callbacks, 
020  * so that non-callbackable clients can use the web service. A polling interface has been 
021  * added to the web service (HelloWorldAsync.jws) allowing this Page Flow (which cannot 
022  * hear callbacks from web services) to retrieve data from the web service. The web 
023  * service's polling interface exists alongside the original callback interface in 
024  * the same file.  
025  
026  * Note that the Workshop issues compiler warnings in this file, indicated by green underscoring.
027  * Workshop issues a warning anytime that a Page Flow declares a web service with a callback interface,
028  * because callback interfaces cannot be used by Page Flows. In this case, the compiler warning
029  * should be ignored since the web service contains *both* a callback 
030  * interface (which Page Flows cannot use) and a polling interface (which Page Flows can use).
031  
032  * @jpf:view-properties view-properties::
033  <!-- This data is auto-generated. Hand-editing this section is not recommended. -->
034  <view-properties>
035  <pageflow-object id="pageflow:/controls/webservice/polling/PollingController.jpf"/>
036  <pageflow-object id="action:begin.do">
037  *   <property value="60" name="x"/>
038  *   <property value="60" name="y"/>
039  </pageflow-object>
040  <pageflow-object id="action:requestMessage.do">
041  *   <property value="200" name="x"/>
042  *   <property value="260" name="y"/>
043  </pageflow-object>
044  <pageflow-object id="page:index.jsp">
045  *   <property value="60" name="x"/>
046  *   <property value="260" name="y"/>
047  </pageflow-object>
048  <pageflow-object id="action-call:@page:result.jsp@#@action:begin.do@">
049  *   <property value="164,130,130,96" name="elbowsX"/>
050  *   <property value="52,52,52,52" name="elbowsY"/>
051  *   <property value="West_1" name="fromPort"/>
052  *   <property value="East_1" name="toPort"/>
053  </pageflow-object>
054  <pageflow-object id="page:result.jsp">
055  *   <property value="200" name="x"/>
056  *   <property value="60" name="y"/>
057  </pageflow-object>
058  <pageflow-object id="forward:path#success#index.jsp#@action:begin.do@">
059  *   <property value="104,160,160,216" name="elbowsY"/>
060  *   <property value="North_1" name="toPort"/>
061  *   <property value="60,60,60,60" name="elbowsX"/>
062  *   <property value="success" name="label"/>
063  *   <property value="South_1" name="fromPort"/>
064  </pageflow-object>
065  <pageflow-object id="forward:path#success#result.jsp#@action:requestMessage.do@">
066  *   <property value="216,160,160,104" name="elbowsY"/>
067  *   <property value="South_1" name="toPort"/>
068  *   <property value="200,200,200,200" name="elbowsX"/>
069  *   <property value="success" name="label"/>
070  *   <property value="North_1" name="fromPort"/>
071  </pageflow-object>
072  <pageflow-object id="forward:path#failure#index.jsp#@action:requestMessage.do@">
073  *   <property value="164,130,130,96" name="elbowsX"/>
074  *   <property value="252,252,252,252" name="elbowsY"/>
075  *   <property value="West_1" name="fromPort"/>
076  *   <property value="East_1" name="toPort"/>
077  *   <property value="failure" name="label"/>
078  </pageflow-object>
079  <pageflow-object id="control:controls.webservice.polling.asyncWebService.HelloWorldAsyncControl#myControl">
080  *   <property value="28" name="x"/>
081  *   <property value="34" name="y"/>
082  </pageflow-object>
083  </view-properties>
084  * ::
085  
086  */
087 public class PollingController extends PageFlowController
088 {
089     /**
090      * The green underscoring under 'myControl', indicates a compiler warning.  
091      * Note that this compiler warning is expected whenever you declare a callback-able 
092      * web service control on a page flow file.  (See the note above for details.)
093      
094      * @common:control
095      */
096     private controls.webservice.polling.asyncWebService.HelloWorldAsyncControl myControl;
097 
098     /*
099      * This member variable stores the message retreived from the web service.
100      */
101     public java.lang.String m_message = "";
102 
103 
104     /**
105      * @jpf:action
106      * @jpf:forward name="success" path="index.jsp"
107      */
108     protected Forward begin()
109     {
110         return new Forward"success" );
111     }
112 
113 
114     /**
115      * This Action polls the HelloWorldAsync.jws web service.
116      
117      * @jpf:action
118      * @jpf:forward name="success" path="result.jsp"
119      * @jpf:forward name="failure" path="index.jsp"
120      */
121     public Forward requestMessage()
122     {
123         try
124         {
125             /*
126              * Request a message from the web service.
127              */
128             myControl.requestMessage(false);
129             
130             /*
131              * Check 10 times to see if the message is ready to be retreived.
132              * from the web service.
133              */
134             for(int i=0; i<10; i++)
135             {
136                 /*
137                  * When the message is ready,
138                  * load the it into the member variable m_message, and 
139                  * forward the user to the response.jsp page.
140                  */
141                 if(myControl.checkStatus() == true)
142                 {
143                      m_message = myControl.getMessageResponse();
144                      return new Forward"success" )
145                 }
146                 else
147                 {
148                     /*
149                      * Wait one second between checks.
150                      */
151                     Thread.sleep(1000,0);
152                 }
153             }
154             
155             /*
156              * If, after 10 seconds, the message is not ready, load an error message into the member
157              * variable m_message, and forward the user to the response.jsp page.
158              */                
159             m_message = "The message was not received in the time allowed.";
160             return new Forward"failure" );
161 
162         }
163         catchThrowable ex )
164         {
165             ex.printStackTrace();
166         }
167         return null;
168     }
169 
170 
171 
172 }