Feature - implementation of Spring Session ReactiveSessionRepository added. #2163

pull/2195/head
Nikita Koksharov 6 years ago
parent e356c1e3d4
commit 054dba84a4

@ -152,14 +152,14 @@
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<artifactId>spring-test</artifactId>
<version>[3.1,)</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>[3.1,5.0)</version>
<artifactId>spring-webflux</artifactId>
<version>[3.1,)</version>
<scope>test</scope>
</dependency>
@ -270,6 +270,14 @@
<scope>provided</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>[3.1,)</version>
<scope>provided</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>

@ -0,0 +1,70 @@
/**
* Copyright (c) 2013-2019 Nikita Koksharov
*
* 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.redisson.spring.session;
import org.redisson.api.RedissonClient;
import org.redisson.spring.session.RedissonSessionRepository.RedissonSession;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.session.ReactiveSessionRepository;
import reactor.core.publisher.Mono;
/**
*
* @author Nikita Koksharov
*
*/
public class ReactiveRedissonSessionRepository implements ReactiveSessionRepository<RedissonSession> {
private final RedissonSessionRepository repository;
public ReactiveRedissonSessionRepository(RedissonClient redissonClient, ApplicationEventPublisher eventPublisher,
String keyPrefix) {
this.repository = new RedissonSessionRepository(redissonClient, eventPublisher, keyPrefix);
}
public void setDefaultMaxInactiveInterval(int defaultMaxInactiveInterval) {
repository.setDefaultMaxInactiveInterval(defaultMaxInactiveInterval);
}
@Override
public Mono<RedissonSession> createSession() {
return Mono.fromCallable(() -> {
return repository.createSession();
});
}
@Override
public Mono<Void> save(RedissonSession session) {
// session changes are stored in real-time
return Mono.empty();
}
@Override
public Mono<RedissonSession> findById(String id) {
return Mono.fromCallable(() -> {
return repository.findById(id);
});
}
@Override
public Mono<Void> deleteById(String id) {
return Mono.fromRunnable(() -> {
repository.deleteById(id);
});
}
}

@ -0,0 +1,62 @@
/**
* Copyright (c) 2013-2019 Nikita Koksharov
*
* 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.redisson.spring.session.config;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.session.MapSession;
import org.springframework.web.server.session.WebSessionManager;
/**
* Enables Redisson's Spring Session implementation backed by Redis and
* exposes {@link WebSessionManager} as a bean named "webSessionManager".
* <p>
* Redisson instance should be registered as bean in application context.
* Usage example:
* <pre>
* <code>
* {@literal @Configuration}
* {@literal EnableRedissonHttpSession}
* public class RedissonHttpSessionConfig {
*
* {@literal @Bean}
* public RedissonClient redisson() {
* return Redisson.create();
* }
*
* }
* </code>
* </pre>
*
* @author Nikita Koksharov
*
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Import(RedissonWebSessionConfiguration.class)
@Configuration
public @interface EnableRedissonWebSession {
int maxInactiveIntervalInSeconds() default MapSession.DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS;
String keyPrefix() default "";
}

@ -0,0 +1,71 @@
/**
* Copyright (c) 2013-2019 Nikita Koksharov
*
* 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.redisson.spring.session.config;
import java.util.Map;
import org.redisson.api.RedissonClient;
import org.redisson.spring.session.ReactiveRedissonSessionRepository;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.annotation.Bean;
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.session.config.annotation.web.server.SpringWebSessionConfiguration;
import org.springframework.web.server.session.WebSessionManager;
/**
* Exposes the {@link WebSessionManager} as the bean
* named "webSessionManager".
* <p>
* Redisson instance should be registered as bean
* in application context.
*
* @author Nikita Koksharov
*
*/
@Configuration
public class RedissonWebSessionConfiguration extends SpringWebSessionConfiguration implements ImportAware {
private Integer maxInactiveIntervalInSeconds;
private String keyPrefix;
@Bean
public ReactiveRedissonSessionRepository sessionRepository(
RedissonClient redissonClient, ApplicationEventPublisher eventPublisher) {
ReactiveRedissonSessionRepository repository = new ReactiveRedissonSessionRepository(redissonClient, eventPublisher, keyPrefix);
repository.setDefaultMaxInactiveInterval(maxInactiveIntervalInSeconds);
return repository;
}
public void setMaxInactiveIntervalInSeconds(Integer maxInactiveIntervalInSeconds) {
this.maxInactiveIntervalInSeconds = maxInactiveIntervalInSeconds;
}
public void setKeyPrefix(String keyPrefix) {
this.keyPrefix = keyPrefix;
}
@Override
public void setImportMetadata(AnnotationMetadata importMetadata) {
Map<String, Object> map = importMetadata.getAnnotationAttributes(EnableRedissonWebSession.class.getName());
AnnotationAttributes attrs = AnnotationAttributes.fromMap(map);
keyPrefix = attrs.getString("keyPrefix");
maxInactiveIntervalInSeconds = attrs.getNumber("maxInactiveIntervalInSeconds");
}
}

@ -1,21 +0,0 @@
package org.redisson.spring.session;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.spring.session.config.EnableRedissonHttpSession;
import org.springframework.context.annotation.Bean;
@EnableRedissonHttpSession
public class Config {
@Bean
public RedissonClient redisson() {
return Redisson.create();
}
@Bean
public SessionEventsListener listener() {
return new SessionEventsListener();
}
}

@ -0,0 +1,32 @@
package org.redisson.spring.session;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.spring.session.config.EnableRedissonHttpSession;
import org.redisson.spring.session.config.EnableRedissonWebSession;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.web.reactive.DispatcherHandler;
import org.springframework.web.server.WebHandler;
import org.springframework.web.server.adapter.WebHttpHandlerBuilder;
@EnableRedissonHttpSession
//@EnableRedissonWebSession
public class HttpConfig {
@Bean
public RedissonClient redisson() {
return Redisson.create();
}
@Bean
public SessionEventsListener listener() {
return new SessionEventsListener();
}
@Bean(WebHttpHandlerBuilder.WEB_HANDLER_BEAN_NAME)
public WebHandler dispatcherHandler(ApplicationContext context) {
return new DispatcherHandler(context);
}
}

@ -6,7 +6,7 @@ import org.redisson.spring.session.config.EnableRedissonHttpSession;
import org.springframework.context.annotation.Bean;
@EnableRedissonHttpSession(maxInactiveIntervalInSeconds = 5)
public class ConfigTimeout {
public class HttpConfigTimeout {
@Bean
public RedissonClient redisson() {

@ -2,11 +2,11 @@ package org.redisson.spring.session;
import org.springframework.session.web.context.AbstractHttpSessionApplicationInitializer;
public class Initializer extends AbstractHttpSessionApplicationInitializer {
public class HttpInitializer extends AbstractHttpSessionApplicationInitializer {
public static Class<?> CONFIG_CLASS = Config.class;
public static Class<?> CONFIG_CLASS = HttpConfig.class;
public Initializer() {
public HttpInitializer() {
super(CONFIG_CLASS);
}

@ -125,7 +125,7 @@ public class RedissonSessionManagerTest {
@Test
public void testExpire() throws Exception {
Initializer.CONFIG_CLASS = ConfigTimeout.class;
HttpInitializer.CONFIG_CLASS = HttpConfigTimeout.class;
// start the server at http://localhost:8080/myapp
TomcatServer server = new TomcatServer("myapp", 8080, "src/test/");
server.start();

@ -0,0 +1,30 @@
package org.redisson.spring.session;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.spring.session.config.EnableRedissonWebSession;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.web.reactive.DispatcherHandler;
import org.springframework.web.server.WebHandler;
import org.springframework.web.server.adapter.WebHttpHandlerBuilder;
@EnableRedissonWebSession
public class WebConfig {
@Bean
public RedissonClient redisson() {
return Redisson.create();
}
@Bean
public SessionEventsListener listener() {
return new SessionEventsListener();
}
@Bean(WebHttpHandlerBuilder.WEB_HANDLER_BEAN_NAME)
public WebHandler dispatcherHandler(ApplicationContext context) {
return new DispatcherHandler(context);
}
}

@ -0,0 +1,14 @@
package org.redisson.spring.session;
import org.springframework.web.server.adapter.AbstractReactiveWebInitializer;
public class WebInitializer extends AbstractReactiveWebInitializer {
public static Class<?> CONFIG_CLASS = HttpConfig.class;
@Override
protected Class<?>[] getConfigClasses() {
return new Class[] {CONFIG_CLASS};
}
}
Loading…
Cancel
Save