Updated to set the proper PlatformTransactionManager for Single-Task

Spring integration's DefaultLockRepository requires a TransactionManager.
However it assumes that only one is available.   Since task can have more than one, we are allowed to tell SI which one to use
This commit is contained in:
Glenn Renfro
2022-05-20 16:50:08 -04:00
parent abb95842f9
commit 1284c80c5e
3 changed files with 21 additions and 5 deletions

View File

@@ -84,6 +84,7 @@ public class SimpleTaskAutoConfiguration {
return this.taskRepository;
}
@Bean
public PlatformTransactionManager springCloudTaskTransactionManager() {
return this.platformTransactionManager;

View File

@@ -27,6 +27,7 @@ import org.springframework.cloud.task.listener.annotation.BeforeTask;
import org.springframework.cloud.task.listener.annotation.FailedTask;
import org.springframework.cloud.task.repository.TaskExecution;
import org.springframework.cloud.task.repository.TaskNameResolver;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationListener;
@@ -37,6 +38,7 @@ import org.springframework.integration.leader.event.OnFailedToAcquireMutexEvent;
import org.springframework.integration.leader.event.OnGrantedEvent;
import org.springframework.integration.support.leader.LockRegistryLeaderInitiator;
import org.springframework.integration.support.locks.LockRegistry;
import org.springframework.transaction.PlatformTransactionManager;
/**
* When spring.cloud.task.single-instance-enabled is set to true this listener will create
@@ -67,24 +69,33 @@ public class SingleInstanceTaskListener implements ApplicationListener<Applicati
private TaskProperties taskProperties;
private ApplicationContext applicationContext;
private PlatformTransactionManager platformTransactionManager;
public SingleInstanceTaskListener(LockRegistry lockRegistry,
TaskNameResolver taskNameResolver, TaskProperties taskProperties,
ApplicationEventPublisher applicationEventPublisher) {
ApplicationEventPublisher applicationEventPublisher,
ApplicationContext applicationContext) {
this.lockRegistry = lockRegistry;
this.taskNameResolver = taskNameResolver;
this.taskProperties = taskProperties;
this.lockRegistryLeaderInitiator = new LockRegistryLeaderInitiator(
this.lockRegistry);
this.applicationEventPublisher = applicationEventPublisher;
this.applicationContext = applicationContext;
}
public SingleInstanceTaskListener(DataSource dataSource,
TaskNameResolver taskNameResolver, TaskProperties taskProperties,
ApplicationEventPublisher applicationEventPublisher) {
ApplicationEventPublisher applicationEventPublisher,
ApplicationContext applicationContext) {
this.taskNameResolver = taskNameResolver;
this.applicationEventPublisher = applicationEventPublisher;
this.dataSource = dataSource;
this.taskProperties = taskProperties;
this.applicationContext = applicationContext;
this.platformTransactionManager = this.applicationContext.getBean("springCloudTaskTransactionManager", PlatformTransactionManager.class);
}
@BeforeTask
@@ -149,7 +160,10 @@ public class SingleInstanceTaskListener implements ApplicationListener<Applicati
String.valueOf(executionId));
lockRepository.setPrefix(this.taskProperties.getTablePrefix());
lockRepository.setTimeToLive(this.taskProperties.getSingleInstanceLockTtl());
lockRepository.setApplicationContext(this.applicationContext);
lockRepository.afterPropertiesSet();
lockRepository.setTransactionManager(this.platformTransactionManager);
lockRepository.afterSingletonsInstantiated();
return new JdbcLockRegistry(lockRepository);
}

View File

@@ -19,6 +19,7 @@ package org.springframework.cloud.task.configuration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cloud.task.repository.TaskNameResolver;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@@ -49,14 +50,14 @@ public class SingleTaskConfiguration {
private TaskConfigurer taskConfigurer;
@Bean
public SingleInstanceTaskListener taskListener(TaskNameResolver resolver) {
public SingleInstanceTaskListener taskListener(TaskNameResolver resolver, ApplicationContext applicationContext) {
if (this.taskConfigurer.getTaskDataSource() == null) {
return new SingleInstanceTaskListener(new PassThruLockRegistry(), resolver,
this.taskProperties, this.applicationEventPublisher);
this.taskProperties, this.applicationEventPublisher, applicationContext);
}
return new SingleInstanceTaskListener(this.taskConfigurer.getTaskDataSource(),
resolver, this.taskProperties, this.applicationEventPublisher);
resolver, this.taskProperties, this.applicationEventPublisher, applicationContext);
}
}