Skip to content

Commit 9544725

Browse files
committed
adding a new sample to schedule Batch Jobs
1 parent dca8de3 commit 9544725

File tree

6 files changed

+112
-0
lines changed

6 files changed

+112
-0
lines changed

batch/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,6 @@
2929
<module>multiple-steps</module>
3030
<module>split</module>
3131
<module>chunk-simple-nobeans</module>
32+
<module>scheduling</module>
3233
</modules>
3334
</project>

batch/scheduling/pom.xml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>org.javaee7.batch</groupId>
7+
<artifactId>batch-samples</artifactId>
8+
<version>1.0-SNAPSHOT</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
12+
<artifactId>scheduling</artifactId>
13+
<packaging>war</packaging>
14+
<name>Batch Schedule</name>
15+
<description>Scheduling a Batch Job</description>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>org.javaee7</groupId>
20+
<artifactId>util-samples</artifactId>
21+
</dependency>
22+
</dependencies>
23+
</project>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package org.samples.wildfly.scheduling.batch;
2+
3+
import java.util.Properties;
4+
import javax.batch.runtime.BatchRuntime;
5+
6+
/**
7+
* @author arungupta
8+
*/
9+
public class MyJob implements Runnable {
10+
11+
public void run() {
12+
BatchRuntime.getJobOperator().start("myJob", new Properties());
13+
}
14+
15+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.samples.wildfly.scheduling.batch;
2+
3+
import java.util.Properties;
4+
import javax.batch.runtime.BatchRuntime;
5+
import javax.ejb.Schedule;
6+
import javax.ejb.Singleton;
7+
8+
/**
9+
* @author arungupta
10+
*/
11+
@Singleton
12+
public class MySingletonEJB {
13+
@Schedule(hour = "23", minute = "59", second = "59")
14+
public void myJob() {
15+
BatchRuntime.getJobOperator().start("myJob", new Properties());
16+
}
17+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package org.samples.wildfly.scheduling.batch;
2+
3+
import java.util.Calendar;
4+
import java.util.Date;
5+
import java.util.concurrent.TimeUnit;
6+
import javax.annotation.Resource;
7+
import javax.ejb.Stateless;
8+
import javax.enterprise.concurrent.LastExecution;
9+
import javax.enterprise.concurrent.ManagedScheduledExecutorService;
10+
import javax.enterprise.concurrent.Trigger;
11+
12+
/**
13+
* @author arungupta
14+
*/
15+
@Stateless
16+
public class MyStatelessEJB {
17+
@Resource
18+
ManagedScheduledExecutorService executor;
19+
20+
public void runJob() {
21+
executor.schedule(new MyJob(), new Trigger() {
22+
23+
@Override
24+
public Date getNextRunTime(LastExecution lastExecutionInfo, Date taskScheduledTime) {
25+
Calendar cal = Calendar.getInstance();
26+
cal.setTime(taskScheduledTime);
27+
cal.add(Calendar.DATE, 1);
28+
return cal.getTime();
29+
}
30+
31+
@Override
32+
public boolean skipRun(LastExecution lastExecutionInfo, Date scheduledRunTime) {
33+
return null == lastExecutionInfo;
34+
}
35+
36+
});
37+
}
38+
39+
public void cancelJob() {
40+
executor.shutdown();
41+
}
42+
43+
public void runJob2() {
44+
executor.scheduleWithFixedDelay(new MyJob(), 1, 2, TimeUnit.MINUTES);
45+
}
46+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Start Page</title>
5+
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
6+
</head>
7+
<body>
8+
<h1>Hello World!</h1>
9+
</body>
10+
</html>

0 commit comments

Comments
 (0)