Tutorial: Create a Timer Control

In this mini-tutorial, you will create and test a web service with a timer control.

Note: This tutorial requests that you create a new workspace; if you already have a workspace open, this will restart the IDE. Before beginning, you might want to launch help in standalone mode to avoid an interruption the restart could cause, then locate this topic in the new browser. See Using Help in a Standalone Mode for more information.

The tasks in this tutorial are:

In this tutorial, we will create a web service that declares/instantiates a timer control (an instance of the com.bea.control.TimerControl base class) that calls back the client web service every two seconds. For this example, we will have two web service operations (web methods):

When the callback is received, the sample program will simply display text in the console window. Normally some programming logic would be inserted in the callback routine to perform some appropriate action.

Configure WebLogic Server

In this step you will add a WebLogic Server domain for use with Workshop. This server domain contains runtime libraries required by the application and the application will be deployed to this server.


To Create the Web Service

A timer control can only be created within a conversational (not stateless) web service. To create the web service for this tutorial:

  1. Launch Workshop and create a new, empty workspace.
  2. Click File > New > Project. Expand Web Services and double click on Web Service Project.
  3. Specify the project name. Click Add project to an EAR. (Note that the name of the EAR project defaults to your project name with the suffix "EAR" appended to it.) Click Finish.
  4. In the Project Explorer view, create a package for your web service by right clicking on the Java Resources/src folder (located in your Web Service project folder) and selecting New > Package. Specify the package name and click Finish.
  5. In the Project Explorer view right-click the new package and select New > WebLogic Web Service. Specify the web service name as TimerService and click Finish. The web service designer displays the new, empty web service with a single default method.

To Set up Web Service and Timer Control Annotations

At this point, you have a project containing a package with a web service with the Web Service Design View (Designer pane) displayed in the editor pane.

Conversational web services must implement the java.io.Serializable interface. To set this in your web service:

  1. Edit the source file for your web service by right clicking on the designer and choosing Edit Source. On the class definition line for your web service, insert implements Serializable so that the class definition appears as follows:
    public class TimerService implements Serializable {

    Note that an error marker has appeared in the marker bar on the class declaration line. Right click on the error marker and choose Quick Fix.

  2. The Quick Fix pull-down will appear:

    Click Import 'Serializable' (java.io) and press Enter and a new import line will be generated to resolve the error. Save the file with File > Save.

By default, a hello() method was inserted when you created the web service. This method is not needed.

  1. Click on the TimerService.java [Designer] tab. Right click on the standard hello() method and choose Delete.

    Confirm the deletion by clicking Yes.

To create the timer control:

  1. Right click in the body of the Designer pane and click New Control Reference.
    In the Select Control dialog, select Timer Control and click OK.
  2. Click on the TimerService.java tab and verify that the following code was inserted:
    @Control
    private TimerControl timerControl;
  3. Place the cursor anywhere within the control declaration private TimerControl timerControl;
  4. Click the Properties view.
  5. In the Properties view locate repeatsEverySeconds.
    In the Value column for repeatesEverySeconds enter the value 2 and press the Enter key.
  6. In the source code editor window, the control annotation is updated to:
    	@Control
    	@TimerControl.TimerSettings(repeatsEverySeconds=2)
    	private TimerControl timerControl;
         
  7. Press Ctrl+S to save your work.

We now have a timer control called timerControl which will call back the web service every two seconds. Next we will define two web methods, one to start the timer control and one to stop it.

To define the web method to start the timer:

  1. Click the TimerService.java [Designer] tab, right-click inside the body of the [Designer] tab and choose New Web Method.

    A new operation (web method) is created in the editor, with the default name of the method highlighted.
  2. Name the methed "start" and press the Enter key.

  3. Right click on the start method name and choose Conversation Start from the pulldown.

  4. Now edit the source code and replace the return; statement of the method body to call the timer start method with
    timerControl.start();
    System.out.println("**************");
    System.out.println("Timer started");
    System.out.println("**************"); 

    The web method now looks like this:

    @WebMethod
    @Conversation(Conversation.Phase.START)
    public void start()
    {
       timerControl.start();
       System.out.println("**************");
       System.out.println("Timer started");
       System.out.println("**************");
    }

To define the web method that ends the timer:

  1. Insert a web method named stop as above. Right-click on the method name and choose Conversation Finish .
  2. From the source code editor, change the method declaration to return a String value.
  3. Replace the return; statement of the method body to call the timer stop method with
    timerControl.stop();
    System.out.println("**************");
    System.out.println("Timer stopped");
    System.out.println("**************");
    return "ok";
    The web method now looks like this:
    @WebMethod
    @Conversation(Conversation.Phase.FINISH)
    public String stop()
    {
       timerControl.stop();
       System.out.println("**************");
       System.out.println("Timer stopped");
       System.out.println("**************");
       return "ok";
    }

To add an event handler for when the timer control signals that the defined time has elapsed:

  1. Right click on the Designer pane or the source code pane and choose Insert > Control Event Handler.

    From the Insert Event Handler dialog, click on timerControl and click OK.
  2. Insert the following lines into the event handler body in the source code pane:
    System.out.println("***********************************");
    System.out.println("Callback received from timer firing");
    System.out.println("***********************************");
    

    The event handler should look like this:

    @EventHandler(field="timerControl", eventSet=TimerControl.Callback.class, eventName="onTimeout")        
    protected void timerControl_Callback_onTimeout(long p0, Serializable p1) {
    { System.out.println("***********************************"); System.out.println("Callback received from timer firing"); System.out.println("***********************************"); }
  3. Save all of your changes with the File > Save command.

We now have a web service that contains:

The source for your web service should now look like this:

package timer;

import java.io.Serializable;

import javax.jws.*;
import org.apache.beehive.controls.api.bean.Control;
import com.bea.control.TimerControl;
import weblogic.jws.Conversation;
import org.apache.beehive.controls.api.events.EventHandler;

@WebService
public class TimerService implements Serializable{
	
  @Control
  @TimerControl.TimerSettings(repeatsEverySeconds=2)
  private TimerControl timerControl;
  private static final long serialVersionUID = 1L;
@WebMethod @Conversation(Conversation.Phase.START) public void start() { timerControl.start(); System.out.println("**************"); System.out.println("Timer started"); System.out.println("**************"); } @WebMethod @Conversation(Conversation.Phase.FINISH) public String stop() { timerControl.stop(); System.out.println("**************"); System.out.println("Timer stopped"); System.out.println("**************"); return "ok"; } @EventHandler(field = "timerControl", eventSet = TimerControl.Callback.class, eventName = "onTimeout") protected void timerControl_Callback_onTimeout(long p0, Serializable p1) { System.out.println("***********************************"); System.out.println("Callback received from timer firing"); System.out.println("***********************************"); } }

Test the Web Service / Timer Control

To test the web service and the timer control:

  1. Right click on the editor pane and choose Run As > Run on Server.
  2. In the Run On Server dialog, click Finish.
  3. The WebLogic Test Client will run in a tab in the editor window.

  4. Click the start button to invoke the start operation. The Test Client will display the results returned from the start operation.

  5. Switch to the WebLogic Server console window (command prompt window with the header bar WebLogic Server - 10.3 ) to see the startup and callback results. The console window is iconized on the status bar by default. When you open the console window, you can see the timer starting and the message lines:

    **************
    Timer started
    **************
    generated by the start operation. This message will quickly scroll away, since the timer will immediately begin firing every two seconds.

    The console window will then show the timer firing. Each time the timer fires, a block of status information will be displayed, including the lines:

    ***********************************
    Callback received from timer firing
    ***********************************
          

    that are generated by the event handler that receives callbacks when the timer fires every two seconds.

  6. To stop the timer firing, click on the Continue this conversation link just above the start Request Summary in the body of the test client window. The test client will then display the operation(s) for the next phase of the conversation, in this case, the stop operation.

  7. Click stop. The result of the stop operation will be displayed in the test client window:

    The console window will no longer show the timer firing every two seconds, and will display the lines:

    **************
    Timer stopped
    **************

    to indicate that the stop operation was successful.

Related Topics

Tutorial: Getting Started with Workshop

Using WebLogic System Controls

Timer Control

TimerControl Interface

Timer Control Reference

Tutorial: Create a Timer Control

 


Still need help? Post a question on the Workshop newsgroup.