Show TOC Start of Content Area

Object documentation JobBean Class  Locate the document in its SAP Library structure

Definition

Provides an implementation of the business logic that the instances of the job definition have to perform in the system.

Structure

The sample below shows the source code for a JobBean class that logs a Hello World! message in the database.

Syntax

package com.sap.scheduler.examples;

 

import java.util.logging.Logger;

import javax.ejb.ActivationConfigProperty;

import javax.ejb.MessageDriven;

import com.sap.scheduler.runtime.JobContext;

import com.sap.scheduler.runtime.mdb.MDBJobImplementation;

      //Specify message selector

      //and destination type

@MessageDriven(activationConfig={

      @ActivationConfigProperty(

            propertyName="messageSelector",

            propertyValue="JobDefinition='HelloWorldJob'"),

        @ActivationConfigProperty(

            propertyName="destinationType",

            propertyValue="javax.jms.Queue")})

public class HelloWorldBean extends MDBJobImplementation {

 

   public void onJob(JobContext ctx) {

      //Implement business logic

      Logger log = ctx.getLogger();

      log.info("Hello World!");

   }

}

 

      Message Selector

A JobBean is executed when it receives a Java Messaging Service (JMS) message from the scheduler runtime service. It uses a message selector to restrict the messages it receives from the JMS.

By using the ActivationConfig element of the of the MessageDriven annotation, the JobBean specifies the value of the message selector. It has to be in the following format: JobDefinition = ‘<Job name>’.

<Job name> may contain any valid message selector string literal composed of letters, digits, hyphens (-), and underscores ( _ ). <Job name> is also specified in the job-definition.xml which is an additional JobBean-specific deployment descriptor. The <Job name> value in the JobBean class has to be identical to the one in the job-definition.xml.

Note

<Job name> specifies the name with which the job definition and its instances appear in the Job Management in the SAP NetWeaver Administrator.

The value of <Job name> does not depend on the name of the JobBean class. By using this mechanism, you can define two or more different jobs which have different names but use the same implementation.

      Using a JMS Queue

JobBeans are associated with a JMS queue as the destination type for point-to-point messaging, which allows jobs to be executed asynchronously. By using the ActivationConfigelement of the of the MessageDriven annotation, the JobBean specifies javax.jms.Queue as the destination type.

      The onJob() Method

The scheduler runtime service provides certain basic services to the job implementation. That is why, the JobBean class cannot implement the onMessage() method, which is the standard business method of message-driven beans.

JobBeans have a single business method, the onJob() method. In the JobBean class, you must provide an implementation of the onJob() method. The JobBean inherits from an MDBJobImplementationbase class which itself provides an implementation of the onMessage() method.

Caution 

If you implement the onMessage() method, and not the onJob() method,  the job definition you deploy on the application server will not be operational.

In the implementation of the onJob() method, you code the logic of the unit of work that the instance of the job definition should perform in the system, when it receives a JMS message from the scheduler runtime service.

      Job Context

As a runtime object, a job accesses the scheduler runtime service through the JobContext interface. This interface provides the job access only to runtime services, but no access to scheduling services. You are always authorized to call methods from the JobContext interface.

Every job has a job context. During runtime, a job object obtains a reference to its job context by an instance of the JobContext class. The JobContext object is passed as the single parameter to the onJob() method as the code sample above shows.

Through the JobContext interface you can:

       Execute jobs and retrieve job objects

       Get a Logger object to write job logs to the database

       Handle job parameters

       Set a return code for the job

A job return code is an optional job attribute. It is an integer value output by the job and persisted in the database. A return code is a means for the job to communicate information. The default value of the return code is 0 and indicates that the job completed successfully.

       Work with child jobs.

You can execute and retrieve child jobs.

The JobContext interface is security aware. It allows you to work only with the jobs that were created by the currently logged on user, as well as work with the child jobs whose parent jobs are created by the currently logged on user. You cannot see the jobs created by another user.

For the reference documentation of the JobContext interface, see the JavaDoc at http://help.sap.com/javadocs/nwce/current/sc/index.html and refer to the com.sap.scheduler.runtime package.

 

See also:

Deployment Descriptors

End of Content Area