|
|
BROWSE
All Titles WordPress Web Services SOA BPEL Web Graphics & Video Web Development RAW Portugues, Espanol, Italiano, French PHP/MySQL Oracle Open Source Networking & Telephony Moodle Microsoft & .NET Linux Servers jQuery Joomla! JBoss Java e-Learning e-Commerce Dynamics Drupal CRM Cookbook Content Management Beginner Guides Architecture and Analysis AJAX Future Titles Recently Published Titles TOP TITLES ![]()
|
OSWorkflow and the Quartz Task Scheduler
In this article, we will explore the Quartz task scheduler and its integration with OSWorkflow. We will also give a tutorial with Quartz sending events and actions to OSWorkflow. This gives OSWorkflow temporal capabilities found in some business domains, such as call centers or customer care services. Task Scheduling with QuartzBoth people-oriented and system-oriented BPM systems need a mechanism to execute tasks within an event or temporal constraint, for example, every time a state change occurs or every two weeks. BPM suites address these requirements with a job-scheduling component responsible for executing tasks at a given time. OSWorkflow, the core of our open-source BPM solution, doesn't include these temporal capabilities by default. Thus, we can enhance OSWorkflow by adding the features present in the Quartz open-source project. What is Quartz?Quartz is a Java job-scheduling system capable of scheduling and executing jobs in a very flexible manner. The latest stable Quartz version is 1.6. You can download Quartz from http://www.opensymphony.com/quartz/download.action.InstallingThe only file you need in order to use Quartz out of the box is quartz.jar. It contains everything you need for basic usage. Quartz configuration is in the quartz. properties file, which you must put in your application's classpath.Basic ConceptsThe Quartz API is very simple and easy to use. The first concept that you need to be familiar with is the scheduler. The scheduler is the most important part of Quartz, managing as the word implies the scheduling and unscheduling of jobs and the firing of triggers.Task Scheduling with QuartzA job is a Java class containing the task to be executed and the trigger is the temporal specification of when to execute the job. A job is associated with one or more triggers and when a trigger fires, it executes all its related jobs. That's all you need to know to execute our Hello World job.Integration with OSWorkflowBy complementing the features of OSWorkflow with the temporal capabilities of Quartz, our open-source BPM solution greatly enhances its usefulness. The Quartz-OSWorkflow integration can be done in two ways—Quartz calling OSWorkflow workflow instances and OSWorkflow scheduling and unscheduling Quartz jobs. We will cover the former first, by using trigger-functions, and the latter with the ScheduleJob function provider.Creating a Custom JobJob's are built by implementing the org.quartz.Job interface as follows:public void execute(JobExecutionContext context) throwsThe interface is very simple and concise, with just one method to be implemented. The Scheduler will invoke the execute method when the trigger associated with the job fires. The JobExecutionContext object passed as an argument has all the context and environment data for the job, such as the JobDataMap. The JobDataMap is very similar to a Java map but provides strongly typed put and get methods. This JobDataMap is set in the JobDetail file before scheduling the job and can be retrieved later during the execution of the job via the JobExecutionContext's getJobDetail().getJobDataMap() method. Trigger Functionstrigger-functions are a special type of OSWorkflow function designed specifically for job scheduling and external triggering. These functions are executed when the Quartz trigger fires, thus the name. trigger-functions are not associated with an action and they have a unique ID. You shouldn't execute a trigger-function in your code. To define a trigger-function in the definition, put the trigger-functions declaration before the initial-actions element. ... This XML definition fragment declares a trigger-function (having an ID of 10), which executes a beanshell script. This script will put a named property inside the PropertySet of the instance but you can define a trigger-function just like any other Java- or BeanShell-based function. To
invoke this trigger-function, you will
need an OSWorkflow built-in function provider to execute trigger-functions and to
schedule a custom job—the ScheduleJob
FunctionProvider. More about TriggersQuartz's triggers are of two
types—the SimpleTrigger and the CronTrigger. The former, as
its name implies, serves for very simple purposes while the latter is
more complex and powerful; it allows for unlimited flexibility for
specifying time periods. SimpleTriggerSimpleTrigger is more suited for job firing at specific points in time, such as Saturday 1st at 3.00 PM, or at an exact point in time repeating the triggering at fixed intervals. The properties for this trigger are the shown in the following table:
CronTriggerThe CronTrigger is based on the concept of the UN*X Cron utility. It lets you specify complex schedules, like every Wednesday at 5.00 AM, or every twenty minutes, or every 5 seconds on Monday. Like the SimpleTrigger, the CronTrigger has a start time property and an optional end time.A CronExpression is made of seven parts, each representing a time component:
Each number represents a time part:
0 0 6 ? * MON: This CronExpression means "Every Monday at 6 AM". 0 0 6 * *: This CronExpression mans "Every day at 6 am". For more information about CronExpressions refer to the following website: http://www.opensymphony.com/quartz/wikidocs/CronTriggers Tutorial.html. Scheduling a JobWe will get a first taste of Quartz, by executing a very simple job. The following snippet of code shows how easy it is to schedule a job. SchedulerFactory schedFact = new The following code assumes a HelloJob class exists. It is a very simple class that implements the job interface and just prints a message to the console. package packtpub.osw; The first three lines of the following code create a SchedulerFactory, an object that creates Schedulers, and then proceed to create and start a new Scheduler. SchedulerFactory schedFact = newThis Scheduler will fire the trigger and subsequently the jobs associated with the trigger. After creating the Scheduler, we must create a JobDetail object that contains information about the job to be executed, the job group to which it belongs, and other administrative data. JobDetail jobDetail = new JobDetail("myJob", null, HelloJob.class); The TriggerUtils is a helper
object used to simplify the trigger code. With the help
of the TriggerUtils, we will create
a trigger that will fire every
hour. This trigger will start firing the
next even hour after the trigger is registered with
the Scheduler. The last line of
code puts a name to the trigger for housekeeping
purposes.Finally, the last line of code associates the trigger with the job and puts them under the control of the Scheduler. sched.scheduleJob(jobDetail, trigger); When the next even hour arrives after this line of code is executed, the Scheduler will fire the trigger and it will execute the job by reading the JobDetail and instantiating the HelloJob.class. This requires that the class implementing the job interface must have a no-arguments constructor. An alternative method is to use an XML file for declaring the jobs and triggers. This will not be covered in the book, but you can find more information about it in the Quartz documentation. Scheduling from a Workflow DefinitionThe ScheduleJob FunctionProvider has two modes of operation, depending on whether you specify the jobClass parameter or not. If you declare the jobClass parameter, ScheduleJob will create a JobDetail with jobClass as the class implementing the job interface. <pre-functions> This
fragment will schedule a job based on the SendMailIfActive class with
the current time as the start time. The ScheduleJob like any
FunctionProvider can be
declared as a pre or a post function. On the other hand, if you don't declare the jobClass, ScheduleJob will use the WorkflowJob.class as the class implementing the job interface. This job executes a trigger-function on the instance that scheduled it when fired.
<pre-functions>
This definition fragment will execute the trigger-function with ID 10 as soon as possible, because no CronExpression or start time arguments have been specified. This FunctionProvider has the arguments shown in the following table:
Books from Packt Transactions in QuartzExcluding a few minor exceptions, Quartz performs the same transactions in a standalone application or inside a full-blown J2EE Container. One of these exceptions is the use of global JTA transactions inside a JTA-complaint container. To enable the creation of a new JTA transaction or to join to an existing JTA transaction, just set the org.quartz.scheduler.wrapJobExecutionInUserTransaction property inside the quartz.properties file to true. Enabling this parameter allows the Quartz job to participate inside a global JTA transaction. This in combination with a JTA workflow implementation puts the workflow step and the temporal task into one transaction, thus assuring the information integrity. JobStoresThe JobStore interface designed in Quartz is responsible for the persistence and retrieval of all job and trigger data. There are two built-in implementations of the JobStore interface, the RamJobStore and the JDBCJobStore. The RamJobStore stores the job, trigger, and calendar data in memory, losing its contents after JVM restarts. On the other hand, JDBCJobStore uses the JDBC API to store the same data. The JDBCJobStore uses a delegate to use specific functions of each database, for example, DB2, PostgreSQL, etc. The JobStore configuration is located in the quartz.properties file. To set the JobStore, add the following line to the configuration file, if you want to use the RamJobStore: org.quartz.jobStore.class = org.quartz.simpl.RAMJobStoreThe configuration of the JDBCJobStore is a little more complex as it involves datasources, transactions, and delegates: To use local JDBC transactions, you only need to set the following parameters: org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX The datasource is your datasource JNDI name. To use global JTA transactions, you need the following parameters: org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreCMT This differs from the JDBC transaction mode in its use of a non-JTA managed datasource for internal JobStore use. For both transaction modes you need to set the database delegate appropriate for your database. org.quartz.jobStore.driverDelegateClass= The delegates included with Quartz are as follows:
Example Application—Customer SupportIn this section we will develop an example to show the capabilities of the OSWorkflow-Quartz duo. In every company, the customer plays a central role. If not attended to correctly, he or she can turn around and buy services or products from a competitor. Every company also has a customer support department and a good performance indicator for this department would be the number of customer requests attended to.Some customer support requests come from mail or web interfaces. Suppose you have an web application that receives customer support requests. A typical customer support process is as follows: ![]() This is the most simple of processes and the most commonly implemented. While in the pending state, the request can be forwarded to many people to finally reach completion. If the request is stalled in this state, the process doesn't add value to the business and doesn't match customer expectations, thereby downgrading the company image and customer loyalty. So a good approach to the process would be to reduce the percentage of support requests in the pending state. If a support request is in the same state for two hours, an e-mail to the customer support coordinator is send, and if a six hour threshold is exceeded an email is send directly to the customer support manager for notification purposes. These notifications assure the request will never be accidentally forgotten. The decision flow is depicted in the following figure:
To implement this process logic, we need temporal support in our business process. Obviously this is done by Quartz. The workflow definition is as follows: <?xml version="1.0" encoding="UTF-8"?> So the process definition is very easy, as we have two steps, but the key of the solution lies in the ScheduleJob2 FunctionProvider. This FunctionProvider is a slightly modified version of OSWorkflow's built-in ScheduleJob; the only difference is that the new implementation puts the function provider's arguments in the JobDataMap of the job. The difference from the original ScheduleJob code is as follows: dataMap.putAll(args);There is just one line to put the arguments of the FunctionProvider into the JobDataMap. The process definition schedules a custom SendMailIfActive job every two hours and a SendMailIfActive job every six hours. If the process is still in pending state, then a mail is sent, otherwise the job is unscheduled. The job code is as follows: package packtpub.osw; This completes the proactive workflow solution commonly requested by users. SummaryIn
this article, we covered the integration of the Quartz job-scheduling
system with OSWorkflow, which provided temporal capabilities to
OSWorkflow. We also took a look at the trigger-functions, which are executed when a Quartz trigger fires. We also learned how to schedule a job from a Workflow definition by using ScheduleJob. Finally, we showed the capabilities of the Quartz-OSWorkflow duo with the help of a sample application.
Diego Adrian Naya Lazo is a Chief Enterprise Architect living in Buenos Aires, Argentina. He currently works for Argentina’s biggest healthcare provider and has more than 10 years of experience in the IT industry. He has participated in several projects as a hands-on software architect and performed the technical lead role in many companies. His interest in computer programming began with his desire to create the most vivid 3D animations as a graphic designer at age of 15. His interests range from Enterprise Architecture to SOA and BPM technology. He is a Sun Certified Enterprise Architect and holds others certifications such as: SCJP, SCWCD, MCSA and Security+. He also is a member of the WWISA and GEAO enterprise architect’s associations as well as an active developer of the OSWorkflow project. He holds a Bachelor degree in IT and is currently enrolled in an MBA program. Away from work, Diego enjoys traveling all around the world with his family. You can reach with at dienaya@gmail.com. Books from Packt | BOOK ![]() Business Process Management with JBoss jBPM See More BOOK ![]() Alfresco Enterprise Content Management Implementation See More BOOK ![]() OSWorkflow: A guide for Java developers and architects to integrating open-source Business Process Management See More |
| ||||||||