4.1.3.1. Creating a Batch to Delete the data in the target table(Batchlet Step)

This section explains a batch that Delete the data in the target table in the batchlet step based on the example application.

Description of the function to be created
  1. Check the current DB status

    Execute the following SQL from the console of H2.

    SELECT * FROM ZIP_CODE_DATA;
    SELECT * FROM ZIP_CODE_DATA_WORK;
    

    If data is not registered, perform step 2.

  2. (If data is not registered) Execute the mailing address registration batch

Execute the following command from the command prompt

$cd {nablarch-example-batch-ee System repository}
$mvn exec:java -Dexec.mainClass=nablarch.fw.batch.ee.Main ^
    -Dexec.args=etl-zip-code-csv-to-db-chunk

Execute the following SQL from the console of H2 and confirm that the data is registered.

SELECT * FROM ZIP_CODE_DATA;
SELECT * FROM ZIP_CODE_DATA_WORK;
  1. Execute the mailing address table delete batch

Execute the following command from the command prompt

$cd {nablarch-example-batch-ee System repository}
$mvn exec:java -Dexec.mainClass=nablarch.fw.batch.ee.Main ^
    -Dexec.args=zip-code-truncate-table
  1. Confirm that the data in the target table has been deleted

    Execute the following SQL from the console of H2 and confirm that the data has been deleted.

    SELECT * FROM ZIP_CODE_DATA;
    SELECT * FROM ZIP_CODE_DATA_WORK;
    

4.1.3.1.1. Delete the data in the target table

This section explains how to implement a batch to delete the mailing address information.

For the process flow, refer to the process flow of the batch of the batchlet step. For the responsibility assignment, refer to the responsibility assignment of the batchlet step.

Create batchlet

Create batchlet class of batch to delete the mailing address information.

Interfaces to be implemented and their responsibilities
Implement the following interface in the batchlet class to create the batch process. The overridden method is called at an appropriate timing by Batch Runtime.
Interface Implementation
Batchlet

Implement batch processing.

Inherits AbstractBatchlet, which provides the default implementation.

  • Batchlet#process
  • Batchlet#stop

Tip

Batch process is configured by a listener that provides common processes such as transaction control in addition to the implementation of the above interface. For details of the listener, see listener used in the batch application and how to specify the listener.

TruncateTableBatchlet.java
@Dependent
@Named
public class TruncateTableBatchlet extends AbstractBatchlet {

    @Inject
    @BatchProperty
    private String tableName;

    @Override
    public String process() {

        final AppDbConnection conn = DbConnectionContext.getConnection();
        final SqlPStatement statement
            = conn.prepareStatement("TRUNCATE TABLE " + tableName);
        statement.executeUpdate();

        return "SUCCESS";
    }
}
Key points of this implementation
  • Inherits AbstractBatchlet, and performs the business process by process method.
  • Named and Dependent are assigned to the class.
    By configuring named and dependent annotations, batchlet implementation class can be used as CDI management bean. As a result, the batchlet class name specified in the job definition can be described with the CDI management name.
    (If CDI management bean is not used, describe with fully qualified name (FQCN))
  • Execute TRUNCATE statement using database access.
Create a job definition file

Create a file that defines the job execution settings.

zip-code-truncate-table.xml
<job id="zip-code-truncate-table" xmlns="http://xmlns.jcp.org/xml/ns/javaee" version="1.0">
  <listeners>
    <listener ref="nablarchJobListenerExecutor" />
  </listeners>

  <step id="step1" next="step2">
    <listeners>
      <listener ref="nablarchStepListenerExecutor" />
    </listeners>
    <batchlet ref="truncateTableBatchlet">
      <properties>
        <property name="tableName" value="ZIP_CODE_DATA" />
      </properties>
    </batchlet>
  </step>
  <step id="step2">
    <listeners>
      <listener ref="nablarchStepListenerExecutor" />
    </listeners>
    <batchlet ref="truncateTableBatchlet">
      <properties>
        <property name="tableName" value="ZIP_CODE_DATA_WORK" />
      </properties>
    </batchlet>
  </step>
</job>
Key points of this implementation
  • Job definition file is located under /src/main/resources/META-INF/batch-jobs/.
  • Specify the job name in the id attribute of the job element.
  • For a batch job consisting of multiple steps, define multiple step elements and execute the process sequentially.
  • Specify a name with the first letter of the batchlet class name in lowercase for the ref attribute of batchlet element.
  • Specify the value to be injected into the property of batchlet class in the property element.
  • Refer to JSR352 Specification(external site) for detailed description method of the configuration file.