Task identifies if JPA is in use and uses JPTransactionManager
resolves #309
This commit is contained in:
committed by
Michael Minella
parent
3b8670e765
commit
b03f8398a7
@@ -16,8 +16,12 @@
|
||||
|
||||
package org.springframework.cloud.task.configuration;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.batch.support.transaction.ResourcelessTransactionManager;
|
||||
import org.springframework.cloud.task.repository.TaskExplorer;
|
||||
import org.springframework.cloud.task.repository.TaskRepository;
|
||||
@@ -26,7 +30,9 @@ import org.springframework.cloud.task.repository.dao.MapTaskExecutionDao;
|
||||
import org.springframework.cloud.task.repository.support.SimpleTaskExplorer;
|
||||
import org.springframework.cloud.task.repository.support.SimpleTaskRepository;
|
||||
import org.springframework.cloud.task.repository.support.TaskExecutionDaoFactoryBean;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
|
||||
import org.springframework.orm.jpa.JpaTransactionManager;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
|
||||
/**
|
||||
@@ -44,6 +50,8 @@ import org.springframework.transaction.PlatformTransactionManager;
|
||||
*/
|
||||
public class DefaultTaskConfigurer implements TaskConfigurer {
|
||||
|
||||
private static final Log logger = LogFactory.getLog(DefaultTaskConfigurer.class);
|
||||
|
||||
private TaskRepository taskRepository;
|
||||
|
||||
private TaskExplorer taskExplorer;
|
||||
@@ -54,6 +62,8 @@ public class DefaultTaskConfigurer implements TaskConfigurer {
|
||||
|
||||
private DataSource dataSource;
|
||||
|
||||
private ApplicationContext context;
|
||||
|
||||
public DefaultTaskConfigurer() {
|
||||
this(TaskProperties.DEFAULT_TABLE_PREFIX);
|
||||
}
|
||||
@@ -67,7 +77,7 @@ public class DefaultTaskConfigurer implements TaskConfigurer {
|
||||
* production use.
|
||||
*/
|
||||
public DefaultTaskConfigurer(DataSource dataSource) {
|
||||
this(dataSource, TaskProperties.DEFAULT_TABLE_PREFIX);
|
||||
this(dataSource, TaskProperties.DEFAULT_TABLE_PREFIX, null);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -77,7 +87,7 @@ public class DefaultTaskConfigurer implements TaskConfigurer {
|
||||
* task infrastructure.
|
||||
*/
|
||||
public DefaultTaskConfigurer(String tablePrefix) {
|
||||
this(null, tablePrefix);
|
||||
this(null, tablePrefix, null);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -89,8 +99,9 @@ public class DefaultTaskConfigurer implements TaskConfigurer {
|
||||
* @param tablePrefix the prefix to apply to the task table names used by
|
||||
* task infrastructure.
|
||||
*/
|
||||
public DefaultTaskConfigurer(DataSource dataSource, String tablePrefix) {
|
||||
public DefaultTaskConfigurer(DataSource dataSource, String tablePrefix, ApplicationContext context) {
|
||||
this.dataSource = dataSource;
|
||||
this.context = context;
|
||||
|
||||
if(this.dataSource != null) {
|
||||
this.taskExecutionDaoFactoryBean = new
|
||||
@@ -120,11 +131,26 @@ public class DefaultTaskConfigurer implements TaskConfigurer {
|
||||
|
||||
@Override
|
||||
public PlatformTransactionManager getTransactionManager() {
|
||||
if(this.transactionManager == null) {
|
||||
if(isDataSourceAvailable()) {
|
||||
this.transactionManager = new DataSourceTransactionManager(this.dataSource);
|
||||
if (this.transactionManager == null) {
|
||||
if (isDataSourceAvailable()) {
|
||||
try {
|
||||
Class.forName("javax.persistence.EntityManager");
|
||||
if (this.context != null && this.context.getBeanNamesForType(EntityManager.class).length > 0) {
|
||||
logger.debug("EntityManager was found, using JpaTransactionManager");
|
||||
this.transactionManager = new JpaTransactionManager();
|
||||
}
|
||||
}
|
||||
catch (ClassNotFoundException ignore) {
|
||||
logger.debug("No EntityManager was found, using DataSourceTransactionManager");
|
||||
}
|
||||
finally {
|
||||
if (this.transactionManager == null) {
|
||||
this.transactionManager = new DataSourceTransactionManager(this.dataSource);
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
logger.debug("No DataSource was found, using ResourcelessTransactionManager");
|
||||
this.transactionManager = new ResourcelessTransactionManager();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -157,11 +157,10 @@ public class SimpleTaskConfiguration {
|
||||
if(!CollectionUtils.isEmpty(this.dataSources) && this.dataSources.size() == 1) {
|
||||
taskConfigurer = new DefaultTaskConfigurer(
|
||||
this.dataSources.iterator().next(),
|
||||
taskProperties.getTablePrefix());
|
||||
taskProperties.getTablePrefix(), context);
|
||||
}
|
||||
else {
|
||||
taskConfigurer = new DefaultTaskConfigurer(
|
||||
taskProperties.getTablePrefix());
|
||||
taskConfigurer = new DefaultTaskConfigurer(taskProperties.getTablePrefix());
|
||||
}
|
||||
this.context.getBeanFactory().registerSingleton("taskConfigurer", taskConfigurer);
|
||||
return taskConfigurer;
|
||||
|
||||
@@ -0,0 +1,117 @@
|
||||
/*
|
||||
* Copyright 2017 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cloud.task.configuration;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.CoreMatchers.nullValue;
|
||||
import static org.hamcrest.core.IsNull.notNullValue;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* @author Glenn Renfro
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@ContextConfiguration(classes = { EmbeddedDataSourceConfiguration.class })
|
||||
public class DefaultTaskConfigurerTests {
|
||||
|
||||
@Autowired
|
||||
DataSource dataSource;
|
||||
|
||||
@Autowired
|
||||
ApplicationContext context;
|
||||
|
||||
@Test
|
||||
public void resourcelessTransactionManagerTest() {
|
||||
DefaultTaskConfigurer defaultTaskConfigurer = new DefaultTaskConfigurer();
|
||||
assertThat(defaultTaskConfigurer.getTransactionManager().getClass().getName(),
|
||||
is("org.springframework.batch.support.transaction.ResourcelessTransactionManager"));
|
||||
defaultTaskConfigurer = new DefaultTaskConfigurer("foo");
|
||||
assertThat(defaultTaskConfigurer.getTransactionManager().getClass().getName(),
|
||||
is("org.springframework.batch.support.transaction.ResourcelessTransactionManager"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefaultContext() throws Exception {
|
||||
AnnotationConfigApplicationContext localContext = new AnnotationConfigApplicationContext();
|
||||
localContext.register(EmbeddedDataSourceConfiguration.class,EntityManagerConfiguration.class);
|
||||
localContext.refresh();
|
||||
DefaultTaskConfigurer defaultTaskConfigurer = new DefaultTaskConfigurer(dataSource, TaskProperties.DEFAULT_TABLE_PREFIX, localContext);
|
||||
assertThat(defaultTaskConfigurer.getTransactionManager().getClass().getName(), is(equalTo("org.springframework.orm.jpa.JpaTransactionManager")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void dataSourceTransactionManagerTest() {
|
||||
DefaultTaskConfigurer defaultTaskConfigurer = new DefaultTaskConfigurer(dataSource);
|
||||
assertThat(defaultTaskConfigurer.getTransactionManager().getClass().getName(),
|
||||
is("org.springframework.jdbc.datasource.DataSourceTransactionManager"));
|
||||
defaultTaskConfigurer = new DefaultTaskConfigurer(dataSource, "FOO", null);
|
||||
assertThat(defaultTaskConfigurer.getTransactionManager().getClass().getName(),
|
||||
is("org.springframework.jdbc.datasource.DataSourceTransactionManager"));
|
||||
defaultTaskConfigurer = new DefaultTaskConfigurer(dataSource, "FOO", context);
|
||||
assertThat(defaultTaskConfigurer.getTransactionManager().getClass().getName(),
|
||||
is("org.springframework.jdbc.datasource.DataSourceTransactionManager"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void taskExplorerTest() {
|
||||
DefaultTaskConfigurer defaultTaskConfigurer = new DefaultTaskConfigurer(dataSource);
|
||||
assertThat(defaultTaskConfigurer.getTaskExplorer(), is(notNullValue()));
|
||||
defaultTaskConfigurer = new DefaultTaskConfigurer();
|
||||
assertThat(defaultTaskConfigurer.getTaskExplorer(), is(notNullValue()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void taskRepositoryTest() {
|
||||
DefaultTaskConfigurer defaultTaskConfigurer = new DefaultTaskConfigurer(dataSource);
|
||||
assertThat(defaultTaskConfigurer.getTaskRepository(), is(notNullValue()));
|
||||
defaultTaskConfigurer = new DefaultTaskConfigurer();
|
||||
assertThat(defaultTaskConfigurer.getTaskRepository(), is(notNullValue()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void taskDataSource() {
|
||||
DefaultTaskConfigurer defaultTaskConfigurer = new DefaultTaskConfigurer(dataSource);
|
||||
assertThat(defaultTaskConfigurer.getTaskDataSource(), is(notNullValue()));
|
||||
defaultTaskConfigurer = new DefaultTaskConfigurer();
|
||||
assertThat(defaultTaskConfigurer.getTaskDataSource(), is(nullValue()));
|
||||
}
|
||||
|
||||
@Configuration
|
||||
public static class EntityManagerConfiguration {
|
||||
@Bean
|
||||
public EntityManager entityManager() {
|
||||
return mock(EntityManager.class);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user