<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>jbatch-example</groupId> <artifactId>jbatch-example</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <failOnMissingWebXml>false</failOnMissingWebXml> </properties> <dependencies> <dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifactId> <version>7.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.apache.batchee</groupId> <artifactId>batchee-servlet-embedded</artifactId> <version>0.2-incubating</version> </dependency> </dependencies> </project>
Entries tagged [batchee]
JBatch examples: get started JBatch with WildFly and batchee-servlet-embedded
TweetPosted on Sunday May 24, 2015 at 12:43PM in JBatch
JSR352 aka JBatch is the standardized batch processing framework for the Java EE platform. it eases tedious work on batch programming such as transaction management of bulk processing, parallel processing, flow control. and it gives well-integrated job information management mechanism, well-designed interfaces that enables us to develop common modules for frequently use. there are some convenient modules aim to be used in typical situation. in this entry, I introduce you some examples to get started.
Setup
Setup WildFly 9.0.0.CR1: download the full distribution from wildfly.org and unpack.
Next, create a war application contains following resources:
pom.xml
This contains a dependency to batchee-servlet-embedded
. it brings a simple web application which enables us to control batch jobs, also it supplies simple REST style interface.
/src/main/java/jbatch/MyBatchlet.java
@Named @Dependent public class MyBatchlet extends AbstractBatchlet { @Override public String process() throws Exception { System.out.println("Hello, JBatch"); return null; } }
/src/main/resources/META-INF/batch-jobs/simple-job.xml
<job id="simple-job" version="1.0" xmlns="http://xmlns.jcp.org/xml/ns/javaee"> <step id="myStep"> <batchlet ref="myBatchlet"/> </step> </job>
Deploy and run the batch
Then deploy the war, and go to http://localhost:8080/jbatch-example-1.0-SNAPSHOT/jbatch/ from your browser. you’ll see following page:
Then click New Batch button. you’ll be transited to following page. enter simple-job
to the text box, then click Set Job Name, and click Submit
If the batch executed successfully, you’ll be transited to following page:
Also you’ll see following output in your WildFly console:
12:23:02,046 INFO [stdout] (Batch Thread - 2) Hello, JBatch
You can see job execution history from the web. click simple-job
in home page and you’ll be transited following page:
Instead of using web browser, you can launch a job with simple REST style API as follows:
curl http://localhost:8080/jbatch-example-1.0-SNAPSHOT/jbatch/rest/start/simple-job
For details of the REST API, you can see help with following command:
curl http://localhost:8080/jbatch-example-1.0-SNAPSHOT/jbatch/rest/
It shows:
Known commands are: * start/ - start a new batch job Sample: http://localhost:8080/myapp/jbatch/rest/start/myjobname?param1=x¶m2=y BatchEE will start the job and immediately return * status/ - query the current status Sample: http://localhost:8080/myapp/jbatch/rest/status/23 will return the state of executionId 23 * stop/ - stop the job with the given executionId Sample: http://localhost:8080/myapp/jbatch/rest/stop/23 will stop the job with executionId 23 * restart/ - restart the job with the given executionId Sample: http://localhost:8080/myapp/jbatch/rest/restart/23 will restart the job with executionId 23
The project which used in this entry can be obtained from my GitHub repository.
Using Apache BatchEE's server API with JBeret
TweetPosted on Wednesday Mar 04, 2015 at 06:11PM in JBatch
Apache BatchEE is a fork of the JSR352 reference implementation with many additional features. it has useful REST APIs built on JAX-RS so we can manipulate (start, stop, restart, and so on) batches through REST API. fortunately, it is well modularized so we can use its REST API implementation with other JSR352 implementation such as JBeret.
I created an example project which works with WildFly 8.2.0.Final and its JSR352 implementation JBeret. after deploy, issue following command:
curl -H 'Content-Type: application/json' \ -d '{"entries": [ {"key": "myKey1", "value": "myValue1"}, {"key": "myKey2", "value": "myValue2"} ]}' \ http://localhost:8080/batcheetest/jbatch/batchee/execution/start/myjob
Then you’ll see following output in WildFly console:
17:57:18,608 INFO [stdout] (Batch Thread - 9) Hello world! 17:57:18,609 INFO [stdout] (Batch Thread - 9) Job Parameters: {myKey2=myValue2, myKey1=myValue1}
It’s much better than create a servlet which kicks the batch.
And I haven’t tested yet, it also have useful client API for that REST API. we can use JobOperator
transparently thanks to its proxy. for details of REST API and BatchEE, see following URLs:
UPDATE:
I added a test case which uses client API of BatchEE, but it doesn’t work with released version. you need to apply a patch by hand. for details refer https://issues.apache.org/jira/browse/BATCHEE-59
Also I created an example of a test class which uses Arquillian. this won’t work with 0.2-incubating but will work with future versions.
@RunWith(Arquillian.class) public class MyJobArquillianIT { @ArquillianResource private URL url; private JobOperator jobOperator; @Deployment(testable = false) public static Archive<?> war() { final File[] files = Maven.configureResolver() .loadPomFromFile("pom.xml") .resolve("org.apache.batchee:batchee-jaxrs-server") .withTransitivity() .asFile(); return ShrinkWrap.create(WebArchive.class) .addClass(HelloBatchlet.class) .addAsResource("META-INF/batch-jobs/myjob.xml") .addAsLibraries(files); } @Before public void before() { jobOperator = BatchEEJAXRSClientFactory.newClient(url.toExternalForm() + "jbatch"); } @Test public void test() { Properties jobParameters = new Properties(); jobParameters.setProperty("someKey", "someValue"); final JobExecution jobExecution = waitForFinish(jobOperator.start("myjob", jobParameters)); Assert.assertEquals(BatchStatus.COMPLETED, jobExecution.getBatchStatus()); } private static final Collection<BatchStatus> BATCH_END_STATUSES = EnumSet.of(BatchStatus.COMPLETED, BatchStatus.FAILED, BatchStatus.STOPPED, BatchStatus.ABANDONED); private JobExecution waitForFinish(long executionId) { JobExecution jobExecution; while (!BATCH_END_STATUSES.contains((jobExecution = jobOperator.getJobExecution(executionId)).getBatchStatus())) { try { Thread.sleep(100); } catch (InterruptedException e) { throw new RuntimeException(e); } } return jobExecution; } }
Web frontend
Its web-frontend GUI (batchee-servlet-embedded
) works even against JBeret runtime as follows. you can view executions, jobs and its definition, and can start jobs with custom parameters.
It also exposes simple-rest
API which is more useful when issue the command by hand or some cases (e.g. cron job). in case of this example, you can start a job as follows:
$ curl 'http://localhost:8080/batcheetest/jbatch-gui/rest/start/myjob?param1=x¶m2=y' 5 OK
You can read its help as follows:
$ curl 'http://localhost:8080/batcheetest/jbatch-gui/rest/' -1 FAILURE Unknown command: The returned response if of MIME type text/plain and contains the following information {jobExecutionId} (or -1 if no executionId was detected)\n OK (or FAILURE)\n followed by command specific information Known commands are: * start/ - start a new batch job Sample: http://localhost:8080/myapp/jbatch/rest/start/myjobname?param1=x¶m2=y BatchEE will start the job and immediately return * status/ - query the current status Sample: http://localhost:8080/myapp/jbatch/rest/status/23 will return the state of executionId 23 * stop/ - stop the job with the given executionId Sample: http://localhost:8080/myapp/jbatch/rest/stop/23 will stop the job with executionId 23 * restart/ - restart the job with the given executionId Sample: http://localhost:8080/myapp/jbatch/rest/restart/23 will restart the job with executionId 23
Tags: arquillian batchee jbatch jberet wildfly