View source | Discuss this page | Page history | Printable version   

ERP 2.50:Background Process


Contents

Introduction

To schedule repetitive tasks within Openbravo, background processes are used. We will present how they are developed, defined and scheduled within this section.

Files Involved

All background processes are stored in $OPENBRAVO_HOME/src/org/openbravo/erpCommon/ad_background folder. There are already a few predefined background processes within the application such as PeriodicAcctServer (accounting), PeriodicAlert (alert process) and PeriodicHeartbeat (heartbeat operation). You can always peek at these to see how these functionalities have been implemented.

We will take the PeriodicAlert example to explain the elements involved with background processes.

The following files are involved (in sequence of execution):

PeriodicBackgroundContextListener.java

The main background processing object (org.openbravo.base.PeriodicBackgroundContextListener) is initialized upon the start of the web application (context). The constructor of this object (contextInitialized) retrieves all background processes defined within the application (AD_PROCESS table, isbackground=Y) and creates an individual instance of PeriodicBackground class for each one of the marked Active. It also starts them and saves a hashmap of all background processes within the context's runtime session (context.setAttribute(BACKGROUND_ATTRIBUTE, backgroundProcess)).

The destructor (contextDestroyed) of this object safely stops all background processes upon stopping of the web application.

This object is defined to be started within the web.xml of the application server configuration file:

<listener>
  <listener-class>org.openbravo.base.PeriodicBackgroundContextListener</listener-class>
</listener>

PeriodicBackground.java

PeriodicBackground implements all the thread control (start, stop, pause) so you do not need to worry about it. You should not need to ever change this file.

The class is instantiated by the PeriodicBackgroundContextListener, once for each background process defined. The constructor of this class has the following parameters:

The constructor creates a new thread for itself. This thread is then started by the PeriodicBackgroundContextListener by calling the start() method of the PeriodicBackground class which in turn calls the start() method of the runner (thread) object.

The run() method of the PeriodicBackground class is now automatically executed and this then calls the processPL() method that the PeriodicAlert needs to implement. This method then contains the actual execution of this background process (in our case, check for any pending alerts).

BackgroundProcess.java

Each background process created in the application must implement the BackgroundProcess interface, which defines a processPL(PeriodicBackground, boolean) that needs to be implemented. This method is called within the run() method of the PeriodicBackground class.

See how PeriodicAlert implements this interface:

package org.openbravo.erpCommon.ad_background;

import org.openbravo.erpCommon.utility.SequenceIdData;
import org.openbravo.erpCommon.businessUtility.EMail;
import org.openbravo.erpCommon.utility.Utility;

public class PeriodicAlert implements BackgroundProcess {

  public String batchSize="50";
  static int counter = 0;  // This variable might be used in order to define how often an alert is going to be executed.

  public void processPL(PeriodicBackground periodicBG, boolean directProcess) throws Exception {
    counter ++;
    periodicBG.addLog("Starting Alert Backgrouond Process. Loop "+counter);
   
    PeriodicAlertData [] alertRule = PeriodicAlertData.selectSQL(periodicBG.conn);

PeriodicAlert.java

ProcessPL has two parameters: PeriodicBackground object, where we obtain the connection pool and information about the login (user, client, organization, ....). Boolean: indicates if the process is being launched manually or it is a scheduled job.

Background Processes in the Application Dictionary

To register a Background process in the application, a new record in the Application Dictionary || Report and Process window must be created:

Application Dictionary Entry

Important note:

There is a bug in the version 2.3x of the application because this field is hidden. To display it, mark it as Active in the “Report and Process” record in “Window, tabs and fields” window. Make sure you also recompile that particular window (ant compile.development -Dtab=Process).

Compiling the Background Process

After introducing the background process to the application dictionary and putting the file(s) inside the correct folder ($OPENBRAVO_HOME/src/org/openbravo/erpCommon/ad_background) we need to recompile the application.

Since background processes are manual code, the following line does the job:

ant compile.development -Dtab=xx

Scheduling

Once the background process is registered in the application and compiled, it must be scheduled. This way, the system will know when to run the new background process.

To do that, go to "General Setup || Application || Process Scheduling" window and enter a new record:

Scheduling

Let's look at two different scenarios

  1. Full time process that needs to be executed all the time
    1. Weekday: empty
    2. Starting Time: 00:00:01
    3. Ending Time: empty
    4. Full Time: checked
  2. A process that needs to be executed every friday afternoon after work
    1. Weekday: Friday
    2. Starting Time: 19:00:00
    3. Ending Time: 19:00:10
    4. Full Time: unchecked

Status

Finally, the "General Setup || Application || Background Process" form is used to deactivate or activate the background processes as well as see their logs.

Status

Retrieved from "http://wiki.openbravo.com/wiki/ERP_2.50:Background_Process"

This page has been accessed 19,547 times. This page was last modified on 3 April 2012, at 11:00. Content is available under Creative Commons Attribution-ShareAlike 2.5 Spain License.