LOADING...

OFBiz Scheduling: Planned and On-demand Services

 

OFBiz schedulingScheduling jobs is an integral function of complex business applications. Some processes– such as report data generation, data cleanup, data backup, and many more–need to be scheduled at certain time intervals. OFBiz provides strong support for scheduling these types of jobs.

Today we’ll take a look at two important job-scheduling categories; Planned Job Scheduling and On-demand Job Scheduling. Below are some of the common jobs that fall within these categories:

  • Data clean up, such as purging old jobs
  • Auto canceling an order in 30 days
  • Reporting data preparation
  • Reporting Services with ETL jobs from the integration engine
  • Auto reordering a subscription-based product

Here’s how to schedule both planned and on-demand jobs in OFBiz:

Scheduling a Planned Job

OFBiz Scheduling Blog ClockLet’s explore a use case for a planned job within an e-commerce system, specifically, SubscriptionAutoReorders. This is an auto-reorder service for subscription-based products. In this scenario, the user registers within an e-commerce system and opts in for a subscription-based plan (for example, signing up for a monthly magazine subscription). The e-commerce system fulfills the user’s request by running this service daily to check the subscription’s end date. It then automatically reorders the selected item on the last day of the subscription plan.

To set up this type of job in OFBiz, you will first set up the data and then the job manager will schedule the service accordingly. We’ll explore two data set up techniques; using the recurrence rule and using temporal expression.

Using the Recurrence Rule

Example:

<RecurrenceRule recurrenceRuleId="400" untilDateTime="" frequency="MONTHLY" intervalNumber="1" countNumber="-1"/>
<RecurrenceInfo recurrenceInfoId="500" startDateTime="2008-05-14 22:00:00.000" recurrenceRuleId="400" recurrenceCount="12"/>
<JobSandbox jobId="8500" jobName="Auto reorder service for the subscription based product" runTime="2008-05-14 00:00:00.000" serviceName="SubscriptionAutoReorders" poolId="pool" runAsUser="system" recurrenceInfoId="500"/>

OFBiz Scheduling Bog Robot Clock 2As you can see, we have scheduled a service named “SubscriptionAutoReorders“.  The above data displays most of  the required data for scheduling the service using the RecurrenceRule and RecurrenceInfo entities. The RecurrenceRule entity includes fields such as “recurrenceRuleId” which is the primary key. There
is also a field named “frequency” which defines the frequency of the execution of rule (MONTHLY, DAILY, etc.). The purpose of the “intervalNumber field is to set intervals for the defined frequency.

In the RecurrenceInfo entity, you associate the RecurrenceInfo and the RecurrenceRule entity. The primary key for this entity is “recurrenceInfoId“. You can specify “recurrenceCount” to designate the number of repeated executions of the service. You provide a default date and time to begin execution of the service, which can be either a future value or a past value. If it is a past value, the value will be updated to the value of the date and time immediately following the loading of the data.

Finally, you associate all this data to the “JobSandbox”entity. In this entity, “jobId” is the primary key that changes after each execution of the service. The purpose of the “jobName” field is to store a description of the job. The “runTime” field contains the date and time at which the service should be executed.  You can add a past or future date in this field using the format shown above. In the “serviceName” field you enter the name of the service you want to schedule. In “poolId” you list the name of the job pool from which it should “pick up” the job. You can get that name in the serviceengine.xml. In the “runAsUser” field, you include the name of the user that is responsible for running this service. You can include a recurrenceInfoId to associate the rule for execution of the service with the “recurrenceInfoId” field.

Using Temporal Expression

If you have complex scheduling needs (such as every Monday, the 15th of month etc,) you can use the TemporalExpression data which is already available in OFBiz. It is a very simple and flexible data design. You can also use the Union, Intersection and Difference operations for TemporalExpressions to produce complex schedules. Here is a link to additional information about Temporal Expressions. In addition,you can refer to existing TemporalExpressions  data within OFBiz here.

Example:

<TemporalExpression tempExprId="FAM_AUTO_CREATE" tempExprTypeId="FREQUENCY" description="Every Hour" integer1="12" integer2="60"/>
<JobSandbox jobId="FAM_AUTO_CREATE" jobName="Auto-create Fixed Asset Maintenances" runTime="2000-01-01 00:00:00.000" serviceName="createMaintsFromTimeInterval" poolId="pool" runAsUser="system" tempExprId="FAM_AUTO_CREATE" maxRecurrenceCount="-1"/>

In the example above, the primary key is tempExprId. The”tempExprTypeId” field names the specific type of TemporalExpression (such as FREQUENCY, UNION, DAY_IN_MONTH, HOUR_RANGE etc.). You can review the ServiceDemoData.xml file for more information about the tempExprTypeId values (in the data example above, we have used  FREQUENCY).OFBiz Scheduling Blog Tooy Clock3 After creating a record for the TemporalExpression entity, you should pass the tempExprId to the JobSandbox entity record to schedule the service.

Scheduling an On-demand Job

Lets explore a common real-world requirement for On-demand Jobs. For example, two different users may be utilizing the same online e-commerce portal to order medication. One user may need to order a particular medication weekly and another user may need to order the same medication every two weeks. In both the cases, both users will place an initial order, and will set the reorder frequency at that time interval to meet their individual needs. The e-commerce system will schedule a service on the day/time individually specified by each user.

In OFBiz, you can use the dispatcher.schedulefunction to schedule a run time for a service. Here’s how:

// schedule service
try {
        dispatcher.schedule(jobName, poolName, serviceName, serviceContext, startTime, frequency, interval, count, endTime, maxRetry);
    } catch (GenericServiceException e) {
    
    }

Above is a code snippet from the CoreEvents.java file within OFBiz. You can see that we have used the dispatcher.schedule method to schedule the specific run time for a service. You can refer to the JobManager API for more information.

Conclusion

With its Universal Data Model, OFBiz makes it easier than ever for you to schedule both planned and on-demand jobs–regardless of the nature of your e-commerce business. To learn more about scheduling jobs, please get in touch with us at info@hotwaxsystems.com. For more information about HotWax Commerce, our enterprise-class, open source, digital commerce solution, powered by OFBiz, you can contact us here.

Photo credit: Profound Whatever / Foter / CC BY-NC-SA

Photo credit: Andy M Johnson / Foter / CC BY-NC-SA

Photo credit: Sean Davis / Foter / CC BY-ND

Photo credit: Andy M Johnson / Foter / CC BY-NC-SA


DATE: Mar 12, 2015
AUTHOR: Arun Patidar
Company News, OFBiz, OFBiz Tutorials

Leave a Reply

Your email address will not be published. Required fields are marked *