001 package timer;
002
003 import com.bea.control.TimerControl;
004 import java.text.SimpleDateFormat;
005 import java.util.Date;
006
007 /**
008 * <p>This web service demonstrates the use of a Timer control.</p>
009 *
010 * <p>Each time setAlarm is called it sets a timer to expire in 5 seconds. When the
011 * timer expires, the onAlarm callback is invoked on the client.</p>
012 *
013 * <p>Note that the setAlarm method calls restart on the timer, instead of start. This
014 * will cause any pending timeout events to be discarded. So if you send three setAlarm
015 * messages quickly in sequence, only one onAlarm will occur. This will come five
016 * seconds after the last time setAlarm was called.</p>
017 *
018 * <p>To test this service, invoke the createTimer method in the Test View.
019 * This will create a new conversational instance of this class. Next click on the
020 * conversation that was created and invoke the setAlarm method. After five seconds
021 * have elapsed, right click anywhere in the Test View and select "Refresh".
022 * You should see an "onAlarm" callback invocation listed in the test View log.
023 * Click on the callback log entry to see the string sent back with the callback.</p>
024 *
025 * <p>Try sending a few setAlarm messages in sequence. See how many onAlarm
026 * callbacks messages are sent.</p>
027 *
028 * @common:target-namespace namespace="http://workshop.bea.com/SimpleTimer"
029 */
030 public class SimpleTimer implements com.bea.jws.WebService
031 {
032
033 /**
034 * <p>A timer control that will fire an onTimeout event exactly one time 5 seconds
035 * after start or restart is called.</p>
036 * @jc:timer timeout="5 seconds"
037 * @common:control
038 */
039 private com.bea.control.TimerControl timer;
040
041 private Callback callback;
042
043 public interface Callback
044 {
045 /**
046 * @jws:conversation phase="continue"
047 */
048 public void onAlarm(String timeFired);
049 }
050
051 /**
052 * <p>Creates a new conversational instance of this service. Once one has been
053 * created, you can send setAlarm messages.</p>
054 *
055 * <p>Click on the Conversation link that comes back from this service to access
056 * the continue and finish methods.</p>
057 *
058 * @common:operation
059 * @jws:conversation phase="start"
060 */
061 public void createTimer()
062 {
063 /*
064 * Nothing to do. A new instance is created automatically because of the
065 * "@jws:conversation start" tag. This method is just here to start the
066 * conversation.
067 */
068 }
069
070 /**
071 * <p>Resets the timer control, or starts it the first time.</p>
072 *
073 * <p>Any pending alarms are discarded, leaving one that will expire according to
074 * the @jc:timer tag, above.</p>
075 *
076 * @common:operation
077 * @jws:conversation phase="continue"
078 */
079 public void setAlarm() throws Exception
080 {
081 timer.restart();
082 }
083
084 /**
085 * <p>This method will be called by the timer 5 seconds after we start or
086 * restart the timer.</p>
087 *
088 * <p>Invokes the onAlarm callback on the appropriate client.</p>
089 *
090 * @param time The time in milliseconds that the timer was scheduled to expire.
091 */
092 private void timer_onTimeout(long time)
093 {
094 /*
095 * Use the time received from the callback to create a java.util.Date object.</p>
096 *
097 * Use a java.text.SimpleDateFormat date formatter to turn the java.util.Date
098 * object into a human readable date string.
099 */
100 SimpleDateFormat formatter = new SimpleDateFormat("yyyy.MM.dd 'at' hh:mm:ss a zzz");
101 callback.onAlarm("timer fired on '" + formatter.format(new Date(time)) + "'");
102 }
103
104 /**
105 * <p>Destroys this instance of the service.</p>
106 *
107 * <p>The <tt>@jws:conversation phase="finish"</tt> annotation on this method
108 * means that when the method is invoked the conversation it belongs to will
109 * be terminated.</p>
110 *
111 * @common:operation
112 * @jws:conversation phase="finish"
113 */
114 public void destroyTimer()
115 {
116 /*
117 * Nothing to do. This method is just here to terminate the
118 * conversation.
119 *
120 * You should always provide a way for clients to terminate conversations
121 * when they are finished. Conversations are resources; if you leave a lot
122 * of dangling conversations performance of your server may suffer.
123 *
124 * The lifetime of a conversation may be set with the
125 * @jws:conversation-lifetime tag. Any conversation will terminate
126 * automatically if after that time if no conversational activity occurs.
127 *
128 * Default value is one day.
129 */
130 }
131
132
133 }
|