Skip to content

Commit

Permalink
upgrading to the static TransactionIdMDC API
Browse files Browse the repository at this point in the history
  • Loading branch information
andrus committed Oct 16, 2023
1 parent 679844c commit 86dca9f
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import io.bootique.job.Job;
import io.bootique.job.JobResult;
import io.bootique.job.runtime.GraphExecutor;
import io.bootique.metrics.mdc.TransactionIdMDC;

import java.util.Map;
import java.util.concurrent.ExecutorService;
Expand All @@ -32,11 +31,8 @@
*/
public class InstrumentedGraphExecutor extends GraphExecutor {

private final TransactionIdMDC transactionIdMDC;

public InstrumentedGraphExecutor(ExecutorService pool, TransactionIdMDC transactionIdMDC) {
public InstrumentedGraphExecutor(ExecutorService pool) {
super(pool);
this.transactionIdMDC = transactionIdMDC;
}

@Override
Expand All @@ -46,6 +42,6 @@ public Future<JobResult> submit(Job job, Map<String, Object> params) {
}

protected Job decorateWithGroupTxId(Job job) {
return TxIdAwareGroupMemberJobDecorator.captureCurrentTxId(job, transactionIdMDC);
return TxIdAwareGroupMemberJobDecorator.captureCurrentTxId(job);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import io.bootique.di.Injector;
import io.bootique.job.runtime.GraphExecutor;
import io.bootique.job.scheduler.SchedulerFactory;
import io.bootique.metrics.mdc.TransactionIdMDC;
import io.bootique.shutdown.ShutdownManager;

import java.util.concurrent.ExecutorService;
Expand All @@ -37,6 +36,6 @@ public class InstrumentedSchedulerFactory extends SchedulerFactory {
public GraphExecutor createGraphExecutor(Injector injector, ShutdownManager shutdownManager) {
ExecutorService pool = createGraphExecutorService();
shutdownManager.addShutdownHook(() -> pool.shutdownNow());
return new InstrumentedGraphExecutor(pool, injector.getInstance(TransactionIdMDC.class));
return new InstrumentedGraphExecutor(pool);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import io.bootique.job.runtime.GraphExecutor;
import io.bootique.job.runtime.JobLogger;
import io.bootique.metrics.mdc.TransactionIdGenerator;
import io.bootique.metrics.mdc.TransactionIdMDC;
import io.bootique.shutdown.ShutdownManager;

import javax.inject.Singleton;
Expand All @@ -52,8 +51,8 @@ JobMetricsManager provideJobMetricsManager(MetricRegistry metricRegistry) {

@Provides
@Singleton
JobMDCManager provideJobMDCManager(TransactionIdGenerator generator, TransactionIdMDC mdc) {
return new JobMDCManager(generator, mdc);
JobMDCManager provideJobMDCManager(TransactionIdGenerator generator) {
return new JobMDCManager(generator);
}

@Provides
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,17 @@
public class JobMDCManager {

private final TransactionIdGenerator idGenerator;
private final TransactionIdMDC transactionIdMDC;

JobMDCManager(TransactionIdGenerator idGenerator, TransactionIdMDC transactionIdMDC) {
public JobMDCManager(TransactionIdGenerator idGenerator) {
this.idGenerator = idGenerator;
this.transactionIdMDC = transactionIdMDC;
}

public TransactionIdMDC getTransactionIdMDC() {
return transactionIdMDC;
}

public void onJobStarted() {
String id = idGenerator.nextId();
transactionIdMDC.reset(id);
TransactionIdMDC.setId(id);
}

public void onJobFinished() {
transactionIdMDC.clear();
TransactionIdMDC.clearId();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,14 @@ class TxIdAwareGroupMemberJobDecorator implements Job {

private final Job delegate;
private final String groupMDC;
private final TransactionIdMDC transactionIdMDC;

public static TxIdAwareGroupMemberJobDecorator captureCurrentTxId(Job delegate, TransactionIdMDC transactionIdMDC) {
return new TxIdAwareGroupMemberJobDecorator(delegate, transactionIdMDC.get(), transactionIdMDC);
public static TxIdAwareGroupMemberJobDecorator captureCurrentTxId(Job delegate) {
return new TxIdAwareGroupMemberJobDecorator(delegate, TransactionIdMDC.getId());
}

TxIdAwareGroupMemberJobDecorator(Job delegate, String groupMDC, TransactionIdMDC transactionIdMDC) {
TxIdAwareGroupMemberJobDecorator(Job delegate, String groupMDC) {
this.delegate = delegate;
this.groupMDC = groupMDC;
this.transactionIdMDC = transactionIdMDC;
}

@Override
Expand All @@ -52,11 +50,11 @@ public JobMetadata getMetadata() {
@Override
public JobResult run(Map<String, Object> parameters) {

transactionIdMDC.reset(groupMDC);
TransactionIdMDC.setId(groupMDC);
try {
return delegate.run(parameters);
} finally {
transactionIdMDC.clear();
TransactionIdMDC.clearId();
}
}
}

0 comments on commit 86dca9f

Please sign in to comment.