Support > Wagby Developer Network(R7) > Batch processing > Register your own class in the job scheduler
ja | en

I will explain how to create a simple Java class that performs batch processing. Register this in the job scheduler provided by Wagby.

We will prepare a "customer registration job" targeting the customer model.The procedure is as follows.

  1. Prepare the folder customize/webapp/WEB-INF/export/init/jfcjob.
  2. Save XXXJob.xml in the same folder.

As an example, create CreateCustomerJob.xml.We will have the following contents.

<?xml version="1.0" encoding="UTF-8"?>
<jfcjob createApplicationContext="false">
  <name>CreateCustomer</name>
  <memo>CreateCustomer</memo>
  <classname>jp.jasminesoft.wagby.job.CreateCustomerJob</classname>
  <description>顧客データを作成する</description>
</jfcjob>
  • The information that has been set will be included in "managed process> job management ...> job master" of the built application.
  • The job master model (jfcjob) is included as a system model of Wagby.Figure 1 shows the design information.
Fig. 1 jfcjob model
In the above example, the name item and memo item in CreateCustomerJob.xml are the same, but you can also enter Japanese for the memo item.Because the description item is drawn on the screen in Figure 6 below, please set a descriptive text that is easy to understand.

Job class

The job class is called from Wagby's job scheduler. Here we prepare the CreateCustomerJob.java class shown above.

package jp.jasminesoft.wagby.job;

import org.apache.log4j.Logger;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import jp.jasminesoft.jfc.DbActionParameter;
import jp.jasminesoft.jfc.job.JFCBaseJobBean;

public class CreateCustomerJob extends JFCBaseJobBean {
    private final static Logger logger = 
        Logger.getLogger(CreateCustomerJob.class.getName());

    private CreateCustomerTask createCustomerTask;

    public void setCreateCustomerTask(CreateCustomerTask createCustomerTask) {
        this.createCustomerTask = createCustomerTask;
    }

    @Override
    protected String executeInternal0(JobExecutionContext jobContext,
	DbActionParameter p) throws JobExecutionException {
	return createCustomerTask.process(p);
    }
}

As shown in this code, the body of batch processing is described in the class called CreateCustomerTask.The job class is a container and it can receive job parameters.We will use this parameter as an argument to the task class and delegate the actual processing to the task class.

Task class

It is the main body of batch processing.CreateCustomerTask.java.

package jp.jasminesoft.wagby.job;

import jp.jasminesoft.jfc.DbActionParameter;
import jp.jasminesoft.jfc.app.EntityHelper;
import jp.jasminesoft.jfc.service.JFCEntityService;
import jp.jasminesoft.wagby.model.customer.*;
import jp.jasminesoft.wagby.app.customer.*;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

public class CreateCustomerTask {
    private final static Logger logger = 
        Logger.getLogger(CreateCustomerTask.class.getName());

    @Autowired
    @Qualifier("CustomerHelper")
    protected EntityHelper<Customer, Integer> customerHelper;
    
    @Autowired
    @Qualifier("CustomerEntityService")
    protected JFCEntityService<Customer, Integer> customerEntityService;

    protected String process(DbActionParameter p) {
	logger.info("顧客データ作成ジョブを開始しました。");
	for (int i = 0; i < 100; i++) {
	    Customer customer = new Customer();
	    customerHelper.initialize(customer, p);
	    customerEntityService.insert(customer);
	    logger.info( i + "件の顧客データを登録しました。");
	}
	return "顧客データ作成ジョブを正常終了しました。";
    }
}

Task class reuses helper and service class (auto-generated) using @Autowired annotation.This realizes business processing without writing SQL.

Since the task class is POJO, it has reusability.

Save the following file as customize/webapp/WEB-INF/applicationContext/customer-ex.xml.(File name isDoes not overlap automatically generated fileIt is free if it is.)

Here we prepare CreateCustomerTask as a bean definition.It also defines the CreateCustomer job and marks it as CreateCustomerTask.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns:context="http://www.springframework.org/schema/context"
     xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context.xsd">

  <bean id="CreateCustomer" scope="prototype"
        class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
    <property name="jobClass">
      <value>jp.jasminesoft.wagby.job.CreateCustomerJob</value>
    </property>
    <property name="jobDataAsMap">
      <map>
        <entry key="createCustomerTask" value-ref="CreateCustomerTask"/>
      </map>
    </property>
  </bean>

  <bean id="CreateCustomerTask" scope="prototype"
        class="jp.jasminesoft.wagby.job.CreateCustomerTask"/>
</beans>

The added files are as follows.

customize/java/jp/jasminesoft/wagby/job/CreateCustomerJob.java
customize/java/jp/jasminesoft/wagby/job/CreateCustomerTask.java
customize/webapp/WEB-INF/applicationContext/customer-ex.xml
customize/webapp/WEB-INF/export/init/jfcjob/CreateCustomerJob.xml

You can download this file.Please expand it under the customize folder.

Separately, prepare the "customer" model before trying.Customer The definition of the model is irrelevant.

Log on as a system administrator."Administrator Guide (R7)> Preparation: Enable job execution accountMake the job execution account available as described in.

Figure 2 Enabling Job Execution Account

Select "Job management ..." on the management processing tab.

Figure 3 Job management

Opens the "Job Schedule Search" screen.

Figure 4 Job Schedule Search Screen

Open the Job Schedule New Registration screen.

Figure 5 Job Schedule New registration screen

Select the created job.

Figure 6 Selecting a job

Specify the job execution account.At this stage, jobs are not enabled.

Figure 7 Specifying job execution account

Click the "Run Now" button and try running this job.

Figure 8 "Run now"

The job is executed.Messages about jobs are recorded in wagbyapp/logs/system.log.

Figure 9 Job is executed
The job program introduced here is to register 100 new customers.You can check if 100 entries were actually registered.

"Job parameters" are added, and this can be received by the job class.

Figure 10 Specifying job parameters

The source code of the receiving side can be described as follows.

package jp.jasminesoft.wagby.job;

import org.apache.log4j.Logger;
import org.quartz.JobDataMap;// 追加
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import jp.jasminesoft.jfc.DbActionParameter;
import jp.jasminesoft.jfc.job.JFCBaseJobBean;

public class CreateCustomerJob extends JFCBaseJobBean {
    private final static Logger logger = 
        Logger.getLogger(CreateCustomerJob.class.getName());

    private CreateCustomerTask createCustomerTask;

    public void setCreateCustomerTask(CreateCustomerTask createCustomerTask) {
        this.createCustomerTask = createCustomerTask;
    }

    @Override
    protected String executeInternal0(JobExecutionContext jobContext,
	DbActionParameter p) throws JobExecutionException {

        JobDataMap dataMap = jobContext.getJobDetail().getJobDataMap();
        String args = (String)dataMap.get("process");

	return createCustomerTask.process(p);
    }
}

The import statement of the fourth line is added.Job parameters are managed by org.quartz.JobDataMap class.
Parameters are acquired at lines 24 and 25.In the example shown in Fig. 9, we received parameters registered (from the screen) with the name "key" key.The string "AAA" is stored in the variable args.

"Support> Administrator Guide (R7)> How to specify a job schedulePlease read.

  • "Renewal processing" can not be performed in the version from R 7.0.0 to R 7.3.2.
  • When performing "update processing" with the version from R 7.4.0 to R 7.5.0, please set the locking method of the target model to "optimistic lock".In the version after R7.5.1, the locking method works with either optimistic locking or pessimistic locking.

For Wagby (R7.0.0 to R7.9.2) before R7.10, specify the org.springframework.scheduling.quartz.JobDetailBean class instead of the org.springframework.scheduling.quartz.JobDetailFactoryBean class.This is because R7.10, the version of Spring Framework has been updated from 3 to 4.

Specifically, the xml file of the job definition is as follows.

...
  <bean id="CreateCustomer" scope="prototype"
        class="org.springframework.scheduling.quartz.JobDetailBean">
...

For Wagby (R 7.0.0 to R 7.4.3) before R 7.5, the createApplicationContext = "false" attribute of the jfcjob element prepared in CreateCustomerJob.xml is ignored.Therefore, CreateCustomer job definition is output to automatically generated WEB-INF/applicationContext/__ jobcontrol.xml.You will need to delete this manually.

Specifically, remove the following line that is included at the end of the $ (DEVHOME)/wagbydesigner/webapps/wagbydesigner/WEB-INF/env/work/srcgen/webpage/WEB-INF/applicationContext/__ jobcontrol.xml.

  <!-- extension -->
  <bean id="CreateCustomer" scope="prototype"
        class="org.springframework.scheduling.quartz.JobDetailBean">
    <property name="jobClass">
      <value>jp.jasminesoft.wagby.job.CreateCustomerJob</value>
    </property>
  </bean>

Save the deleted file in $ (DEVHOME)/customize/webapp/WEB-INF/applicationContext.In other words, this file will be overwritten to the file saved in the customize folder from now on.Maintenance of this file should be done by the developer.