Automating scheduled task creation
Welcome to another Hidden Gem, where we will highlight a lesser-known but valuable feature of the webMethods suite. There are many such hidden gems that you might not have stumbled upon in training or documentation. These gems could make your life easier or your implementation even better. This edition’s hidden gem is automating scheduled task creation in webMethods Integration Server.
What are scheduled tasks?
In webMethods Integration Server, everything is a service. Services can be invoked by external clients over HTTP or other protocols, through the receipt of a message using triggers, or from the step of a BPM process. You can also use so-called scheduled tasks to configure a service to be executed automatically on a time-based schedule.
For example, you can set up a service to run every minute to check the arrival of some data or to clean up temporary artifacts. You can configure a service to run on the first Monday of every month at 3 a.m. to generate a report of the past month’s sales data.
Scheduled tasks are typically configured in the Integration Server administration User Interface (UI).
Figure 1: Scheduled tasks in the Integration Server administration UI
Deploying scheduled tasks
Scheduled tasks can be deployed using Deployer, as described in the Deployer Users Guide. However, it is easy to forget or make mistakes as the workflow is quite different for scheduled tasks than it is for Integration Server packages and services.
A little-known alternative is to automatically configure the scheduled task when the package containing the relevant service is loaded and to remove it when the package is unloaded. This mechanism sits directly in the same package containing the service to be scheduled and is, therefore, much easier to handle during deployment.
How does it work?
This approach uses two capabilities of Integration Server:
These services in the WmPublic package can be used to register and remove scheduled tasks. The services are documented in the Integration Server Built-In Services Reference.
Startup and shutdown services
Each package in Integration Server can have one or more startup and shutdown services configured. These are user-defined services that are run automatically when the package is loaded or unloaded.
By combining these two services and creating a service to register the scheduled task and another to unregister it, then configuring them as startup and shutdown services for the package, you now have the scheduled task created automatically when the package is loaded and removed when it is unloaded. You only need to deploy the package to a target server, and the scheduled tasks will follow automatically.
Create the services
In the package containing the service to be scheduled, create two additional services: one for creating the scheduled task, one for removing it.
Figure 2: Services for creating and removing scheduled task
The service for adding the task is quite simple: Just invoke pub.scheduler:addRepeatingTask
(or one of the other pub.scheduler:add
services) and set the name of the service to be scheduled, as well as other inputs, such as the interval and the runAsUser.
Figure 3: Services for creating a scheduled task
The service for removing the task is a little more complicated. You need to loop through the tasks, and find the one for the relevant service. This is because the scheduled tasks always have different unique task IDs on each server.
To set up the service as shown in Figure 4, first invoke pub.scheduler:getTaskIDs to get the list of task IDs. Then create a loop step that has taskIDs as its input array. Within the loop, invoke pub.scheduler:getTaskInfo, mapping taskIDs to taskID (see Figure 4). Next, still within the loop, add a branch statement and set service as the switch variable. Within the branch, invoke pub.scheduler:cancelTask, with the service that was scheduled as the Label property (in this example it was myPackage.services:workService).
Figure 4: Service for removing the scheduled task.
Test the two services, checking in the Integration Server administration UI that the first service creates the scheduled task and the second service removes it.
Configure startup and shutdown services
Next, configure these two services to be run at package startup and shutdown time. In the Package Navigator view in Designer, right-click on the package containing the service to be scheduled and then add/remove services, and choose Properties.
Select Startup/Shutdown services on the left, and then add the relevant add and remove services as startup and shutdown services respectively.
Figure 5: Configuring the startup/shutdown services
Then, click on Package Dependencies and add a dependency on WmPublic (version *) to ensure that the WmPublic package is always loaded before this package.
To test, reload the package by right-clicking in Package Navigator in Designer and then choosing Reload Package. Verify in the Integration Server administration UI that the scheduled task has been created. Now, go to Packages > Management in the Integration Server administration UI and disable the package by clicking Yes in the Enabled column for your package. Check that the scheduled task has been removed. Enable the package again by clicking No in Package Management and the scheduled task is created again.
Automating scheduled task creation allows scheduled tasks to be managed more closely by the developer. Also, having the scheduled tasks move around across environments in the same Integration Server package can be an attractive alternative to deploying the scheduled tasks themselves as separate administrative assets.
The pub.scheduler services are documented in the Integration Server Built-In Services Reference, while package properties such as startup/shutdown services and package dependencies are described in the Managing Packages section of the Service Development Help. Both documents are available at the Software AG documentation website on Empower
and Tech Community
Let us know what you think of our new Hidden Gems series. And look for another Hidden Gem in the next edition of TECHniques.