Imperatively create TTL indexes

This commit is contained in:
Greg Turnquist
2017-09-19 11:59:02 -05:00
parent f4e3ed8c3b
commit 2afebe8e78
6 changed files with 136 additions and 60 deletions

View File

@@ -62,9 +62,7 @@ public class MongoOperationsSessionRepository
private final MongoOperations mongoOperations;
private AbstractMongoSessionConverter mongoSessionConverter =
SessionConverterProvider.getDefaultMongoConverter();
private AbstractMongoSessionConverter mongoSessionConverter = new JdkMongoSessionConverter();
private Integer maxInactiveIntervalInSeconds = DEFAULT_INACTIVE_INTERVAL;
private String collectionName = DEFAULT_COLLECTION_NAME;

View File

@@ -17,9 +17,13 @@ package org.springframework.session.data.mongo;
import static org.springframework.session.data.mongo.MongoSessionUtils.*;
import javax.annotation.PostConstruct;
import org.bson.Document;
import reactor.core.publisher.Mono;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.ReactiveMongoOperations;
import org.springframework.data.mongodb.core.index.IndexOperations;
import org.springframework.session.ReactorSessionRepository;
/**
@@ -39,10 +43,12 @@ public class ReactiveMongoOperationsSessionRepository implements ReactorSessionR
private final ReactiveMongoOperations mongoOperations;
private AbstractMongoSessionConverter mongoSessionConverter = SessionConverterProvider.getDefaultMongoConverter();
private AbstractMongoSessionConverter mongoSessionConverter = new JdkMongoSessionConverter();
private Integer maxInactiveIntervalInSeconds = DEFAULT_INACTIVE_INTERVAL;
private String collectionName = DEFAULT_COLLECTION_NAME;
private MongoOperations blockingMongoOperations;
public ReactiveMongoOperationsSessionRepository(ReactiveMongoOperations mongoOperations) {
this.mongoOperations = mongoOperations;
}
@@ -114,6 +120,19 @@ public class ReactiveMongoOperationsSessionRepository implements ReactorSessionR
return this.mongoOperations.remove(findSession(id), this.collectionName).then();
}
/**
* Do not use {@link org.springframework.data.mongodb.core.index.ReactiveIndexOperations} to ensure indexes exist.
* Instead, get a blocking {@link IndexOperations} and use that instead, if possible.
*/
@PostConstruct
public void ensureIndexesAreCreated() {
if (this.blockingMongoOperations != null) {
IndexOperations indexOperations = this.blockingMongoOperations.indexOps(this.collectionName);
this.mongoSessionConverter.ensureIndexes(indexOperations);
}
}
private Mono<Document> findSession(String id) {
return this.mongoOperations.findById(id, Document.class, this.collectionName);
}
@@ -130,4 +149,11 @@ public class ReactiveMongoOperationsSessionRepository implements ReactorSessionR
this.collectionName = collectionName;
}
public MongoOperations getBlockingMongoOperations() {
return this.blockingMongoOperations;
}
public void setBlockingMongoOperations(MongoOperations blockingMongoOperations) {
this.blockingMongoOperations = blockingMongoOperations;
}
}

View File

@@ -1,34 +0,0 @@
/*
* Copyright 2014-2016 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.session.data.mongo;
/**
* Provider choosing proper AbstractMongoSessionConverter.
*
* @author Jakub Kubrynski
*/
final class SessionConverterProvider {
private static final String JACKSON_CLASS_NAME = "com.fasterxml.jackson.databind.ObjectMapper";
private SessionConverterProvider() {
}
static AbstractMongoSessionConverter getDefaultMongoConverter() {
return new JdkMongoSessionConverter();
}
}

View File

@@ -22,6 +22,7 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportAware;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.ReactiveMongoOperations;
import org.springframework.session.data.mongo.AbstractMongoSessionConverter;
import org.springframework.session.data.mongo.ReactiveMongoOperationsSessionRepository;
@@ -41,6 +42,9 @@ public class ReactiveMongoWebSessionConfiguration implements EmbeddedValueResolv
private String collectionName;
private StringValueResolver embeddedValueResolver;
@Autowired(required = false)
private MongoOperations mongoOperations;
@Bean
public ReactiveMongoOperationsSessionRepository reactiveMongoOperationsSessionRepository(ReactiveMongoOperations operations) {
@@ -57,6 +61,10 @@ public class ReactiveMongoWebSessionConfiguration implements EmbeddedValueResolv
if (this.collectionName != null) {
repository.setCollectionName(this.collectionName);
}
if (this.mongoOperations != null) {
repository.setBlockingMongoOperations(this.mongoOperations);
}
return repository;
}