intra-mart Accel Platform / TERASOLUNA Global Framework Programming Guide

Version 2 2014-01-01

«  Asynchronous Process   ::   Contents   ::   Lock Service  »

Job Scheduler

About Job Scheduler

Job scheduler is a function in which the schedule when to execute the process units called jobs is defined
for automatic execution. It is used when the same task needs to be executed multiple times periodically or the time-consuming task handling
large volume data needs to be executed at night.
In order to meet these requirements stated above, job scheduler of intra-mart Accel Platform provides functions
to automatically execute by the defined schedule, to monitor execution status, or to manage execution result of
of arbitrary business tasks which are made up of Java or Server Side JavaScript on the server.

Specifications

Please refer to the Job Scheduler Specifications for the specifications of job scheduler.

Sample Program

Sample Contents

Fixed character string “Hello.” is concatenated with the parameter vallue set in the key “message”, and outputted.
Get bean of GreetingService from Application Context of Spring Framework, and generate the message.

Program Source

package sample.tgfw.job.hello;

import jp.co.intra_mart.foundation.context.Contexts;
import jp.co.intra_mart.foundation.context.model.AccountContext;
import jp.co.intra_mart.foundation.job_scheduler.Job;
import jp.co.intra_mart.foundation.job_scheduler.JobResult;
import jp.co.intra_mart.foundation.job_scheduler.JobSchedulerContext;
import jp.co.intra_mart.foundation.job_scheduler.annotation.Parameter;
import jp.co.intra_mart.foundation.job_scheduler.annotation.Parameters;
import jp.co.intra_mart.foundation.job_scheduler.exception.JobExecuteException;
import jp.co.intra_mart.framework.extension.spring.context.ApplicationContextProvider;
import sample.tgfw.domain.greeting.GreetingService;

public class HelloJob implements Job {

    public HelloJob() {
    }

    @Override
    @Parameters(@Parameter(key = "message", value = "world!"))
    public JobResult execute() throws JobExecuteException {
        try {
            // Account Context
            final AccountContext accountContext = Contexts.get(AccountContext.class);
            System.out.println("Account context : " + accountContext.toString());

            // Job Scheduler Context
            final JobSchedulerContext jobSchedulerContext = Contexts.get(JobSchedulerContext.class);
            System.out.println("Job scheduler context : " + jobSchedulerContext.toString());

            // Get Parameter
            final String message = jobSchedulerContext.getParameter("message");
            if (null == message) {
                // Process Result : Error
                return JobResult.error("There is no message in the parameter.");
            } else if (message.trim().isEmpty()) {
                // Process Result : Warning
                return JobResult.waring("The message is empty.");
            }
            // Display Message
            System.out.println("Hello. " + message);

            // Get service bean from application context
            final GreetingService greetingService = ApplicationContextProvider.getApplicationContext().getBean(GreetingService.class);
            System.out.println(greetingService.hello());

            // Process Result : Error
            return JobResult.success("Job was executed successfully.");
        } catch (final Exception e) {
        	// Process Result : Error (return process result by exception)
            throw new JobExecuteException("Unexpected error has occurred.", e);
        }
    }
}
Add the following to the applicationContext*.xml of module project in order to register bean of sample.tgfw.domain.greeting.GreetingService to the Application Context of Spring Framework.
<context:component-scan base-package="sample.tgfw.domain" />
  • Job Implementation Class Creation
    Job program of Java creates implementation class of jp.co.intra_mart.foundation.job_scheduler.Job.
    This interface has the execute method definition to state job execution process.
    Job developer should state the program to be executed in job process to this execute method.
  • Getting Application Context of Spring Framework
    In case Application Context of Spring Framework is obtained, please use jp.co.intra_mart.framework.extension.spring.context.ApplicationContextProvider.getApplicationContext().
    In this example, GreetingService which is registered in Application Context of Spring Framework is obtained.

Note

In the Job Process, account context and job scheduler context can be obtained.
These 2 contexts are used to state any arbitrary business process.

Account Context

Account Context includes the account information that it is executed from the job scheduler.

Job Scheduler Context

Job Scheduler Context includes execution information such as the definition information of job, job net, and trigger,
and also the execution date and time.

«  Asynchronous Process   ::   Contents   ::   Lock Service  »