Skip to content

Commit 1f37312

Browse files
committed
Adding some spring examples
1 parent 93a78c7 commit 1f37312

File tree

3 files changed

+63
-1
lines changed

3 files changed

+63
-1
lines changed

db-scheduler-boot-starter/src/main/java/com/github/kagkarlsson/scheduler/boot/autoconfigure/DbSchedulerAutoConfiguration.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import com.github.kagkarlsson.scheduler.boot.config.DbSchedulerStarter;
2323
import com.github.kagkarlsson.scheduler.boot.config.startup.ContextReadyStart;
2424
import com.github.kagkarlsson.scheduler.boot.config.startup.ImmediateStart;
25+
import com.github.kagkarlsson.scheduler.event.ExecutionInterceptor;
26+
import com.github.kagkarlsson.scheduler.event.SchedulerListener;
2527
import com.github.kagkarlsson.scheduler.exceptions.SerializationException;
2628
import com.github.kagkarlsson.scheduler.serializer.Serializer;
2729
import com.github.kagkarlsson.scheduler.stats.StatsRegistry;
@@ -68,18 +70,24 @@ public class DbSchedulerAutoConfiguration {
6870
private final DbSchedulerProperties config;
6971
private final DataSource existingDataSource;
7072
private final List<Task<?>> configuredTasks;
73+
private final List<SchedulerListener> schedulerListeners;
74+
private final List<ExecutionInterceptor> executionInterceptors;
7175

7276
public DbSchedulerAutoConfiguration(
7377
DbSchedulerProperties dbSchedulerProperties,
7478
DataSource dataSource,
75-
List<Task<?>> configuredTasks) {
79+
List<Task<?>> configuredTasks,
80+
List<SchedulerListener> schedulerListeners,
81+
List<ExecutionInterceptor> executionInterceptors) {
7682
this.config =
7783
Objects.requireNonNull(
7884
dbSchedulerProperties, "Can't configure db-scheduler without required configuration");
7985
this.existingDataSource =
8086
Objects.requireNonNull(dataSource, "An existing javax.sql.DataSource is required");
8187
this.configuredTasks =
8288
Objects.requireNonNull(configuredTasks, "At least one Task must be configured");
89+
this.schedulerListeners = schedulerListeners;
90+
this.executionInterceptors = executionInterceptors;
8391
}
8492

8593
/** Provide an empty customizer if not present in the context. */
@@ -179,6 +187,12 @@ public Scheduler scheduler(DbSchedulerCustomizer customizer, StatsRegistry regis
179187
// Shutdown max wait
180188
builder.shutdownMaxWait(config.getShutdownMaxWait());
181189

190+
// Register listeners
191+
schedulerListeners.forEach(builder::addSchedulerListener);
192+
193+
// Register interceptors
194+
executionInterceptors.forEach(builder::addExecutionInterceptor);
195+
182196
return builder.build();
183197
}
184198

examples/spring-boot-example/src/main/java/com/github/kagkarlsson/examples/boot/config/SchedulerConfiguration.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,26 @@
1515

1616
import com.github.kagkarlsson.scheduler.SchedulerName;
1717
import com.github.kagkarlsson.scheduler.boot.config.DbSchedulerCustomizer;
18+
import com.github.kagkarlsson.scheduler.event.AbstractSchedulerListener;
19+
import com.github.kagkarlsson.scheduler.event.ExecutionInterceptor;
20+
import com.github.kagkarlsson.scheduler.event.SchedulerListener;
1821
import com.github.kagkarlsson.scheduler.serializer.JacksonSerializer;
1922
import com.github.kagkarlsson.scheduler.serializer.Serializer;
23+
import com.github.kagkarlsson.scheduler.task.ExecutionComplete;
2024
import java.util.Optional;
25+
import org.slf4j.Logger;
26+
import org.slf4j.LoggerFactory;
27+
import org.slf4j.MDC;
2128
import org.springframework.context.annotation.Bean;
2229
import org.springframework.context.annotation.Configuration;
2330

2431
@Configuration
2532
public class SchedulerConfiguration {
2633

34+
public static final String MDC_TASKNAME = "task-name";
35+
public static final String MDC_TASKINSTANCEID = "task-instance-id";
36+
private static final Logger LOG = LoggerFactory.getLogger(SchedulerConfiguration.class);
37+
2738
/** Bean defined when a configuration-property in DbSchedulerCustomizer needs to be overridden. */
2839
@Bean
2940
DbSchedulerCustomizer customizer() {
@@ -39,4 +50,33 @@ public Optional<Serializer> serializer() {
3950
}
4051
};
4152
}
53+
54+
@Bean
55+
SchedulerListener schedulerListener() {
56+
return new AbstractSchedulerListener() {
57+
58+
@Override
59+
public void onExecutionComplete(ExecutionComplete executionComplete) {
60+
LOG.info(
61+
"SchedulerListener.onExecutionComplete. Result={}, took={}ms ",
62+
executionComplete.getResult(),
63+
executionComplete.getDuration().toMillis());
64+
}
65+
};
66+
}
67+
68+
@Bean
69+
ExecutionInterceptor mdcExecutionInterceptor() {
70+
return (taskInstance, executionContext, chain) -> {
71+
LOG.info("Setting MDC before execution");
72+
MDC.put(MDC_TASKNAME, taskInstance.getTaskName());
73+
MDC.put(MDC_TASKINSTANCEID, taskInstance.getId());
74+
try {
75+
return chain.proceed(taskInstance, executionContext);
76+
} finally {
77+
MDC.remove(MDC_TASKNAME);
78+
MDC.remove(MDC_TASKINSTANCEID);
79+
}
80+
};
81+
}
4282
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<configuration>
3+
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
4+
<include resource="org/springframework/boot/logging/logback/console-appender.xml" />
5+
<root level="INFO">
6+
<appender-ref ref="CONSOLE" />
7+
</root>
8+
</configuration>

0 commit comments

Comments
 (0)