org.springframework.data.redis.connection.ReactiveRedisConnectionFactory Java Examples

The following examples show how to use org.springframework.data.redis.connection.ReactiveRedisConnectionFactory. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JetLinksRedisConfiguration.java    From jetlinks-community with Apache License 2.0 6 votes vote down vote up
@Bean
public ReactiveRedisTemplate<Object, Object> reactiveRedisTemplate(
    ReactiveRedisConnectionFactory reactiveRedisConnectionFactory, ResourceLoader resourceLoader) {

    FstSerializationRedisSerializer serializer = new FstSerializationRedisSerializer(() -> {
        FSTConfiguration configuration = FSTConfiguration.createDefaultConfiguration()
            .setForceSerializable(true);
        configuration.setClassLoader(resourceLoader.getClassLoader());
        return configuration;
    });
    @SuppressWarnings("all")
    RedisSerializationContext<Object, Object> serializationContext = RedisSerializationContext
        .newSerializationContext()
        .key((RedisSerializer)new StringRedisSerializer())
        .value(serializer)
        .hashKey(StringRedisSerializer.UTF_8)
        .hashValue(serializer)
        .build();

    return new ReactiveRedisTemplate<>(reactiveRedisConnectionFactory, serializationContext);
}
 
Example #2
Source File: TracingRedisConnectionFactory.java    From java-redis-client with Apache License 2.0 6 votes vote down vote up
@Override
public ReactiveRedisConnection getReactiveConnection() {
  if (this.delegate instanceof ReactiveRedisConnectionFactory) {
    ReactiveRedisConnectionFactory connectionFactory = (ReactiveRedisConnectionFactory) this.delegate;
    ReactiveRedisConnection connection = connectionFactory.getReactiveConnection();
    // support cluster connection
    if (connection instanceof ReactiveRedisClusterConnection) {
      return new TracingReactiveRedisClusterConnection(
          (ReactiveRedisClusterConnection) connection, tracingConfiguration);
    }
    return new TracingReactiveRedisConnection(connectionFactory.getReactiveConnection(),
        tracingConfiguration);
  }
  // TODO: shouldn't we throw an exception?
  return null;
}
 
Example #3
Source File: RedisConfig.java    From spring-reactive-sample with GNU General Public License v3.0 6 votes vote down vote up
@Bean
public ReactiveRedisMessageListenerContainer redisMessageListenerContainer(PostRepository posts, ReactiveRedisConnectionFactory connectionFactory) {
    ReactiveRedisMessageListenerContainer container = new ReactiveRedisMessageListenerContainer(connectionFactory);
    ObjectMapper objectMapper = new ObjectMapper();
    container.receive(ChannelTopic.of("posts"))
        .map(p->p.getMessage())
        .map(m -> {
            try {
                Post post= objectMapper.readValue(m, Post.class);
                post.setId(UUID.randomUUID().toString());
                return post;
            } catch (IOException e) {
                return null;
            }
        })
        .switchIfEmpty(Mono.error(new IllegalArgumentException()))
        .flatMap(p-> posts.save(p))
        .subscribe(c-> log.info(" count:" + c), null , () -> log.info("saving post."));
    return container;
}
 
Example #4
Source File: RedisConfig.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
@Bean
public ReactiveRedisTemplate<String, String> stringReactiveRedisTemplate(ReactiveRedisConnectionFactory connectionFactory){
    RedisSerializationContext<String, String> serializationContext = RedisSerializationContext
        .<String, String>newSerializationContext(new StringRedisSerializer())
        .hashKey(new StringRedisSerializer())
        .hashValue(new StringRedisSerializer())
        .build();
    return new ReactiveRedisTemplate<>(connectionFactory, serializationContext);
}
 
Example #5
Source File: RedisTestConfiguration.java    From spring-data-examples with Apache License 2.0 5 votes vote down vote up
/**
 * Configures a {@link ReactiveRedisTemplate} with {@link String} keys and a typed
 * {@link Jackson2JsonRedisSerializer}.
 */
@Bean
public ReactiveRedisTemplate<String, Person> reactiveJsonPersonRedisTemplate(
		ReactiveRedisConnectionFactory connectionFactory) {

	Jackson2JsonRedisSerializer<Person> serializer = new Jackson2JsonRedisSerializer<>(Person.class);
	RedisSerializationContextBuilder<String, Person> builder = RedisSerializationContext
			.newSerializationContext(new StringRedisSerializer());

	RedisSerializationContext<String, Person> serializationContext = builder.value(serializer).build();

	return new ReactiveRedisTemplate<>(connectionFactory, serializationContext);
}
 
Example #6
Source File: RedisCachingConfig.java    From POC with Apache License 2.0 5 votes vote down vote up
@Bean
public ReactiveRedisTemplate<String, Book> reactiveJsonBookRedisTemplate(
		ReactiveRedisConnectionFactory reactiveRedisConnectionFactory) {

	RedisSerializationContext<String, Book> serializationContext = RedisSerializationContext
			.<String, Book>newSerializationContext(new StringRedisSerializer()).hashKey(new StringRedisSerializer())
			.hashValue(new Jackson2JsonRedisSerializer<>(Book.class)).build();

	return new ReactiveRedisTemplate<>(reactiveRedisConnectionFactory, serializationContext);
}
 
Example #7
Source File: TracingRedisConnectionFactory.java    From java-redis-client with Apache License 2.0 5 votes vote down vote up
@Override
public ReactiveRedisClusterConnection getReactiveClusterConnection() {
  if (delegate instanceof ReactiveRedisConnectionFactory) {
    return ((ReactiveRedisConnectionFactory) delegate).getReactiveClusterConnection();
  }
  // TODO: shouldn't we throw an exception?
  return null;
}
 
Example #8
Source File: RedisTestConfiguration.java    From spring-data-examples with Apache License 2.0 5 votes vote down vote up
/**
 * Configures a {@link ReactiveRedisTemplate} with {@link String} keys and {@link GenericJackson2JsonRedisSerializer}.
 */
@Bean
public ReactiveRedisTemplate<String, Object> reactiveJsonObjectRedisTemplate(
		ReactiveRedisConnectionFactory connectionFactory) {

	RedisSerializationContextBuilder<String, Object> builder = RedisSerializationContext
			.newSerializationContext(new StringRedisSerializer());

	RedisSerializationContext<String, Object> serializationContext = builder
			.value(new GenericJackson2JsonRedisSerializer("_type")).build();

	return new ReactiveRedisTemplate<>(connectionFactory, serializationContext);
}
 
Example #9
Source File: RedisWebSessionConfiguration.java    From spring-session with Apache License 2.0 5 votes vote down vote up
@Autowired
public void setRedisConnectionFactory(
		@SpringSessionRedisConnectionFactory ObjectProvider<ReactiveRedisConnectionFactory> springSessionRedisConnectionFactory,
		ObjectProvider<ReactiveRedisConnectionFactory> redisConnectionFactory) {
	ReactiveRedisConnectionFactory redisConnectionFactoryToUse = springSessionRedisConnectionFactory
			.getIfAvailable();
	if (redisConnectionFactoryToUse == null) {
		redisConnectionFactoryToUse = redisConnectionFactory.getObject();
	}
	this.redisConnectionFactory = redisConnectionFactoryToUse;
}
 
Example #10
Source File: RedisWebSessionConfigurationTests.java    From spring-session with Apache License 2.0 5 votes vote down vote up
@Test
void qualifiedConnectionFactoryRedisConfig() {
	registerAndRefresh(RedisConfig.class, QualifiedConnectionFactoryRedisConfig.class);

	ReactiveRedisSessionRepository repository = this.context.getBean(ReactiveRedisSessionRepository.class);
	ReactiveRedisConnectionFactory redisConnectionFactory = this.context.getBean("qualifiedRedisConnectionFactory",
			ReactiveRedisConnectionFactory.class);
	assertThat(repository).isNotNull();
	assertThat(redisConnectionFactory).isNotNull();
	ReactiveRedisOperations redisOperations = (ReactiveRedisOperations) ReflectionTestUtils.getField(repository,
			"sessionRedisOperations");
	assertThat(redisOperations).isNotNull();
	assertThat(ReflectionTestUtils.getField(redisOperations, "connectionFactory"))
			.isEqualTo(redisConnectionFactory);
}
 
Example #11
Source File: RedisWebSessionConfigurationTests.java    From spring-session with Apache License 2.0 5 votes vote down vote up
@Test
void primaryConnectionFactoryRedisConfig() {
	registerAndRefresh(RedisConfig.class, PrimaryConnectionFactoryRedisConfig.class);

	ReactiveRedisSessionRepository repository = this.context.getBean(ReactiveRedisSessionRepository.class);
	ReactiveRedisConnectionFactory redisConnectionFactory = this.context.getBean("primaryRedisConnectionFactory",
			ReactiveRedisConnectionFactory.class);
	assertThat(repository).isNotNull();
	assertThat(redisConnectionFactory).isNotNull();
	ReactiveRedisOperations redisOperations = (ReactiveRedisOperations) ReflectionTestUtils.getField(repository,
			"sessionRedisOperations");
	assertThat(redisOperations).isNotNull();
	assertThat(ReflectionTestUtils.getField(redisOperations, "connectionFactory"))
			.isEqualTo(redisConnectionFactory);
}
 
Example #12
Source File: RedisWebSessionConfigurationTests.java    From spring-session with Apache License 2.0 5 votes vote down vote up
@Test
void qualifiedAndPrimaryConnectionFactoryRedisConfig() {
	registerAndRefresh(RedisConfig.class, QualifiedAndPrimaryConnectionFactoryRedisConfig.class);

	ReactiveRedisSessionRepository repository = this.context.getBean(ReactiveRedisSessionRepository.class);
	ReactiveRedisConnectionFactory redisConnectionFactory = this.context.getBean("qualifiedRedisConnectionFactory",
			ReactiveRedisConnectionFactory.class);
	assertThat(repository).isNotNull();
	assertThat(redisConnectionFactory).isNotNull();
	ReactiveRedisOperations redisOperations = (ReactiveRedisOperations) ReflectionTestUtils.getField(repository,
			"sessionRedisOperations");
	assertThat(redisOperations).isNotNull();
	assertThat(ReflectionTestUtils.getField(redisOperations, "connectionFactory"))
			.isEqualTo(redisConnectionFactory);
}
 
Example #13
Source File: DemoApplication.java    From spring-reactive-sample with GNU General Public License v3.0 5 votes vote down vote up
@Bean
public ReactiveRedisTemplate<String, Post> reactiveJsonPostRedisTemplate(
    ReactiveRedisConnectionFactory connectionFactory) {

    RedisSerializationContext<String, Post> serializationContext = RedisSerializationContext
        .<String, Post>newSerializationContext(new StringRedisSerializer())
        .hashKey(new StringRedisSerializer())
        .hashValue(new Jackson2JsonRedisSerializer<>(Post.class))
        .build();


    return new ReactiveRedisTemplate<>(connectionFactory, serializationContext);
}
 
Example #14
Source File: RedisConfig.java    From spring-reactive-sample with GNU General Public License v3.0 5 votes vote down vote up
@Bean
public ReactiveRedisTemplate<String, Post> reactiveRedisTemplate(ReactiveRedisConnectionFactory factory) {
    return new ReactiveRedisTemplate<String, Post>(
        factory,
        RedisSerializationContext.fromSerializer(new Jackson2JsonRedisSerializer(Post.class))
    );
}
 
Example #15
Source File: RedisWebSessionConfigurationTests.java    From spring-session with Apache License 2.0 5 votes vote down vote up
@Test
void namedConnectionFactoryRedisConfig() {
	registerAndRefresh(RedisConfig.class, NamedConnectionFactoryRedisConfig.class);

	ReactiveRedisSessionRepository repository = this.context.getBean(ReactiveRedisSessionRepository.class);
	ReactiveRedisConnectionFactory redisConnectionFactory = this.context.getBean("redisConnectionFactory",
			ReactiveRedisConnectionFactory.class);
	assertThat(repository).isNotNull();
	assertThat(redisConnectionFactory).isNotNull();
	ReactiveRedisOperations redisOperations = (ReactiveRedisOperations) ReflectionTestUtils.getField(repository,
			"sessionRedisOperations");
	assertThat(redisOperations).isNotNull();
	assertThat(ReflectionTestUtils.getField(redisOperations, "connectionFactory"))
			.isEqualTo(redisConnectionFactory);
}
 
Example #16
Source File: RedisConfig.java    From spring-reactive-sample with GNU General Public License v3.0 5 votes vote down vote up
@Bean
public ReactiveRedisTemplate<String, Post> reactiveRedisTemplate(ReactiveRedisConnectionFactory factory) {
    return new ReactiveRedisTemplate<String, Post>(
        factory,
        RedisSerializationContext.fromSerializer(new Jackson2JsonRedisSerializer(Post.class))
    );
}
 
Example #17
Source File: RedisConfig.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
@Bean
public ReactiveRedisTemplate<String, String> stringReactiveRedisTemplate(ReactiveRedisConnectionFactory connectionFactory){
    RedisSerializationContext<String, String> serializationContext = RedisSerializationContext
        .<String, String>newSerializationContext(new StringRedisSerializer())
        .hashKey(new StringRedisSerializer())
        .hashValue(new StringRedisSerializer())
        .build();
    return new ReactiveRedisTemplate<>(connectionFactory, serializationContext);
}
 
Example #18
Source File: RedisConfig.java    From tutorials with MIT License 5 votes vote down vote up
@Bean
public ReactiveRedisTemplate<String, Employee> reactiveRedisTemplate(ReactiveRedisConnectionFactory factory) {
    Jackson2JsonRedisSerializer<Employee> serializer = new Jackson2JsonRedisSerializer<>(Employee.class);
    RedisSerializationContext.RedisSerializationContextBuilder<String, Employee> builder = RedisSerializationContext.newSerializationContext(new StringRedisSerializer());
    RedisSerializationContext<String, Employee> context = builder.value(serializer)
        .build();
    return new ReactiveRedisTemplate<>(factory, context);
}
 
Example #19
Source File: RedisReactiveInitializer.java    From spring-fu with Apache License 2.0 5 votes vote down vote up
@Override
  public void initialize(GenericApplicationContext context) {
RedisReactiveAutoConfiguration redisAutoConfiguration = new RedisReactiveAutoConfiguration();
context.registerBean("reactiveRedisTemplate", ReactiveRedisTemplate.class,
		() -> redisAutoConfiguration.reactiveRedisTemplate(context.getBean(ReactiveRedisConnectionFactory.class), context),
		(definition) -> ((RootBeanDefinition) definition).setTargetType(ResolvableType.forClassWithGenerics(ReactiveRedisTemplate.class, Object.class, Object.class)));
context.registerBean("reactiveStringRedisTemplate", ReactiveStringRedisTemplate.class,
		() -> redisAutoConfiguration.reactiveStringRedisTemplate(context.getBean(ReactiveRedisConnectionFactory.class)));
  }
 
Example #20
Source File: RedisWebSessionConfigurationTests.java    From spring-session with Apache License 2.0 4 votes vote down vote up
@Bean
ReactiveRedisConnectionFactory defaultRedisConnectionFactory() {
	return mock(ReactiveRedisConnectionFactory.class);
}
 
Example #21
Source File: RedisWebSessionConfigurationTests.java    From spring-session with Apache License 2.0 4 votes vote down vote up
@Bean
@SpringSessionRedisConnectionFactory
ReactiveRedisConnectionFactory qualifiedRedisConnectionFactory() {
	return mock(ReactiveRedisConnectionFactory.class);
}
 
Example #22
Source File: RedisWebSessionConfigurationTests.java    From spring-session with Apache License 2.0 4 votes vote down vote up
@Bean
@Primary
ReactiveRedisConnectionFactory primaryRedisConnectionFactory() {
	return mock(ReactiveRedisConnectionFactory.class);
}
 
Example #23
Source File: RedisWebSessionConfigurationTests.java    From spring-session with Apache License 2.0 4 votes vote down vote up
@Bean
@SpringSessionRedisConnectionFactory
ReactiveRedisConnectionFactory qualifiedRedisConnectionFactory() {
	return mock(ReactiveRedisConnectionFactory.class);
}
 
Example #24
Source File: RedisWebSessionConfigurationTests.java    From spring-session with Apache License 2.0 4 votes vote down vote up
@Bean
@Primary
ReactiveRedisConnectionFactory primaryRedisConnectionFactory() {
	return mock(ReactiveRedisConnectionFactory.class);
}
 
Example #25
Source File: RedisWebSessionConfigurationTests.java    From spring-session with Apache License 2.0 4 votes vote down vote up
@Bean
ReactiveRedisConnectionFactory redisConnectionFactory() {
	return mock(ReactiveRedisConnectionFactory.class);
}
 
Example #26
Source File: RedisWebSessionConfigurationTests.java    From spring-session with Apache License 2.0 4 votes vote down vote up
@Bean
ReactiveRedisConnectionFactory secondaryRedisConnectionFactory() {
	return mock(ReactiveRedisConnectionFactory.class);
}
 
Example #27
Source File: RedisConfig.java    From tutorials with MIT License 4 votes vote down vote up
@Bean
public ReactiveRedisTemplate<String, String> reactiveRedisTemplateString(ReactiveRedisConnectionFactory connectionFactory) {
    return new ReactiveRedisTemplate<>(connectionFactory, RedisSerializationContext.string());
}
 
Example #28
Source File: RedisConfig.java    From tutorials with MIT License 4 votes vote down vote up
@Bean
public ReactiveKeyCommands keyCommands(final ReactiveRedisConnectionFactory reactiveRedisConnectionFactory) {
    return reactiveRedisConnectionFactory.getReactiveConnection()
        .keyCommands();
}
 
Example #29
Source File: RedisConfig.java    From tutorials with MIT License 4 votes vote down vote up
@Bean
public ReactiveStringCommands stringCommands(final ReactiveRedisConnectionFactory reactiveRedisConnectionFactory) {
    return reactiveRedisConnectionFactory.getReactiveConnection()
        .stringCommands();
}
 
Example #30
Source File: DataInitializer.java    From spring-reactive-sample with GNU General Public License v3.0 4 votes vote down vote up
public DataInitializer(ReactiveRedisConnectionFactory factory, PostRepository posts) {
    this.factory = factory;
    this.posts = posts;
}