Merge pull request #8568 from eddumelendez:gh-8052

* pr/8568:
  Polish "Add reactive support for Spring Data Cassandra"
  Add reactive support for Spring Data Cassandra
pull/8819/merge
Stephane Nicoll 8 years ago
commit 3477b02cb4

@ -0,0 +1,71 @@
/*
* 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.data.cassandra;
import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.Session;
import reactor.core.publisher.Flux;
import reactor.core.scheduler.Schedulers;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.cassandra.core.session.DefaultBridgedReactiveSession;
import org.springframework.cassandra.core.session.DefaultReactiveSessionFactory;
import org.springframework.cassandra.core.session.ReactiveSession;
import org.springframework.cassandra.core.session.ReactiveSessionFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.cassandra.convert.CassandraConverter;
import org.springframework.data.cassandra.core.ReactiveCassandraTemplate;
/**
* {@link EnableAutoConfiguration Auto-configuration} for Spring Data's reactive Cassandra
* support.
*
* @author Eddú Meléndez
* @since 2.0.0
*/
@Configuration
@ConditionalOnClass({ Cluster.class, ReactiveCassandraTemplate.class, Flux.class })
@ConditionalOnBean(Session.class)
@AutoConfigureAfter(CassandraDataAutoConfiguration.class)
public class ReactiveCassandraDataAutoConfiguration {
@Bean
@ConditionalOnMissingBean(ReactiveSession.class)
public ReactiveSession reactiveCassandraSession(Session session) {
return new DefaultBridgedReactiveSession(session, Schedulers.elastic());
}
@Bean
public ReactiveSessionFactory reactiveCassandraSessionFactory(
ReactiveSession reactiveCassandraSession) {
return new DefaultReactiveSessionFactory(reactiveCassandraSession);
}
@Bean
@ConditionalOnMissingBean
public ReactiveCassandraTemplate reactiveCassandraTemplate(
ReactiveSession reactiveCassandraSession,
CassandraConverter converter) {
return new ReactiveCassandraTemplate(reactiveCassandraSession, converter);
}
}

@ -0,0 +1,47 @@
/*
* 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.data.cassandra;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cassandra.core.session.ReactiveSession;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.data.cassandra.repository.ReactiveCassandraRepository;
import org.springframework.data.cassandra.repository.config.EnableReactiveCassandraRepositories;
import org.springframework.data.cassandra.repository.support.ReactiveCassandraRepositoryFactoryBean;
/**
* {@link EnableAutoConfiguration Auto-configuration} for Spring Data's Cassandra
* Reactive Repositories.
*
* @author Eddú Meléndez
* @since 2.0.0
* @see EnableReactiveCassandraRepositories
*/
@Configuration
@ConditionalOnClass({ ReactiveSession.class, ReactiveCassandraRepository.class })
@ConditionalOnProperty(prefix = "spring.data.cassandra.reactive-repositories", name = "enabled", havingValue = "true", matchIfMissing = true)
@ConditionalOnMissingBean(ReactiveCassandraRepositoryFactoryBean.class)
@Import(ReactiveCassandraRepositoriesAutoConfigureRegistrar.class)
@AutoConfigureAfter(ReactiveCassandraDataAutoConfiguration.class)
public class ReactiveCassandraRepositoriesAutoConfiguration {
}

@ -0,0 +1,57 @@
/*
* 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.data.cassandra;
import java.lang.annotation.Annotation;
import org.springframework.boot.autoconfigure.data.AbstractRepositoryConfigurationSourceSupport;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.data.cassandra.repository.config.EnableReactiveCassandraRepositories;
import org.springframework.data.cassandra.repository.config.ReactiveCassandraRepositoryConfigurationExtension;
import org.springframework.data.repository.config.RepositoryConfigurationExtension;
/**
* {@link ImportBeanDefinitionRegistrar} used to auto-configure Spring Data Cassandra
* Reactive Repositories.
*
* @author Eddú Meléndez
* @since 2.0.0
*/
class ReactiveCassandraRepositoriesAutoConfigureRegistrar
extends AbstractRepositoryConfigurationSourceSupport {
@Override
protected Class<? extends Annotation> getAnnotation() {
return EnableReactiveCassandraRepositories.class;
}
@Override
protected Class<?> getConfiguration() {
return EnableReactiveCassandraRepositoriesConfiguration.class;
}
@Override
protected RepositoryConfigurationExtension getRepositoryConfigurationExtension() {
return new ReactiveCassandraRepositoryConfigurationExtension();
}
@EnableReactiveCassandraRepositories
private static class EnableReactiveCassandraRepositoriesConfiguration {
}
}

@ -84,6 +84,12 @@
"name": "spring.data.cassandra.compression",
"defaultValue": "none"
},
{
"name": "spring.data.cassandra.reactive-repositories.enabled",
"type": "java.lang.Boolean",
"description": "Enable Cassandra reactive repositories.",
"defaultValue": true
},
{
"name": "spring.data.couchbase.consistency",
"defaultValue": "read-your-own-writes"

@ -31,6 +31,8 @@ org.springframework.boot.autoconfigure.couchbase.CouchbaseAutoConfiguration,\
org.springframework.boot.autoconfigure.dao.PersistenceExceptionTranslationAutoConfiguration,\
org.springframework.boot.autoconfigure.data.cassandra.CassandraDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.cassandra.CassandraRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.cassandra.ReactiveCassandraDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.cassandra.ReactiveCassandraRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.couchbase.CouchbaseDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.couchbase.CouchbaseRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchAutoConfiguration,\

@ -0,0 +1,24 @@
/*
* 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.data.alt.cassandra;
import org.springframework.boot.autoconfigure.data.cassandra.city.City;
import org.springframework.data.repository.reactive.ReactiveCrudRepository;
public interface ReactiveCityCassandraRepository extends ReactiveCrudRepository<City, Long> {
}

@ -0,0 +1,117 @@
/*
* 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.data.cassandra;
import java.util.Set;
import com.datastax.driver.core.Session;
import org.junit.After;
import org.junit.Test;
import org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration;
import org.springframework.boot.autoconfigure.data.cassandra.city.City;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.test.util.EnvironmentTestUtils;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.cassandra.core.ReactiveCassandraTemplate;
import org.springframework.data.cassandra.mapping.CassandraMappingContext;
import org.springframework.data.cassandra.mapping.SimpleUserTypeResolver;
import org.springframework.test.util.ReflectionTestUtils;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
/**
* Tests for {@link ReactiveCassandraDataAutoConfiguration}.
*
* @author Eddú Meléndez
* @author Stephane Nicoll
*/
public class ReactiveCassandraDataAutoConfigurationTests {
private AnnotationConfigApplicationContext context;
@After
public void close() {
if (this.context != null) {
this.context.close();
}
}
@Test
public void templateExists() {
load("spring.data.cassandra.keyspaceName:boot_test");
assertThat(this.context.getBeanNamesForType(ReactiveCassandraTemplate.class))
.hasSize(1);
}
@Test
@SuppressWarnings("unchecked")
public void entityScanShouldSetInitialEntitySet() throws Exception {
load(EntityScanConfig.class, "spring.data.cassandra.keyspaceName:boot_test");
CassandraMappingContext mappingContext = this.context
.getBean(CassandraMappingContext.class);
Set<Class<?>> initialEntitySet = (Set<Class<?>>) ReflectionTestUtils
.getField(mappingContext, "initialEntitySet");
assertThat(initialEntitySet).containsOnly(City.class);
}
@Test
public void userTypeResolverShouldBeSet() throws Exception {
load("spring.data.cassandra.keyspaceName:boot_test");
CassandraMappingContext mappingContext = this.context
.getBean(CassandraMappingContext.class);
assertThat(ReflectionTestUtils.getField(mappingContext, "userTypeResolver"))
.isInstanceOf(SimpleUserTypeResolver.class);
}
private void load(String... environment) {
load(null, environment);
}
private void load(Class<?> config, String... environment) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(ctx, environment);
if (config != null) {
ctx.register(config);
}
ctx.register(TestConfiguration.class, CassandraAutoConfiguration.class,
CassandraDataAutoConfiguration.class,
ReactiveCassandraDataAutoConfiguration.class);
ctx.refresh();
this.context = ctx;
}
@Configuration
static class TestConfiguration {
@Bean
public Session session() {
return mock(Session.class);
}
}
@Configuration
@EntityScan("org.springframework.boot.autoconfigure.data.cassandra.city")
static class EntityScanConfig {
}
}

@ -0,0 +1,138 @@
/*
* 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.data.cassandra;
import java.util.Set;
import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.Session;
import org.junit.After;
import org.junit.Test;
import org.springframework.boot.autoconfigure.TestAutoConfigurationPackage;
import org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration;
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.autoconfigure.data.alt.cassandra.ReactiveCityCassandraRepository;
import org.springframework.boot.autoconfigure.data.cassandra.city.City;
import org.springframework.boot.autoconfigure.data.cassandra.city.ReactiveCityRepository;
import org.springframework.boot.autoconfigure.data.empty.EmptyDataPackage;
import org.springframework.cassandra.core.session.ReactiveSession;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.data.cassandra.mapping.BasicCassandraMappingContext;
import org.springframework.data.cassandra.repository.config.EnableReactiveCassandraRepositories;
import org.springframework.test.util.ReflectionTestUtils;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
/**
* Tests for {@link ReactiveCassandraRepositoriesAutoConfiguration}.
*
* @author Eddú Meléndez
* @author Stephane Nicoll
*/
public class ReactiveCassandraRepositoriesAutoConfigurationTests {
private AnnotationConfigApplicationContext context;
@After
public void close() {
if (this.context != null) {
this.context.close();
}
}
@Test
public void testDefaultRepositoryConfiguration() {
load(TestConfiguration.class);
assertThat(this.context.getBean(ReactiveCityRepository.class)).isNotNull();
assertThat(this.context.getBean(Cluster.class)).isNotNull();
assertThat(getInitialEntitySet()).hasSize(1);
}
@Test
public void testNoRepositoryConfiguration() {
load(TestExcludeConfiguration.class, EmptyConfiguration.class);
assertThat(this.context.getBean(Cluster.class)).isNotNull();
assertThat(getInitialEntitySet()).hasSize(1).containsOnly(City.class);
}
@Test
public void doesNotTriggerDefaultRepositoryDetectionIfCustomized() {
load(TestExcludeConfiguration.class, CustomizedConfiguration.class);
assertThat(this.context.getBean(ReactiveCityCassandraRepository.class)).isNotNull();
assertThat(getInitialEntitySet()).hasSize(1).containsOnly(City.class);
}
@SuppressWarnings("unchecked")
private Set<Class<?>> getInitialEntitySet() {
BasicCassandraMappingContext mappingContext = this.context
.getBean(BasicCassandraMappingContext.class);
return (Set<Class<?>>) ReflectionTestUtils.getField(mappingContext,
"initialEntitySet");
}
private void load(Class<?>... configurations) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(configurations);
ctx.register(CassandraAutoConfiguration.class,
CassandraRepositoriesAutoConfiguration.class,
CassandraDataAutoConfiguration.class,
ReactiveCassandraDataAutoConfiguration.class,
ReactiveCassandraRepositoriesAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
ctx.refresh();
this.context = ctx;
}
@Configuration
@TestAutoConfigurationPackage(City.class)
static class TestConfiguration {
@Bean
public Session Session() {
return mock(Session.class);
}
}
@Configuration
@TestAutoConfigurationPackage(EmptyDataPackage.class)
static class EmptyConfiguration {
}
@Configuration
@TestAutoConfigurationPackage(ReactiveCassandraRepositoriesAutoConfigurationTests.class)
@EnableReactiveCassandraRepositories(basePackageClasses = ReactiveCityCassandraRepository.class)
static class CustomizedConfiguration {
}
@Configuration
@ComponentScan(excludeFilters = @Filter(classes = {
ReactiveSession.class }, type = FilterType.ASSIGNABLE_TYPE))
static class TestExcludeConfiguration {
}
}

@ -0,0 +1,23 @@
/*
* 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.data.cassandra.city;
import org.springframework.data.repository.reactive.ReactiveCrudRepository;
public interface ReactiveCityRepository extends ReactiveCrudRepository<City, Long> {
}

@ -352,6 +352,11 @@
<artifactId>spring-boot-starter-data-cassandra</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-cassandra-reactive</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-couchbase</artifactId>

@ -573,6 +573,7 @@ content into your application; rather pick only the properties that you need.
spring.data.cassandra.load-balancing-policy= # Class name of the load balancing policy.
spring.data.cassandra.port= # Port of the Cassandra server.
spring.data.cassandra.password= # Login password of the server.
spring.data.cassandra.reactive-repositories.enabled=true # Enable Cassandra reactive repositories.
spring.data.cassandra.read-timeout-millis= # Socket option: read time out.
spring.data.cassandra.reconnection-policy= # Reconnection policy class.
spring.data.cassandra.repositories.enabled= # Enable Cassandra repositories.

@ -29,6 +29,7 @@
<module>spring-boot-starter-cache</module>
<module>spring-boot-starter-cloud-connectors</module>
<module>spring-boot-starter-data-cassandra</module>
<module>spring-boot-starter-data-cassandra-reactive</module>
<module>spring-boot-starter-data-couchbase</module>
<module>spring-boot-starter-data-elasticsearch</module>
<module>spring-boot-starter-data-jpa</module>

@ -0,0 +1,52 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starters</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</version>
</parent>
<artifactId>spring-boot-starter-data-cassandra-reactive</artifactId>
<name>Spring Boot Data Cassandra Reactive Starter</name>
<description>Starter for using Cassandra distributed database and Spring Data
Cassandra Reactive
</description>
<url>http://projects.spring.io/spring-boot/</url>
<organization>
<name>Pivotal Software, Inc.</name>
<url>http://www.spring.io</url>
</organization>
<properties>
<main.basedir>${basedir}/../..</main.basedir>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-cassandra</artifactId>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-cql</artifactId>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-core</artifactId>
</dependency>
</dependencies>
</project>
Loading…
Cancel
Save