Merge pull request #8881 from vpavic:integration-jdbc

* pr/8881:
  Polish "Add database initializer for Spring Integration"
  Add database initializer for Spring Integration
pull/5311/head
Stephane Nicoll 8 years ago
commit 55233eba9c

@ -329,6 +329,11 @@
<artifactId>spring-integration-core</artifactId> <artifactId>spring-integration-core</artifactId>
<optional>true</optional> <optional>true</optional>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-jdbc</artifactId>
<optional>true</optional>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.integration</groupId> <groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-jmx</artifactId> <artifactId>spring-integration-jmx</artifactId>

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2016 the original author or authors. * Copyright 2012-2017 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -17,6 +17,7 @@
package org.springframework.boot.autoconfigure.integration; package org.springframework.boot.autoconfigure.integration;
import javax.management.MBeanServer; import javax.management.MBeanServer;
import javax.sql.DataSource;
import org.springframework.beans.BeansException; import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.BeanFactory;
@ -25,17 +26,21 @@ import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
import org.springframework.boot.autoconfigure.condition.SearchStrategy; import org.springframework.boot.autoconfigure.condition.SearchStrategy;
import org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration; import org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration;
import org.springframework.boot.bind.RelaxedPropertyResolver; import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.EnvironmentAware; import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
import org.springframework.core.io.ResourceLoader;
import org.springframework.integration.config.EnableIntegration; import org.springframework.integration.config.EnableIntegration;
import org.springframework.integration.config.EnableIntegrationManagement; import org.springframework.integration.config.EnableIntegrationManagement;
import org.springframework.integration.gateway.GatewayProxyFactoryBean; import org.springframework.integration.gateway.GatewayProxyFactoryBean;
import org.springframework.integration.jdbc.store.JdbcMessageStore;
import org.springframework.integration.jmx.config.EnableIntegrationMBeanExport; import org.springframework.integration.jmx.config.EnableIntegrationMBeanExport;
import org.springframework.integration.monitor.IntegrationMBeanExporter; import org.springframework.integration.monitor.IntegrationMBeanExporter;
import org.springframework.integration.support.management.IntegrationManagementConfigurer; import org.springframework.integration.support.management.IntegrationManagementConfigurer;
@ -48,10 +53,12 @@ import org.springframework.util.StringUtils;
* @author Artem Bilan * @author Artem Bilan
* @author Dave Syer * @author Dave Syer
* @author Stephane Nicoll * @author Stephane Nicoll
* @author Vedran Pavic
* @since 1.1.0 * @since 1.1.0
*/ */
@Configuration @Configuration
@ConditionalOnClass(EnableIntegration.class) @ConditionalOnClass(EnableIntegration.class)
@EnableConfigurationProperties(IntegrationProperties.class)
@AutoConfigureAfter(JmxAutoConfiguration.class) @AutoConfigureAfter(JmxAutoConfiguration.class)
public class IntegrationAutoConfiguration { public class IntegrationAutoConfiguration {
@ -131,4 +138,24 @@ public class IntegrationAutoConfiguration {
} }
/**
* Integration JDBC configuration.
*/
@Configuration
@ConditionalOnClass(JdbcMessageStore.class)
@ConditionalOnSingleCandidate(DataSource.class)
protected static class IntegrationJdbcConfiguration {
@Bean
@ConditionalOnMissingBean
@ConditionalOnProperty(prefix = "spring.integration.jdbc.initializer", name = "enabled")
public IntegrationDatabaseInitializer integrationDatabaseInitializer(
DataSource dataSource, ResourceLoader resourceLoader,
IntegrationProperties properties) {
return new IntegrationDatabaseInitializer(dataSource, resourceLoader,
properties);
}
}
} }

@ -0,0 +1,52 @@
/*
* Copyright 2012-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.boot.autoconfigure.integration;
import javax.sql.DataSource;
import org.springframework.boot.autoconfigure.AbstractDatabaseInitializer;
import org.springframework.core.io.ResourceLoader;
import org.springframework.util.Assert;
/**
* Initializer for Spring Integration schema.
*
* @author Vedran Pavic
* @since 2.0.0
*/
public class IntegrationDatabaseInitializer extends AbstractDatabaseInitializer {
private final IntegrationProperties.Jdbc properties;
public IntegrationDatabaseInitializer(DataSource dataSource,
ResourceLoader resourceLoader, IntegrationProperties properties) {
super(dataSource, resourceLoader);
Assert.notNull(properties, "IntegrationProperties must not be null");
this.properties = properties.getJdbc();
}
@Override
protected boolean isEnabled() {
return this.properties.getInitializer().isEnabled();
}
@Override
protected String getSchemaLocation() {
return this.properties.getSchema();
}
}

@ -0,0 +1,80 @@
/*
* Copyright 2012-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.boot.autoconfigure.integration;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* Configuration properties for Spring Integration.
*
* @author Vedran Pavic
* @author Stephane Nicoll
* @since 2.0.0
*/
@ConfigurationProperties(prefix = "spring.integration")
public class IntegrationProperties {
private final Jdbc jdbc = new Jdbc();
public Jdbc getJdbc() {
return this.jdbc;
}
public static class Jdbc {
private static final String DEFAULT_SCHEMA_LOCATION = "classpath:org/springframework/"
+ "integration/jdbc/schema-@@platform@@.sql";
/**
* Path to the SQL file to use to initialize the database schema.
*/
private String schema = DEFAULT_SCHEMA_LOCATION;
private final Initializer initializer = new Initializer();
public String getSchema() {
return this.schema;
}
public void setSchema(String schema) {
this.schema = schema;
}
public Initializer getInitializer() {
return this.initializer;
}
public class Initializer {
/**
* Create the required integration tables on startup.
*/
private boolean enabled = false;
public boolean isEnabled() {
return this.enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
}
}
}

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2016 the original author or authors. * Copyright 2012-2017 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -22,10 +22,16 @@ import java.util.List;
import javax.management.MBeanServer; import javax.management.MBeanServer;
import org.junit.After; import org.junit.After;
import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.boot.autoconfigure.integration.IntegrationAutoConfiguration.IntegrationComponentScanAutoConfiguration; import org.springframework.boot.autoconfigure.integration.IntegrationAutoConfiguration.IntegrationComponentScanAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration;
import org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration;
import org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration; import org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration;
import org.springframework.boot.test.util.EnvironmentTestUtils;
import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
@ -36,6 +42,8 @@ import org.springframework.integration.annotation.MessagingGateway;
import org.springframework.integration.gateway.RequestReplyExchanger; import org.springframework.integration.gateway.RequestReplyExchanger;
import org.springframework.integration.support.channel.HeaderChannelRegistry; import org.springframework.integration.support.channel.HeaderChannelRegistry;
import org.springframework.integration.support.management.IntegrationManagementConfigurer; import org.springframework.integration.support.management.IntegrationManagementConfigurer;
import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jmx.export.MBeanExporter; import org.springframework.jmx.export.MBeanExporter;
import org.springframework.test.context.support.TestPropertySourceUtils; import org.springframework.test.context.support.TestPropertySourceUtils;
@ -47,9 +55,13 @@ import static org.mockito.Mockito.mock;
* *
* @author Artem Bilan * @author Artem Bilan
* @author Stephane Nicoll * @author Stephane Nicoll
* @author Vedran Pavic
*/ */
public class IntegrationAutoConfigurationTests { public class IntegrationAutoConfigurationTests {
@Rule
public ExpectedException thrown = ExpectedException.none();
private AnnotationConfigApplicationContext context; private AnnotationConfigApplicationContext context;
@After @After
@ -126,12 +138,62 @@ public class IntegrationAutoConfigurationTests {
@Test @Test
public void primaryExporterIsAllowed() { public void primaryExporterIsAllowed() {
load(CustomMBeanExporter.class); load(new Class[] { CustomMBeanExporter.class });
assertThat(this.context.getBeansOfType(MBeanExporter.class)).hasSize(2); assertThat(this.context.getBeansOfType(MBeanExporter.class)).hasSize(2);
assertThat(this.context.getBean(MBeanExporter.class)) assertThat(this.context.getBean(MBeanExporter.class))
.isSameAs(this.context.getBean("myMBeanExporter")); .isSameAs(this.context.getBean("myMBeanExporter"));
} }
@Test
public void integrationJdbcDatabaseInitializerEnabled() {
load(new Class[] { EmbeddedDataSourceConfiguration.class,
DataSourceTransactionManagerAutoConfiguration.class,
JdbcTemplateAutoConfiguration.class,
IntegrationAutoConfiguration.class},
"spring.datasource.generate-unique-name=true",
"spring.integration.jdbc.initializer.enabled=true");
assertThat(this.context.getBean(IntegrationProperties.class).getJdbc()
.getInitializer().isEnabled()).isTrue();
JdbcOperations jdbcOperations = this.context.getBean(JdbcOperations.class);
assertThat(jdbcOperations.queryForList("select * from INT_MESSAGE")).isEmpty();
assertThat(jdbcOperations.queryForList("select * from INT_GROUP_TO_MESSAGE"))
.isEmpty();
assertThat(jdbcOperations.queryForList("select * from INT_MESSAGE_GROUP"))
.isEmpty();
assertThat(jdbcOperations.queryForList("select * from INT_LOCK")).isEmpty();
assertThat(jdbcOperations.queryForList("select * from INT_CHANNEL_MESSAGE"))
.isEmpty();
}
@Test
public void integrationJdbcDatabaseInitializerDisabled() {
load(new Class[] { EmbeddedDataSourceConfiguration.class,
DataSourceTransactionManagerAutoConfiguration.class,
JdbcTemplateAutoConfiguration.class,
IntegrationAutoConfiguration.class },
"spring.datasource.generate-unique-name=true",
"spring.integration.jdbc.initializer.enabled=false");
assertThat(this.context.getBean(IntegrationProperties.class).getJdbc()
.getInitializer().isEnabled()).isFalse();
JdbcOperations jdbcOperations = this.context.getBean(JdbcOperations.class);
this.thrown.expect(BadSqlGrammarException.class);
jdbcOperations.queryForList("select * from INT_MESSAGE");
}
@Test
public void integrationJdbcDatabaseInitializerDisabledByDefault() {
load(new Class[] { EmbeddedDataSourceConfiguration.class,
DataSourceTransactionManagerAutoConfiguration.class,
JdbcTemplateAutoConfiguration.class,
IntegrationAutoConfiguration.class },
"spring.datasource.generate-unique-name=true");
assertThat(this.context.getBean(IntegrationProperties.class).getJdbc()
.getInitializer().isEnabled()).isFalse();
JdbcOperations jdbcOperations = this.context.getBean(JdbcOperations.class);
this.thrown.expect(BadSqlGrammarException.class);
jdbcOperations.queryForList("select * from INT_MESSAGE");
}
private static void assertDomains(MBeanServer mBeanServer, boolean expected, private static void assertDomains(MBeanServer mBeanServer, boolean expected,
String... domains) { String... domains) {
List<String> actual = Arrays.asList(mBeanServer.getDomains()); List<String> actual = Arrays.asList(mBeanServer.getDomains());
@ -144,12 +206,12 @@ public class IntegrationAutoConfigurationTests {
load(null, environment); load(null, environment);
} }
private void load(Class<?> config, String... environment) { private void load(Class<?>[] configs, String... environment) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
if (config != null) { EnvironmentTestUtils.addEnvironment(ctx, environment);
ctx.register(config); if (configs != null) {
ctx.register(configs);
} }
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(ctx, environment);
ctx.register(JmxAutoConfiguration.class, IntegrationAutoConfiguration.class); ctx.register(JmxAutoConfiguration.class, IntegrationAutoConfiguration.class);
ctx.refresh(); ctx.refresh();
this.context = ctx; this.context = ctx;

@ -890,6 +890,10 @@ content into your application; rather pick only the properties that you need.
spring.batch.schema=classpath:org/springframework/batch/core/schema-@@platform@@.sql # Path to the SQL file to use to initialize the database schema. spring.batch.schema=classpath:org/springframework/batch/core/schema-@@platform@@.sql # Path to the SQL file to use to initialize the database schema.
spring.batch.table-prefix= # Table prefix for all the batch meta-data tables. spring.batch.table-prefix= # Table prefix for all the batch meta-data tables.
# SPRING INTEGRATION ({sc-spring-boot-autoconfigure}/integration/IntegrationProperties.{sc-ext}[IntegrationProperties])
spring.integration.jdbc.initializer.enabled=false # Create the required integration tables on startup.
spring.integration.jdbc.schema=classpath:org/springframework/integration/jdbc/schema-@@platform@@.sql # Path to the SQL file to use to initialize the database schema.
# JMS ({sc-spring-boot-autoconfigure}/jms/JmsProperties.{sc-ext}[JmsProperties]) # JMS ({sc-spring-boot-autoconfigure}/jms/JmsProperties.{sc-ext}[JmsProperties])
spring.jms.jndi-name= # Connection factory JNDI name. When set, takes precedence to others connection factory auto-configurations. spring.jms.jndi-name= # Connection factory JNDI name. When set, takes precedence to others connection factory auto-configurations.
spring.jms.listener.acknowledge-mode= # Acknowledge mode of the container. By default, the listener is transacted with automatic acknowledgment. spring.jms.listener.acknowledge-mode= # Acknowledge mode of the container. By default, the listener is transacted with automatic acknowledgment.

@ -5113,10 +5113,23 @@ Spring Boot offers several conveniences for working with Spring Integration, inc
the `spring-boot-starter-integration` '`Starter`'. Spring Integration provides the `spring-boot-starter-integration` '`Starter`'. Spring Integration provides
abstractions over messaging and also other transports such as HTTP, TCP etc. If Spring abstractions over messaging and also other transports such as HTTP, TCP etc. If Spring
Integration is available on your classpath it will be initialized through the Integration is available on your classpath it will be initialized through the
`@EnableIntegration` annotation. Message processing statistics will be published over JMX `@EnableIntegration` annotation.
if `'spring-integration-jmx'` is also on the classpath. See the
Spring Boot will also configure some features that are triggered by the presence of
additional Spring Integration modules. Message processing statistics will be published
over JMX if `'spring-integration-jmx'` is also on the classpath. If
`'spring-integration-jdbc'` is available, the default database schema can be created
on startup:
[source,properties,indent=0]
----
spring.integration.jdbc.initializer.enabled=true
----
See the
{sc-spring-boot-autoconfigure}/integration/IntegrationAutoConfiguration.{sc-ext}[`IntegrationAutoConfiguration`] {sc-spring-boot-autoconfigure}/integration/IntegrationAutoConfiguration.{sc-ext}[`IntegrationAutoConfiguration`]
class for more details. and {sc-spring-boot-autoconfigure}/integration/IntegrationProperties.{sc-ext}[`IntegrationProperties`]
classes for more details.

Loading…
Cancel
Save