如何同步 jBatch 执行?
How can I synchronize a jBatch execution?
我正在用 jBeret 编写 jBatch 程序。
我目前就是这样做的。
final JobOperator operator = BatchRuntime.getJobOperator();
logger.debug("operator: {}", operator);
final long id = operator.start("some", null);
logger.debug("id: {}", id);
final JobExecution execution = operator.getJobExecution(id);
logger.debug("execution: {}", execution);
问题是执行似乎 运行 是异步的,主要方法只是 returns。
我能做的最好的事情就是循环直到退出状态不为空。
String status;
while ((status = execution.getExitStatus()) == null) {
//logger.debug("sleeping");
Thread.sleep(1000L);
}
logger.debug("status: {}", status);
还有其他方法吗?
您可以实施 JobListener class, or just extend AbstractJobListener:
...
public class MyJobListener extends AbstractJobListenerJobListener {
// The afterJob method receives control after the job execution ends.
@Override
public void afterJob() throws Exception { ... }
...
}
并且在 afterJob 方法中,您可以使用一些基本的 Java 同步技术(未来左右)。
如果您需要 block-and-wait
,正如您所描述的,没有其他选择,但可以实现 awaiCompletion()
之类的东西。
您的循环方法可以改进。我们以 ThreadPoolExecutor
为例。它有以下方法:
/**
* Blocks until all tasks have completed execution after a shutdown
* request, or the timeout occurs, or the current thread is
* interrupted, whichever happens first.
*
* @param timeout the maximum time to wait
* @param unit the time unit of the timeout argument
* @return {@code true} if this executor terminated and
* {@code false} if the timeout elapsed before termination
* @throws InterruptedException if interrupted while waiting
*/
boolean awaitTermination(long timeout, TimeUnit unit)
throws InterruptedException;
这是实现:
public boolean awaitTermination(long timeout, TimeUnit unit)
throws InterruptedException {
long nanos = unit.toNanos(timeout);
final ReentrantLock mainLock = this.mainLock;
mainLock.lock();
try {
for (;;) {
if (runStateAtLeast(ctl.get(), TERMINATED))
return true;
if (nanos <= 0)
return false;
nanos = termination.awaitNanos(nanos);
}
} finally {
mainLock.unlock();
}
}
请注意:
- 无限循环应该总是定义退出条件
- 在你的情况下,超时是必须的,因为你不太可能准备好无休止的等待
- 自然要知道是超时还是job终止
所以,这是一个改编版本:
public static boolean awaitTermination(JobExecution execution, long timeout) throws InterruptedException {
final long limit = System.currentTimeMillis() + timeout;
for (;;) {
if (null != execution.getExitStatus()) {
return true;
}
if (System.currentTimeMillis() >= limit) {
return false;
}
Thread.sleep(timeout/10);
}
}
JBeret 有一个内部方法:
org.jberet.runtime.JobExecutionImpl#awaitTermination(long timeout, TimeUnit timeUnit);
为此目的。
当运行 JBeret时,您可以在从启动作业获得的JobExecution上调用该方法。
我正在用 jBeret 编写 jBatch 程序。 我目前就是这样做的。
final JobOperator operator = BatchRuntime.getJobOperator();
logger.debug("operator: {}", operator);
final long id = operator.start("some", null);
logger.debug("id: {}", id);
final JobExecution execution = operator.getJobExecution(id);
logger.debug("execution: {}", execution);
问题是执行似乎 运行 是异步的,主要方法只是 returns。
我能做的最好的事情就是循环直到退出状态不为空。
String status;
while ((status = execution.getExitStatus()) == null) {
//logger.debug("sleeping");
Thread.sleep(1000L);
}
logger.debug("status: {}", status);
还有其他方法吗?
您可以实施 JobListener class, or just extend AbstractJobListener:
...
public class MyJobListener extends AbstractJobListenerJobListener {
// The afterJob method receives control after the job execution ends.
@Override
public void afterJob() throws Exception { ... }
...
}
并且在 afterJob 方法中,您可以使用一些基本的 Java 同步技术(未来左右)。
如果您需要 block-and-wait
,正如您所描述的,没有其他选择,但可以实现 awaiCompletion()
之类的东西。
您的循环方法可以改进。我们以 ThreadPoolExecutor
为例。它有以下方法:
/**
* Blocks until all tasks have completed execution after a shutdown
* request, or the timeout occurs, or the current thread is
* interrupted, whichever happens first.
*
* @param timeout the maximum time to wait
* @param unit the time unit of the timeout argument
* @return {@code true} if this executor terminated and
* {@code false} if the timeout elapsed before termination
* @throws InterruptedException if interrupted while waiting
*/
boolean awaitTermination(long timeout, TimeUnit unit)
throws InterruptedException;
这是实现:
public boolean awaitTermination(long timeout, TimeUnit unit)
throws InterruptedException {
long nanos = unit.toNanos(timeout);
final ReentrantLock mainLock = this.mainLock;
mainLock.lock();
try {
for (;;) {
if (runStateAtLeast(ctl.get(), TERMINATED))
return true;
if (nanos <= 0)
return false;
nanos = termination.awaitNanos(nanos);
}
} finally {
mainLock.unlock();
}
}
请注意:
- 无限循环应该总是定义退出条件
- 在你的情况下,超时是必须的,因为你不太可能准备好无休止的等待
- 自然要知道是超时还是job终止
所以,这是一个改编版本:
public static boolean awaitTermination(JobExecution execution, long timeout) throws InterruptedException {
final long limit = System.currentTimeMillis() + timeout;
for (;;) {
if (null != execution.getExitStatus()) {
return true;
}
if (System.currentTimeMillis() >= limit) {
return false;
}
Thread.sleep(timeout/10);
}
}
JBeret 有一个内部方法:
org.jberet.runtime.JobExecutionImpl#awaitTermination(long timeout, TimeUnit timeUnit);
为此目的。
当运行 JBeret时,您可以在从启动作业获得的JobExecution上调用该方法。