001/* 002 * Copyright 2013-2018 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.springframework.batch.test; 017 018import javax.batch.operations.JobOperator; 019import javax.batch.runtime.BatchRuntime; 020import javax.batch.runtime.BatchStatus; 021import javax.batch.runtime.JobExecution; 022import javax.batch.runtime.Metric; 023import javax.batch.runtime.StepExecution; 024import java.util.Date; 025import java.util.Properties; 026import java.util.concurrent.TimeoutException; 027 028import org.springframework.lang.Nullable; 029 030/** 031 * Provides testing utilities to execute JSR-352 jobs and block until they are complete (since all JSR-352 based jobs 032 * are executed asynchronously). 033 * 034 * @author Michael Minella 035 * @author Mahmoud Ben Hassine 036 * @since 3.0 037 */ 038public class JsrTestUtils { 039 040 private static JobOperator operator; 041 042 static { 043 operator = BatchRuntime.getJobOperator(); 044 } 045 046 private JsrTestUtils() {} 047 048 /** 049 * Executes a job and waits for it's status to be any of {@link BatchStatus#STOPPED}, 050 * {@link BatchStatus#COMPLETED}, or {@link BatchStatus#FAILED}. If the job does not 051 * reach one of those statuses within the given timeout, a {@link java.util.concurrent.TimeoutException} is 052 * thrown. 053 * 054 * @param jobName the name of the job. 055 * @param properties job parameters to be associated with the job. 056 * @param timeout maximum amount of time to wait in milliseconds. 057 * @return the {@link JobExecution} for the final state of the job 058 * @throws java.util.concurrent.TimeoutException if the timeout occurs 059 */ 060 public static JobExecution runJob(String jobName, Properties properties, long timeout) throws TimeoutException{ 061 long executionId = operator.start(jobName, properties); 062 JobExecution execution = operator.getJobExecution(executionId); 063 064 Date curDate = new Date(); 065 BatchStatus curBatchStatus = execution.getBatchStatus(); 066 067 while(true) { 068 if(curBatchStatus == BatchStatus.STOPPED || curBatchStatus == BatchStatus.COMPLETED || curBatchStatus == BatchStatus.FAILED) { 069 break; 070 } 071 072 if(new Date().getTime() - curDate.getTime() > timeout) { 073 throw new TimeoutException("Job processing did not complete in time"); 074 } 075 076 execution = operator.getJobExecution(executionId); 077 curBatchStatus = execution.getBatchStatus(); 078 } 079 return execution; 080 } 081 082 /** 083 * Restarts a job and waits for it's status to be any of {@link BatchStatus#STOPPED}, 084 * {@link BatchStatus#COMPLETED}, or {@link BatchStatus#FAILED}. If the job does not 085 * reach one of those statuses within the given timeout, a {@link java.util.concurrent.TimeoutException} is 086 * thrown. 087 * 088 * @param executionId the id of the job execution to restart. 089 * @param properties job parameters to be associated with the job. 090 * @param timeout maximum amount of time to wait in milliseconds. 091 * @return the {@link JobExecution} for the final state of the job 092 * @throws java.util.concurrent.TimeoutException if the timeout occurs 093 */ 094 public static JobExecution restartJob(long executionId, Properties properties, long timeout) throws TimeoutException { 095 long restartId = operator.restart(executionId, properties); 096 JobExecution execution = operator.getJobExecution(restartId); 097 098 Date curDate = new Date(); 099 BatchStatus curBatchStatus = execution.getBatchStatus(); 100 101 while(true) { 102 if(curBatchStatus == BatchStatus.STOPPED || curBatchStatus == BatchStatus.COMPLETED || curBatchStatus == BatchStatus.FAILED) { 103 break; 104 } 105 106 if(new Date().getTime() - curDate.getTime() > timeout) { 107 throw new TimeoutException("Job processing did not complete in time"); 108 } 109 110 execution = operator.getJobExecution(restartId); 111 curBatchStatus = execution.getBatchStatus(); 112 } 113 return execution; 114 } 115 116 @Nullable 117 public static Metric getMetric(StepExecution stepExecution, Metric.MetricType type) { 118 Metric[] metrics = stepExecution.getMetrics(); 119 120 for (Metric metric : metrics) { 121 if(metric.getType() == type) { 122 return metric; 123 } 124 } 125 126 return null; 127 } 128 129}