Last active
November 16, 2015 07:59
-
-
Save siosio/230b7fa57368023f2d62 to your computer and use it in GitHub Desktop.
ステップの実行状態を一定間隔ごとログに出力するやつ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package siosio | |
import org.jboss.logging.Logger | |
import java.util.concurrent.ExecutorService | |
import java.util.concurrent.Executors | |
import java.util.concurrent.TimeUnit | |
import javax.batch.operations.JobOperator | |
import javax.batch.runtime.BatchRuntime | |
import javax.batch.runtime.BatchStatus | |
import javax.batch.runtime.Metric | |
val logger: Logger = Logger.getLogger("batch-sample") | |
fun main(args: Array<String>) { | |
if (args.isEmpty()) { | |
System.exit(1) | |
} | |
val jobOperator = BatchRuntime.getJobOperator() | |
val executionId = jobOperator.start(args[0], null) | |
val jobExecution = jobOperator.getJobExecution(executionId) | |
logger.log(Logger.Level.INFO, "job start. job name: ${jobExecution.jobName}, executionId=${executionId}") | |
val executor = startWriteMetricLog(jobOperator, executionId) | |
while (true) { | |
if (jobExecution.batchStatus in arrayOf(BatchStatus.ABANDONED, BatchStatus.COMPLETED, BatchStatus.STOPPED)) { | |
logger.log(Logger.Level.INFO, "job end. job name: ${jobExecution.jobName}, batch status: ${jobExecution.batchStatus}, exit status: ${jobExecution.exitStatus}") | |
executor.shutdown() | |
break | |
} | |
TimeUnit.SECONDS.sleep(5) | |
} | |
} | |
fun startWriteMetricLog(jobOperator: JobOperator, executionId: Long): ExecutorService { | |
val scheduledExecutor = Executors.newSingleThreadScheduledExecutor() | |
scheduledExecutor.scheduleAtFixedRate(MetricLogWriter(jobOperator, executionId), 1, 1, TimeUnit.MINUTES) | |
return scheduledExecutor | |
} | |
class MetricLogWriter(val jobOperator: JobOperator, val executionId: Long) : Runnable { | |
override fun run() { | |
val stepExecutions = jobOperator.getStepExecutions(executionId) | |
stepExecutions.find { it.endTime != null }?.let {step -> | |
step.metrics.find { it.type == Metric.MetricType.WRITE_COUNT }?.let { | |
logger.log(Logger.Level.INFO, "step: ${step.stepName}," + | |
" write count: ${it.value}") | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment