use of javax.batch.operations.JobOperator in project wildfly by wildfly.
the class JobControlTestCase method testRestartOnExecutionResource.
@Test
public void testRestartOnExecutionResource() throws Exception {
final ModelNode address = Operations.createAddress("deployment", DEPLOYMENT_NAME, "subsystem", "batch-jberet");
ModelNode op = Operations.createOperation("start-job", address);
op.get("job-xml-name").set("test-chunk");
ModelNode properties = op.get("properties");
properties.get("reader.end").set("20");
// We're adding a long wait time to ensure we can stop, 1 seconds should be okay
properties.get("writer.sleep.time").set(Integer.toString(TimeoutUtil.adjust(1000)));
ModelNode result = executeOperation(op);
long executionId = result.asLong();
Assert.assertTrue("Execution id should be greater than 0", executionId > 0L);
// Test the stop operation
final ModelNode executionAddress = Operations.createAddress("deployment", DEPLOYMENT_NAME, "subsystem", "batch-jberet", "job", "test-chunk", "execution", Long.toString(executionId));
executeOperation(Operations.createOperation("stop-job", executionAddress));
// Validate that the job as executed
final JobOperator jobOperator = BatchRuntime.getJobOperator();
JobExecution execution = jobOperator.getJobExecution(executionId);
Assert.assertNotNull(execution);
// Wait for 1 seconds max for the execution to finish.
waitForTermination(execution, 3);
// Reset the counter as we're not sure how many were actually written
currentCount = countingItemWriter.getWrittenItemSize();
// Check that the status is stopped
Assert.assertEquals(BatchStatus.STOPPED, execution.getBatchStatus());
// Restart the execution
op = Operations.createOperation("restart-job", executionAddress);
properties = op.get("properties");
properties.get("reader.end").set("10");
properties.get("writer.sleep.time").set("0");
result = executeOperation(op);
executionId = result.asLong();
Assert.assertTrue("Execution id should be greater than 0", executionId > 0L);
execution = jobOperator.getJobExecution(executionId);
Assert.assertNotNull(execution);
// Wait for 3 seconds max for the execution to finish.
waitForTermination(execution, 3);
// Check that the status is stopped
Assert.assertEquals(BatchStatus.COMPLETED, execution.getBatchStatus());
}
use of javax.batch.operations.JobOperator in project wildfly by wildfly.
the class BatchSubsystemSecurityTestCase method waitForJobEnd.
private void waitForJobEnd(Long id, int timeoutSeconds) throws TimeoutException {
Long start = System.currentTimeMillis();
final JobOperator operator = BatchRuntime.getJobOperator();
while (System.currentTimeMillis() - start < (TimeoutUtil.adjust(timeoutSeconds) * 1000)) {
if (operator.getJobExecution(id).getEndTime() != null)
return;
}
throw new TimeoutException();
}
use of javax.batch.operations.JobOperator in project Payara by payara.
the class BatchRuntimeHelper method event.
@Override
public void event(Event event) {
try {
if (event.is(EventTypes.SERVER_READY)) {
for (String appName : applicationRegistry.getAllApplicationNames()) {
ApplicationInfo applicationInfo = applicationRegistry.get(appName);
registerIfBatchJobsDirExists(applicationInfo);
}
} else if (event.is(Deployment.APPLICATION_LOADED)) {
if (event.hook() != null && event.hook() instanceof ApplicationInfo) {
ApplicationInfo applicationInfo = (ApplicationInfo) event.hook();
registerIfBatchJobsDirExists(applicationInfo);
}
}
if (event.is(Deployment.UNDEPLOYMENT_SUCCESS)) {
if (event.hook() != null && event.hook() instanceof DeploymentContextImpl) {
DeploymentContextImpl deploymentContext = (DeploymentContextImpl) event.hook();
Properties props = deploymentContext.getAppProps();
String appName = props.getProperty("defaultAppName");
if (!Boolean.parseBoolean(props.getProperty("retain-batch-jobs"))) {
String tagName = config.getName() + ":" + appName;
ClassLoader prevCL = Thread.currentThread().getContextClassLoader();
try {
// set TCCL to ensure loading of the Joboperator
Thread.currentThread().setContextClassLoader(BatchSPIManager.class.getClassLoader());
BatchSPIManager batchSPIManager = BatchSPIManager.getInstance();
if (batchSPIManager != null && batchSPIManager.getBatchJobUtil() != null) {
batchSPIManager.getBatchJobUtil().purgeOwnedRepositoryData(tagName);
tagNamesRequiringCleanup.remove(tagName);
} else if (tagNamesRequiringCleanup.contains(tagName)) {
// Force initialization of BatchRuntime
JobOperator jobOperator = BatchRuntime.getJobOperator();
if (batchSPIManager.getBatchJobUtil() != null) {
batchSPIManager.getBatchJobUtil().purgeOwnedRepositoryData(tagName);
tagNamesRequiringCleanup.remove(tagName);
}
}
} catch (Exception ex) {
logger.log(Level.FINE, "Error while purging jobs", ex);
} finally {
Thread.currentThread().setContextClassLoader(prevCL);
}
}
}
}
} catch (Exception ex) {
logger.log(Level.FINE, "Exception while handling event: " + event, ex);
}
}
use of javax.batch.operations.JobOperator in project quickstart by wildfly.
the class BatchController method getJobsExecution.
public Set<JobData> getJobsExecution() {
Set<JobData> jobsData = new TreeSet<JobData>(new Comparator<JobData>() {
@Override
public int compare(JobData o1, JobData o2) {
return o2.getJobInstanceId().compareTo(o1.getJobInstanceId());
}
});
Map<Long, JobExecution> jobIntances = new HashMap<>();
JobOperator jobOperator = BatchRuntime.getJobOperator();
try {
List<JobInstance> instances = jobOperator.getJobInstances("import-file", 0, jobOperator.getJobInstanceCount("import-file"));
for (JobInstance ji : instances) {
List<JobExecution> executions = jobOperator.getJobExecutions(ji);
for (JobExecution jobExecution : executions) {
// initialize the map if null
if (jobIntances.get(ji.getInstanceId()) == null) {
jobIntances.put(ji.getInstanceId(), jobExecution);
}
// Update the jobExecution if is newer
JobExecution existing = jobIntances.get(ji.getInstanceId());
if (jobExecution.getExecutionId() > existing.getExecutionId()) {
jobIntances.put(ji.getInstanceId(), jobExecution);
}
}
}
for (Long instaceId : jobIntances.keySet()) {
JobExecution jobExecution = jobIntances.get(instaceId);
JobInstance ji = jobOperator.getJobInstance(jobExecution.getExecutionId());
Properties parameters = jobOperator.getParameters(jobExecution.getExecutionId());
jobsData.add(new JobData(ji.getInstanceId(), jobExecution.getExecutionId(), ji.getJobName(), jobExecution.getCreateTime(), jobExecution.getEndTime(), jobExecution.getBatchStatus(), parameters, jobExecution.getExitStatus()));
}
} catch (NoSuchJobException e) {
// It's ok if when doesn't have any jobs yet to show
}
return jobsData;
}
use of javax.batch.operations.JobOperator in project quickstart by wildfly.
the class BatchController method restartJob.
public void restartJob(int executionId) {
JobOperator jobOperator = BatchRuntime.getJobOperator();
Properties jobParameters = jobOperator.getParameters(executionId);
jobParameters.setProperty("restartedOnce", "true");
long newExecutionId = jobOperator.restart(executionId, jobParameters);
facesContext.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "Execution " + executionId + " restarted! New execution id: " + newExecutionId, null));
}
Aggregations