package com.example.batch; import javax.batch.api.AbstractBatchlet; import javax.batch.runtime.context.JobContext; import javax.inject.Inject; import java.io.BufferedReader; import java.io.FileReader; public class StudentBatchlet extends AbstractBatchlet { @Inject private JobContext jobContext; @Override public String process() throws Exception { String inputFile = jobContext.getProperties().getProperty("inputFile"); try (BufferedReader reader = new BufferedReader(new FileReader(inputFile))) { String line; while ((line = reader.readLine()) != null) { // ... } } return "COMPLETED"; } } <job id="studentJob" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/jobXML_1_0.xsd" version="1.0"> <properties> <property name="inputFile" value="/path/to/student.csv"/> </properties> <step id="studentStep"> <batchlet ref="com.example.batch.StudentBatchlet"/> </step> </job> package com.example.batch; import javax.batch.operations.JobOperator; import javax.batch.runtime.BatchRuntime; public class Main { public static void main(String[] args) throws Exception { JobOperator jobOperator = BatchRuntime.getJobOperator(); long jobId = jobOperator.start("studentJob", new Properties()); jobOperator.waitFor(jobId); } }


上一篇:
下一篇:
切换中文