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

The following examples show how to use org.springframework.data.redis.connection.ReactiveRedisConnectionFactory. These examples are extracted from open source projects. 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
@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
@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 3
@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 4
Source Project: Sentinel-Dashboard-Nacos   Source File: RedisConfig.java    License: 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 Project: spring-fu   Source File: RedisReactiveInitializer.java    License: 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 6
Source Project: Sentinel   Source File: RedisConfig.java    License: 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 7
@Bean
public ReactiveRedisTemplate<String, Post> reactiveRedisTemplate(ReactiveRedisConnectionFactory factory) {
    return new ReactiveRedisTemplate<String, Post>(
        factory,
        RedisSerializationContext.fromSerializer(new Jackson2JsonRedisSerializer(Post.class))
    );
}
 
Example 8
@Bean
public ReactiveRedisTemplate<String, Post> reactiveRedisTemplate(ReactiveRedisConnectionFactory factory) {
    return new ReactiveRedisTemplate<String, Post>(
        factory,
        RedisSerializationContext.fromSerializer(new Jackson2JsonRedisSerializer(Post.class))
    );
}
 
Example 9
@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 10
@Override
public ReactiveRedisClusterConnection getReactiveClusterConnection() {
  if (delegate instanceof ReactiveRedisConnectionFactory) {
    return ((ReactiveRedisConnectionFactory) delegate).getReactiveClusterConnection();
  }
  // TODO: shouldn't we throw an exception?
  return null;
}
 
Example 11
Source Project: POC   Source File: RedisCachingConfig.java    License: 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 12
/**
 * 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 13
/**
 * 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 14
@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 15
@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 16
@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 17
@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 18
@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 19
Source Project: tutorials   Source File: RedisConfig.java    License: 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 20
Source Project: spring-fu   Source File: LettuceRedisInitializer.java    License: Apache License 2.0 4 votes vote down vote up
@Override
  public void initialize(GenericApplicationContext context) {
context.registerBean(RedisConnectionFactory.class, () -> getLettuceConnectionFactory(context));
context.registerBean(ReactiveRedisConnectionFactory.class, () -> getLettuceConnectionFactory(context));
  }
 
Example 21
Source Project: spring-redis-websocket   Source File: HerokuRedisConfig.java    License: Apache License 2.0 4 votes vote down vote up
@Bean
ReactiveRedisConnectionFactory reactiveRedisConnectionFactory() {
	return lettuceConnectionFactory();
}
 
Example 22
Source Project: spring-redis-websocket   Source File: HerokuRedisConfig.java    License: Apache License 2.0 4 votes vote down vote up
@Bean
ReactiveStringRedisTemplate template(ReactiveRedisConnectionFactory reactiveRedisConnectionFactory) {
	return new ReactiveStringRedisTemplate(reactiveRedisConnectionFactory);
}
 
Example 23
Source Project: spring-redis-websocket   Source File: RedisConfig.java    License: Apache License 2.0 4 votes vote down vote up
@Bean
ReactiveRedisConnectionFactory reactiveRedisConnectionFactory(RedisProperties redisProperties) {
	RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration(redisProperties.getHost(), redisProperties.getPort());
	redisStandaloneConfiguration.setPassword(redisProperties.getPassword());
	return new LettuceConnectionFactory(redisStandaloneConfiguration);
}
 
Example 24
Source Project: spring-redis-websocket   Source File: RedisConfig.java    License: Apache License 2.0 4 votes vote down vote up
@Bean
ReactiveStringRedisTemplate template(ReactiveRedisConnectionFactory reactiveRedisConnectionFactory) {
	return new ReactiveStringRedisTemplate(reactiveRedisConnectionFactory);
}
 
Example 25
@Bean
public ReactiveRedisTemplate<String,String> reactiveRedisTemplate(ReactiveRedisConnectionFactory reactiveRedisConnectionFactory){
  return new ReactiveRedisTemplate<>(reactiveRedisConnectionFactory, RedisSerializationContext.string());
}
 
Example 26
@Bean
public ReactiveRedisTemplate<String,String> reactiveRedisTemplate(ReactiveRedisConnectionFactory reactiveRedisConnectionFactory){
  return new ReactiveRedisTemplate<>(reactiveRedisConnectionFactory, RedisSerializationContext.string());
}
 
Example 27
Source Project: Spring-Boot-2.0-Projects   Source File: RedisConfig.java    License: MIT License 4 votes vote down vote up
@Bean
public ReactiveRedisTemplate<String, String> reactiveRedisTemplate(ReactiveRedisConnectionFactory connectionFactory) {
    return new ReactiveRedisTemplate<>(connectionFactory, RedisSerializationContext.string());
}
 
Example 28
public DataInitializer(ReactiveRedisConnectionFactory factory, PostRepository posts) {
    this.factory = factory;
    this.posts = posts;
}
 
Example 29
public FavoriteController(ReactiveRedisConnectionFactory factory) {
    this.factory = factory;
}
 
Example 30
@Bean
public ReactiveRedisConnectionFactory connectionFactory() {
    return new LettuceConnectionFactory("localhost", 6379);
}