The taskcreator Concurrency Example (original) (raw)

The taskcreator example demonstrates how to use Concurrency Utilities for Java EE to run tasks immediately, periodically, or after a fixed delay. This example provides a JavaServer Faces interface that enables users to submit tasks to be executed and displays information messages for each task. The example uses the Managed Executor Service to run tasks immediately and the Managed Scheduled Executor Service to run tasks periodically or after a fixed delay. (SeeMain Components of the Concurrency Utilities for information about these services.)

The taskcreator example consists of the following components.

Figure 59-1 shows the architecture of the taskcreatorexample.

Figure 59-1 Architecture of the taskcreator Example

The figure shows the architecture of the taskcreator example. The JavaServer Faces page invokes methods on a CDI-managed bean, which submits task initiation requests to an enterprise bean. The enterprise bean uses a WebSocket endpoint to indicate to clients that an updated task execution log is available.

The TaskEJB class obtains the default executor service objects from the application server as follows:

@Resource(name="java:comp/DefaultManagedExecutorService")
ManagedExecutorService mExecService;

@Resource(name="java:comp/DefaultManagedScheduledExecutorService")
ManagedScheduledExecutorService sExecService;

The submitTask method in TaskEJB uses these objects to submit tasks for execution as follows:

public void submitTask(Task task, String type) {
    /* Use the managed executor objects from the app server */
    switch (type) {
        case "IMMEDIATE":
            mExecService.submit(task);
            break;
        case "DELAYED":
            sExecService.schedule(task, 3, TimeUnit.SECONDS);
            break;
        case "PERIODIC":
            ScheduledFuture<?> fut;
            fut = sExecService.scheduleAtFixedRate(task, 0, 8,
                    TimeUnit.SECONDS);
            periodicTasks.put(task.getName(), fut);
            break;
    }
}

For periodic tasks, TaskEJB keeps a reference to the ScheduledFutureobject, so that the user can cancel the task at any time.

Running the taskcreator Example

This section describes how to build, package, deploy, and run thetaskcreator example using NetBeans IDE or Maven.

The following topics are addressed here:

To Build, Package, and Deploy the taskcreator Example Using NetBeans IDE

  1. Make sure that GlassFish Server has been started (seeStarting and Stopping GlassFish Server).
  2. From the File menu, choose Open Project.
  3. In the Open Project dialog box, navigate to:
tut-install/examples/concurrency  
  1. Select the taskcreator folder.
  2. Click Open Project.
  3. In the Projects tab, right-click the taskcreator project and select Build.
    This command builds and deploys the application.

To Build, Package, and Deploy the taskcreator Example Using Maven

  1. Make sure that GlassFish Server has been started (seeStarting and Stopping GlassFish Server).
  2. In a terminal window, go to:
tut-install/examples/concurrency/taskcreator  
  1. Enter the following command to build and deploy the application:

To Run the taskcreator Example

  1. Open the following URL in a web browser:
http://localhost:8080/taskcreator/  

The page contains a form to submit tasks, a task execution log, and a form to cancel periodic tasks. 2. Select the Immediate task type, enter a task name, and click the Submit button. Messages like the following appear in the task execution log:

12:40:47 - IMMEDIATE Task TaskA finished  
12:40:45 - IMMEDIATE Task TaskA started  
  1. Select the Delayed (3 sec) task type, enter a task name, and click the Submit button. Messages like the following appear in the task execution log:
12:43:26 - DELAYED Task TaskB finished  
12:43:25 - DELAYED Task TaskB started  
12:43:22 - DELAYED Task TaskB submitted  
  1. Select the Periodic (8 sec) task type, enter a task name, and click the Submit button. Messages like the following appear in the task execution log:
12:45:25 - PERIODIC Task TaskC finished run #2  
12:45:23 - PERIODIC Task TaskC started run #2  
12:45:17 - PERIODIC Task TaskC finished run #1  
12:45:15 - PERIODIC Task TaskC started run #1  

You can add more than one periodic task. To cancel a periodic task, select it from the form and click Cancel Task.