Previous Next vertical dots separating previous/next from contents/index/pdf

Tutorial: Creating a Web Service with Timer Control

In this mini-tutorial, you will create and test a web service with a timer control. This is an advanced tutorial and we assume that you know the basics of Workshop for WebLogic, including how to create workspaces, projects and packages and how to run a web service and test operations with the Test Client. If you are new to Workshop for WebLogic, we recommend that you complete Tutorial: Getting Started and Tutorial: Web Service before beginning this tutorial.

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 step 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.

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 for WebLogic and create a new, empty workspace.
  2. Create an EAR project with File > New > Project > J2EE > Enterprise Application Project.
  3. Define a new domain and server.
  4. Click File > New > Project. Expand Web Services and double click on Web Service Project. Specify the project name. Click Add project to an EAR and choose your EAR project from the pulldown. Click Finish.
  5. Create a package for your web service by right clicking on the src folder of your web service project in the Package Explorer view at the left and clicking New > Package. Specify the package name and click Finish.
  6. Create a web service within the package by right clicking on the package in the Package Explorer view and clicking New > WebLogic Web Service. Specify the web service name and click Finish.
  7. Delete the standard hello() method that was created automatically in the new web service.
    @WebMethod
    public void hello() {
    }

To Set up Web Service and Timer Control Annotations

At this point, you have a project containing a package with a web service. The web service source .java file should be displayed in the editor pane.

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

  1. On the class definition line for your web service, insert implements Serializable so that the class definition looks something like this:
    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 and press Enter and a new import line will be generated to resolve the error.

To create the timer control:

  1. Right click on the editor pane and click Insert > Control. Under New System Control, click on Timer Control and click OK. The control declaration is inserted into the body of the class, with the name (timerControl) highlighted.
    @Control
    private TimerControl timerControl;

    and the properties of the TimerControl annotation are displayed in the Annotations view to the right.
  2. Click in the Value column beside the repeatsEverySeconds property.

    Type 2 and push the Enter key. In the code editor window, the control annotation is updated to:

    	@TimerControl.TimerSettings(repeatsEverySeconds=2)
    	@Control
    	private TimerControl timerControl;
    

    and the new property value is also displayed in the Annotations view.

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. From the editor pane, right click and choose Insert > Web Method. A new operation (web method) is created in the editor, with the name of the method (newWebMethod1) highlighted and the properties of the web method in the Annotations view at the right.
  2. Click in the Value column beside Conversation > value. Use the pulldown to choose START.

  3. Change the name of the web method to start (instead of newWebMethod1).
  4. Replace the //TODO stub 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:

    @Conversation(value=Conversation.Phase.START)
    @WebMethod
    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 as above.
  2. Set the Conversational property to FINISH.

  3. Change the name of the web method to stop (instead of newWebMethod2) and set a String return value.
  4. Replace the //TODO stub 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:

    @Conversation(value=Conversation.Phase.FINISH)
    @WebMethod
    public String stop()
    {
       timerControl.stop();
       System.out.println("**************");
       System.out.println("Timer stopped");
       System.out.println("**************");
       return "ok";
    }

To define the event handler for when the timer control signals that the timer has elapsed:

  1. Right click on the editor 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
    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 All 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;


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

  @WebMethod
  @Conversation(value=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. The WebLogic Test Client will run in a tab in the editor window.

    There are several things to note here:

    • The web service is conversational so only the starting operation is available. The test client provides a reminder Available operations for current conversation phase at the top of the window.
    • A link at the top left (above the Message Log/Clear Log buttons) allows you to start a new conversation.
  2. Click the start button to invoke the start operation. The Test Client will display the results returned from the start operation.

  3. Switch to the WebLogic Server console window (command prompt window with the header bar WebLogic Server - 9.2) 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.

  4. 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.

  5. 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 BEA Workshop for WebLogic Platform

Using WebLogic System Controls

Timer Control

TimerControl Interface

Timer Control Reference

Tutorial: Creating a Web Service with Timer Control

 

 

Skip navigation bar   Back to Top