diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/job/builder/JobBuilderHelper.java b/spring-batch-core/src/main/java/org/springframework/batch/core/job/builder/JobBuilderHelper.java index c054425ea..df1f262e0 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/job/builder/JobBuilderHelper.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/job/builder/JobBuilderHelper.java @@ -1,5 +1,5 @@ /* - * Copyright 2006-2022 the original author or authors. + * Copyright 2006-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -34,6 +34,8 @@ import org.springframework.batch.core.annotation.AfterJob; import org.springframework.batch.core.annotation.BeforeJob; import org.springframework.batch.core.job.AbstractJob; import org.springframework.batch.core.listener.JobListenerFactoryBean; +import org.springframework.batch.core.observability.BatchJobObservationConvention; +import org.springframework.batch.core.observability.DefaultBatchJobObservationConvention; import org.springframework.batch.core.repository.JobRepository; import org.springframework.batch.support.ReflectionUtils; @@ -102,6 +104,18 @@ public abstract class JobBuilderHelper> { return result; } + /** + * Sets the job observation convention. + * @param observationConvention the job observation convention (optional) + * @return this to enable fluent chaining + */ + public B observationConvention(BatchJobObservationConvention observationConvention) { + properties.observationConvention = observationConvention; + @SuppressWarnings("unchecked") + B result = (B) this; + return result; + } + /** * Sets the observation registry for the job. * @param observationRegistry the observation registry (optional) @@ -193,6 +207,10 @@ public abstract class JobBuilderHelper> { if (jobParametersValidator != null) { job.setJobParametersValidator(jobParametersValidator); } + BatchJobObservationConvention observationConvention = properties.getObservationConvention(); + if (observationConvention != null) { + job.setObservationConvention(observationConvention); + } ObservationRegistry observationRegistry = properties.getObservationRegistry(); if (observationRegistry != null) { job.setObservationRegistry(observationRegistry); @@ -221,6 +239,8 @@ public abstract class JobBuilderHelper> { private JobRepository jobRepository; + private BatchJobObservationConvention observationConvention = new DefaultBatchJobObservationConvention(); + private ObservationRegistry observationRegistry; private MeterRegistry meterRegistry; @@ -236,6 +256,7 @@ public abstract class JobBuilderHelper> { this.name = properties.name; this.restartable = properties.restartable; this.jobRepository = properties.jobRepository; + this.observationConvention = properties.observationConvention; this.observationRegistry = properties.observationRegistry; this.meterRegistry = properties.meterRegistry; this.jobExecutionListeners = new LinkedHashSet<>(properties.jobExecutionListeners); @@ -267,6 +288,14 @@ public abstract class JobBuilderHelper> { this.jobRepository = jobRepository; } + public BatchJobObservationConvention getObservationConvention() { + return observationConvention; + } + + public void setObservationConvention(BatchJobObservationConvention observationConvention) { + this.observationConvention = observationConvention; + } + public ObservationRegistry getObservationRegistry() { return observationRegistry; } diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/step/builder/StepBuilderHelper.java b/spring-batch-core/src/main/java/org/springframework/batch/core/step/builder/StepBuilderHelper.java index 9d6c5c285..3a7110655 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/step/builder/StepBuilderHelper.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/step/builder/StepBuilderHelper.java @@ -1,5 +1,5 @@ /* - * Copyright 2006-2022 the original author or authors. + * Copyright 2006-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -31,6 +31,8 @@ import org.springframework.batch.core.StepExecutionListener; import org.springframework.batch.core.annotation.AfterStep; import org.springframework.batch.core.annotation.BeforeStep; import org.springframework.batch.core.listener.StepListenerFactoryBean; +import org.springframework.batch.core.observability.BatchStepObservationConvention; +import org.springframework.batch.core.observability.DefaultBatchStepObservationConvention; import org.springframework.batch.core.repository.JobRepository; import org.springframework.batch.core.step.AbstractStep; import org.springframework.batch.support.ReflectionUtils; @@ -70,6 +72,11 @@ public abstract class StepBuilderHelper> { return self(); } + public B observationConvention(BatchStepObservationConvention observationConvention) { + properties.observationConvention = observationConvention; + return self(); + } + public B observationRegistry(ObservationRegistry observationRegistry) { properties.observationRegistry = observationRegistry; return self(); @@ -131,6 +138,11 @@ public abstract class StepBuilderHelper> { protected void enhance(AbstractStep step) { step.setJobRepository(properties.getJobRepository()); + BatchStepObservationConvention observationConvention = properties.getObservationConvention(); + if (observationConvention != null) { + step.setObservationConvention(observationConvention); + } + ObservationRegistry observationRegistry = properties.getObservationRegistry(); if (observationRegistry != null) { step.setObservationRegistry(observationRegistry); @@ -164,6 +176,8 @@ public abstract class StepBuilderHelper> { private JobRepository jobRepository; + private BatchStepObservationConvention observationConvention = new DefaultBatchStepObservationConvention(); + private ObservationRegistry observationRegistry = ObservationRegistry.NOOP; private MeterRegistry meterRegistry = Metrics.globalRegistry; @@ -176,6 +190,7 @@ public abstract class StepBuilderHelper> { this.startLimit = properties.startLimit; this.allowStartIfComplete = properties.allowStartIfComplete; this.jobRepository = properties.jobRepository; + this.observationConvention = properties.observationConvention; this.observationRegistry = properties.observationRegistry; this.meterRegistry = properties.meterRegistry; this.stepExecutionListeners = new ArrayList<>(properties.stepExecutionListeners); @@ -189,6 +204,14 @@ public abstract class StepBuilderHelper> { this.jobRepository = jobRepository; } + public BatchStepObservationConvention getObservationConvention() { + return observationConvention; + } + + public void setObservationConvention(BatchStepObservationConvention observationConvention) { + this.observationConvention = observationConvention; + } + public ObservationRegistry getObservationRegistry() { return observationRegistry; }