001 package timer;
002
003 import com.bea.control.JwsContext;
004 import com.bea.control.TimerControl;
005 import java.util.Date;
006
007 /**
008 * <p>A web service that demonstrates an asynchronous interface on a legacy system that
009 * does not support asynchrony. It does so by "polling" the legacy system: calling
010 * it repeatedly to see if it's done.</p>
011 *
012 * <p>To use this service, call <b>start</b> to create a new conversational instance and invoke
013 * the operation on the legacy system. After the legacy system completes it's
014 * operation, we will invoke the client's <b>onDone</b> callback. If the legacy system does
015 * not complete in 15 seconds, we will invoke the <b>onDone</b> callback, but with a failure
016 * result.</p>
017 *
018 * @common:target-namespace namespace="http://workshop.bea.com/AdvancedTimer"
019 */
020 public class AdvancedTimer implements com.bea.jws.WebService
021 {
022 /**
023 * @common:context
024 */
025 JwsContext context;
026
027 /**
028 * @common:control
029 */
030 private timer.LegacySystemControl legacySystem;
031
032
033 /**
034 * <p>This is the timer that we use to poll the legacy system. Each time the timer
035 * calls us back, we will poll the legacy system again.</p>
036 *
037 * <p>The timer will expire in 10 seconds the first time, then every second after
038 * that.</p>
039 *
040 * @jc:timer repeats-every="1 seconds" timeout="10 seconds"
041 * @common:control
042 */
043 private TimerControl timer;
044
045 /**
046 * <p>This variable counts how many times the timer has called us back. Once it
047 * has called us 6 times, then 15 seconds have elapsed.</p>
048 *
049 * <p>Note that this member variable is made persistent by the presence of
050 * the <b>@jws:conversation</b> tags.</p>
051 */
052 private int alarmCount;
053
054 public Callback callback;
055
056 public interface Callback
057 {
058 /**
059 * <p>This callback will be invoked when the legacy system completes successfully
060 * or if it does not complete in 15 seconds.</p>
061 *
062 * @param succeeded Indicates whether the legacy system completed its
063 * operation successfully in time.
064 *
065 * @jws:conversation phase="finish"
066 */
067 public void onDone(boolean succeeded);
068 }
069
070 /**
071 * <p>Starts a new conversation. This will result in an <b>onDone</b> callback in
072 * 10 to 15 seconds.</p>
073 *
074 * @throws Exception As thrown by TimerControl.start().
075 *
076 * @common:operation
077 * @jws:conversation phase="start"
078 */
079 public void start() throws Exception
080 {
081 // Start the legacy system working on this operation.
082 legacySystem.start();
083
084 // Start the timer going. It will call us back after 10 seconds, and then
085 // once a second after that.
086 timer.start();
087
088 // Record that we have not received any onTimeout events yet.
089 alarmCount = 0;
090 }
091
092 /**
093 * <p>This will be called each time the timer alarm goes off. Each time we get
094 * invoked, we will "poll" the legacy system to see if it has completed.
095 * If it has not completed after 15 seconds (6 calls to this method), then
096 * we will send back a failure result.</p>
097 *
098 * @throws Exception This can be thrown by the timer.
099 */
100 private void timer_onTimeout(long time) throws Exception
101 {
102 // Poll the legacy system to see if it has completed.
103 if (legacySystem.isDone())
104 {
105 // Notify our client that the operation completed successfully.
106 callback.onDone(true);
107
108 // Turn the timer off since we no longer need it.
109 timer.stop();
110
111 // terminate the conversation since we no longer need it.
112 context.finishConversation();
113 }
114 else
115 {
116 // Increment the count of times we have received onTimout events.
117 alarmCount += 1;
118
119 // If we have been called 6 times, then 15 seconds have elapsed.
120 // In that case, send the operation has failed.
121 if (alarmCount == 6)
122 {
123 // Notify our client that the operation failed.
124 callback.onDone(false);
125
126 // Turn off the timer since we no longer need it.
127 timer.stop();
128 }
129 }
130 }
131
132 }
|