Java Code Examples for org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory

The following examples show how to use org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory. 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
Source Project: spring-cloud-aws   Source File: RedisCacheFactory.java    License: Apache License 2.0 7 votes vote down vote up
@Override
protected RedisConnectionFactory createConnectionClient(String hostName, int port) {
	RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration();
	configuration.setHostName(hostName);
	configuration.setPort(port);
	if (JEDIS_AVAILABLE) {
		return new JedisConnectionFactory(configuration);
	}
	else if (LETTUCE_AVAILABLE) {
		return new LettuceConnectionFactory(configuration);
	}
	else {
		throw new IllegalArgumentException("No Jedis or lettuce client on classpath. "
				+ "Please add one of the implementation to your classpath");
	}
}
 
Example 2
Source Project: SpringMVC-Project   Source File: RedisConfig.java    License: MIT License 7 votes vote down vote up
@Bean
public LettuceConnectionFactory redisConnectionFactory() {
    RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
    redisStandaloneConfiguration.setHostName(hostName);
    redisStandaloneConfiguration.setPort(port);

    GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
    poolConfig.setMaxIdle(maxIdle);
    poolConfig.setMinIdle(minIdle);
    poolConfig.setMaxWaitMillis(maxWaitMillis);
    poolConfig.setMaxTotal(maxTotal);

    LettucePoolingClientConfiguration lettucePoolingClientConfiguration = LettucePoolingClientConfiguration.builder()
            .commandTimeout(Duration.ofSeconds(10))
            .shutdownTimeout(Duration.ZERO)
            .poolConfig(poolConfig)
            .build();

    LettuceConnectionFactory lettuceConnectionFactory = new LettuceConnectionFactory(redisStandaloneConfiguration, lettucePoolingClientConfiguration);
    lettuceConnectionFactory.setShareNativeConnection(false);

    return lettuceConnectionFactory;
}
 
Example 3
Source Project: distributed-lock   Source File: EmbeddedRedis.java    License: MIT License 6 votes vote down vote up
@PostConstruct
public void start() throws IOException {
  // find free port
  final var serverSocket = new ServerSocket(0);
  final var port = serverSocket.getLocalPort();
  serverSocket.close();

  server = RedisServer.builder().setting("bind 127.0.0.1").port(port).build(); // bind to ignore windows firewall popup each time the server starts
  server.start();

  final var connectionFactory = new LettuceConnectionFactory(new RedisStandaloneConfiguration("localhost", port));
  connectionFactory.setDatabase(0);
  connectionFactory.afterPropertiesSet();

  // set the property so that RedisAutoConfiguration picks up the right port
  System.setProperty("spring.redis.port", String.valueOf(port));
}
 
Example 4
Source Project: SpringBootLearn   Source File: commons.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * RedisTemplate
 *
 * @return
 */
@Bean
public RedisTemplate<String, Serializable> limitRedisTemplate(LettuceConnectionFactory redisConnectionFactory) {
    RedisTemplate<String, Serializable> template = new RedisTemplate<>();
    template.setKeySerializer(new StringRedisSerializer());
    template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
    template.setConnectionFactory(redisConnectionFactory);
    return template;
}
 
Example 5
Source Project: microservice-recruit   Source File: CacheConfig.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * redis序列化配置,使用lettuce客户端
 */
@Bean
public <T> RedisTemplate<String, T> redisTemplate(LettuceConnectionFactory redisConnectionFactory) {
    RedisTemplate<String, T> template = new RedisTemplate<>();
    template.setKeySerializer(new PrefixRedisSerializer());
    template.setValueSerializer(new GenericFastJsonRedisSerializer());
    template.setConnectionFactory(redisConnectionFactory);
    template.setHashKeySerializer(new StringRedisSerializer());
    template.setHashValueSerializer(new GenericFastJsonRedisSerializer());
    template.afterPropertiesSet();
    return template;
}
 
Example 6
@Bean
public LettuceConnectionFactory lettuceConnectionFactory() {
	final LettuceConnectionFactory factory = new LettuceConnectionFactory();

	factory.setHostName(BotSettings.REDIS_HOSTNAME.get());
	factory.setPort(Integer.valueOf(BotSettings.REDIS_PORT.get()));
	factory.setPassword(BotSettings.REDIS_PASSWORD.get());

	return factory;
}
 
Example 7
Source Project: microservice-recruit   Source File: CacheConfig.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * redis序列化配置,使用lettuce客户端
 */
@Bean
public <T> RedisTemplate<String, T> redisTemplate(LettuceConnectionFactory redisConnectionFactory) {
    RedisTemplate<String, T> template = new RedisTemplate<>();
    template.setKeySerializer(new PrefixRedisSerializer());
    template.setValueSerializer(new GenericFastJsonRedisSerializer());
    template.setConnectionFactory(redisConnectionFactory);
    template.setHashKeySerializer(new StringRedisSerializer());
    template.setHashValueSerializer(new GenericFastJsonRedisSerializer());
    template.afterPropertiesSet();
    return template;
}
 
Example 8
Source Project: microservice-recruit   Source File: CacheConfig.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
public StringRedisTemplate stringRedisTemplate(LettuceConnectionFactory redisConnectionFactory) {
    StringRedisTemplate template = new StringRedisTemplate();
    template.setKeySerializer(new PrefixRedisSerializer());
    template.setValueSerializer(new StringRedisSerializer());
    template.setConnectionFactory(redisConnectionFactory);
    template.setHashKeySerializer(new StringRedisSerializer());
    template.setHashValueSerializer(new StringRedisSerializer());
    template.afterPropertiesSet();
    return template;
}
 
Example 9
Source Project: microservice-recruit   Source File: CacheConfig.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * redis序列化配置,使用lettuce客户端
 */
@Bean
public <T> RedisTemplate<String, T> redisTemplate(LettuceConnectionFactory redisConnectionFactory) {
    RedisTemplate<String, T> template = new RedisTemplate<>();
    template.setKeySerializer(new PrefixRedisSerializer());
    template.setValueSerializer(new GenericFastJsonRedisSerializer());
    template.setConnectionFactory(redisConnectionFactory);
    template.setHashKeySerializer(new StringRedisSerializer());
    template.setHashValueSerializer(new GenericFastJsonRedisSerializer());
    template.afterPropertiesSet();
    return template;
}
 
Example 10
Source Project: microservice-recruit   Source File: CacheConfig.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
public StringRedisTemplate stringRedisTemplate(LettuceConnectionFactory redisConnectionFactory) {
    StringRedisTemplate template = new StringRedisTemplate();
    template.setKeySerializer(new PrefixRedisSerializer());
    template.setValueSerializer(new StringRedisSerializer());
    template.setConnectionFactory(redisConnectionFactory);
    template.setHashKeySerializer(new StringRedisSerializer());
    template.setHashValueSerializer(new StringRedisSerializer());
    template.afterPropertiesSet();
    return template;
}
 
Example 11
@Bean(name = "redisTemplateB")
public StringRedisTemplate redisTemplateB(@Autowired @Qualifier("factoryB") LettuceConnectionFactory factoryB) {
    StringRedisTemplate template = new StringRedisTemplate(factoryB);
    Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
    ObjectMapper om = new ObjectMapper();
    om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
    // 将类名称序列化到json串中
    om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
    jackson2JsonRedisSerializer.setObjectMapper(om);
    template.setValueSerializer(jackson2JsonRedisSerializer);
    template.afterPropertiesSet();
    return template;
}
 
Example 12
Source Project: microservice-recruit   Source File: CacheConfig.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
public StringRedisTemplate stringRedisTemplate(LettuceConnectionFactory redisConnectionFactory) {
    StringRedisTemplate template = new StringRedisTemplate();
    template.setKeySerializer(new PrefixRedisSerializer());
    template.setValueSerializer(new StringRedisSerializer());
    template.setConnectionFactory(redisConnectionFactory);
    template.setHashKeySerializer(new StringRedisSerializer());
    template.setHashValueSerializer(new StringRedisSerializer());
    template.afterPropertiesSet();
    return template;
}
 
Example 13
Source Project: microservice-recruit   Source File: CacheConfig.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * redis序列化配置,使用lettuce客户端
 */
@Bean
public <T> RedisTemplate<String, T> redisTemplate(LettuceConnectionFactory redisConnectionFactory) {
    RedisTemplate<String, T> template = new RedisTemplate<>();
    template.setKeySerializer(new PrefixRedisSerializer());
    template.setValueSerializer(new GenericFastJsonRedisSerializer());
    template.setConnectionFactory(redisConnectionFactory);
    template.setHashKeySerializer(new StringRedisSerializer());
    template.setHashValueSerializer(new GenericFastJsonRedisSerializer());
    template.afterPropertiesSet();
    return template;
}
 
Example 14
Source Project: microservice-recruit   Source File: CacheConfig.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
public StringRedisTemplate stringRedisTemplate(LettuceConnectionFactory redisConnectionFactory) {
    StringRedisTemplate template = new StringRedisTemplate();
    template.setKeySerializer(new PrefixRedisSerializer());
    template.setValueSerializer(new StringRedisSerializer());
    template.setConnectionFactory(redisConnectionFactory);
    template.setHashKeySerializer(new StringRedisSerializer());
    template.setHashValueSerializer(new StringRedisSerializer());
    template.afterPropertiesSet();
    return template;
}
 
Example 15
Source Project: ultimate-redis-boot   Source File: CacheConfig.java    License: MIT License 5 votes vote down vote up
@Bean
public LettuceConnectionFactory redisConnectionFactory(CacheConfigurationProperties properties) {
    log.info("Redis (/Lettuce) configuration enabled. With cache timeout " + properties.getTimeoutSeconds() + " seconds.");

    RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
    redisStandaloneConfiguration.setHostName(properties.getRedisHost());
    redisStandaloneConfiguration.setPort(properties.getRedisPort());
    return new LettuceConnectionFactory(redisStandaloneConfiguration);
}
 
Example 16
Source Project: ChengFeng1.5   Source File: RedisConfig.java    License: MIT License 5 votes vote down vote up
@Bean
public RedisTemplate<String, Serializable> redisSerializableTemplate(LettuceConnectionFactory redisConnectionFactory) {
    RedisTemplate<String, Serializable> template = new RedisTemplate<>();
    template.setKeySerializer(new StringRedisSerializer());
    template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
    template.setConnectionFactory(redisConnectionFactory);
    return template;
}
 
Example 17
@Test
public void createMessageListenerContainer3() {
  simpleRqueueListenerContainerFactory.setRedisConnectionFactory(new LettuceConnectionFactory());
  simpleRqueueListenerContainerFactory.setRqueueMessageHandler(new RqueueMessageHandler());
  RqueueMessageListenerContainer container =
      simpleRqueueListenerContainerFactory.createMessageListenerContainer();
  assertNotNull(container);
  assertNotNull(container.getRqueueMessageHandler());
  assertTrue(container.isAutoStartup());
  assertNotNull(simpleRqueueListenerContainerFactory.getRqueueMessageTemplate());
}
 
Example 18
@Test
public void deadLetterMessageProcessor() {
  MessageProcessor messageProcessor = new MessageProcessor() {};
  simpleRqueueListenerContainerFactory.setDeadLetterQueueMessageProcessor(messageProcessor);
  assertEquals(
      messageProcessor,
      simpleRqueueListenerContainerFactory.getDeadLetterQueueMessageProcessor());
  simpleRqueueListenerContainerFactory.setRedisConnectionFactory(new LettuceConnectionFactory());
  simpleRqueueListenerContainerFactory.setRqueueMessageHandler(new RqueueMessageHandler());
  RqueueMessageListenerContainer container =
      simpleRqueueListenerContainerFactory.createMessageListenerContainer();
  assertNotNull(container);
  assertEquals(messageProcessor, container.getDeadLetterQueueMessageProcessor());
}
 
Example 19
@Test
public void discardMessageProcessor() {
  MessageProcessor messageProcessor = new MessageProcessor() {};
  simpleRqueueListenerContainerFactory.setDiscardMessageProcessor(messageProcessor);
  assertEquals(
      messageProcessor, simpleRqueueListenerContainerFactory.getDiscardMessageProcessor());
  simpleRqueueListenerContainerFactory.setRedisConnectionFactory(new LettuceConnectionFactory());
  simpleRqueueListenerContainerFactory.setRqueueMessageHandler(new RqueueMessageHandler());
  RqueueMessageListenerContainer container =
      simpleRqueueListenerContainerFactory.createMessageListenerContainer();
  assertNotNull(container);
  assertEquals(messageProcessor, container.getDiscardMessageProcessor());
}
 
Example 20
@Test
public void createContainer() {
  MessageProcessor pre = new MessageProcessor() {};
  MessageProcessor post = new MessageProcessor() {};
  MessageProcessor deadLetter = new MessageProcessor() {};
  MessageProcessor deletion = new MessageProcessor() {};
  MessageProcessor discardMessageProcessor = new MessageProcessor() {};
  TaskExecutionBackOff backOff = new FixedTaskExecutionBackOff(1000L, 4);
  AsyncTaskExecutor executor = new TestAsyncTaskExecutor();
  simpleRqueueListenerContainerFactory.setPostExecutionMessageProcessor(post);
  simpleRqueueListenerContainerFactory.setPreExecutionMessageProcessor(pre);
  simpleRqueueListenerContainerFactory.setDeadLetterQueueMessageProcessor(deadLetter);
  simpleRqueueListenerContainerFactory.setManualDeletionMessageProcessor(deletion);
  simpleRqueueListenerContainerFactory.setDiscardMessageProcessor(discardMessageProcessor);
  simpleRqueueListenerContainerFactory.setRedisConnectionFactory(new LettuceConnectionFactory());
  simpleRqueueListenerContainerFactory.setRqueueMessageHandler(new RqueueMessageHandler());
  simpleRqueueListenerContainerFactory.setTaskExecutionBackOff(backOff);
  simpleRqueueListenerContainerFactory.setTaskExecutor(executor);
  simpleRqueueListenerContainerFactory.setPriorityMode(PriorityMode.WEIGHTED);

  RqueueMessageListenerContainer container =
      simpleRqueueListenerContainerFactory.createMessageListenerContainer();
  assertNotNull(container);
  assertEquals(PriorityMode.WEIGHTED, container.getPriorityMode());
  assertEquals(backOff, container.getTaskExecutionBackOff());
  assertEquals(executor, container.getTaskExecutor());
  assertEquals(pre, container.getPreExecutionMessageProcessor());
  assertEquals(post, container.getPostExecutionMessageProcessor());
  assertEquals(deadLetter, container.getDeadLetterQueueMessageProcessor());
  assertEquals(deletion, container.getManualDeletionMessageProcessor());
  assertEquals(discardMessageProcessor, container.getDiscardMessageProcessor());
}
 
Example 21
Source Project: Milkomeda   Source File: MilkomedaContextConfig.java    License: MIT License 5 votes vote down vote up
@Bean
@ConditionalOnClass(name = "org.springframework.data.redis.core.RedisTemplate")
@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
public RedisTemplate<String, Serializable> jsonRedisTemplate(LettuceConnectionFactory redisConnectionFactory) {
    RedisTemplate<String, Serializable> template = new RedisTemplate<>();
    template.setKeySerializer(new StringRedisSerializer());
    template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
    template.setConnectionFactory(redisConnectionFactory);
    return template;
}
 
Example 22
Source Project: ad   Source File: RedisConfiguration.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
public LettuceConnectionFactory lettuceConnectionFactory() {
    LettucePoolingClientConfiguration configuration = LettucePoolingClientConfiguration.builder()
            .poolConfig(genericObjectPoolConfig())
            .commandTimeout(Duration.ofMillis(timeout))
            .build();
    LettuceConnectionFactory factory = new LettuceConnectionFactory(standaloneConfiguration(), configuration);
    factory.setShareNativeConnection(true);
    return factory;
}
 
Example 23
Source Project: DrivingAgency   Source File: RedisConfig.java    License: MIT License 5 votes vote down vote up
@Bean
public RedisTemplate<String, Serializable> redisSerializableTemplate(LettuceConnectionFactory redisConnectionFactory) {
    RedisTemplate<String, Serializable> template = new RedisTemplate<>();
    template.setKeySerializer(new StringRedisSerializer());
    template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
    template.setConnectionFactory(redisConnectionFactory);
    return template;
}
 
Example 24
Source Project: spring-boot-demo   Source File: RedisConfig.java    License: MIT License 5 votes vote down vote up
/**
 * 默认情况下的模板只能支持RedisTemplate<String, String>,也就是只能存入字符串,因此支持序列化
 */
@Bean
public RedisTemplate<String, Serializable> redisCacheTemplate(LettuceConnectionFactory redisConnectionFactory) {
    RedisTemplate<String, Serializable> template = new RedisTemplate<>();
    template.setKeySerializer(new StringRedisSerializer());
    template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
    template.setConnectionFactory(redisConnectionFactory);
    return template;
}
 
Example 25
public LettuceConnectionFactory createFactory(ClientResources clientResources) {
    LettuceClientConfiguration clientConfig = getLettuceClientConfiguration(clientResources,
        this.properties.getLettuce().getPool());
    LettuceConnectionFactory factory = createLettuceConnectionFactory(clientConfig);
    factory.afterPropertiesSet();
    return factory;
}
 
Example 26
private LettuceConnectionFactory createLettuceConnectionFactory(LettuceClientConfiguration clientConfiguration) {
    if (getSentinelConfig() != null) {
        return new LettuceConnectionFactory(getSentinelConfig(), clientConfiguration);
    }
    if (getClusterConfiguration() != null) {
        return new LettuceConnectionFactory(getClusterConfiguration(), clientConfiguration);
    }
    return new LettuceConnectionFactory(getStandaloneConfig(), clientConfiguration);
}
 
Example 27
Source Project: alcor   Source File: RedisConfiguration.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
LettuceConnectionFactory lettuceConnectionFactory() {
    RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration();
    configuration.setHostName(redisHostName);
    configuration.setPort(redisHostPort);
    return new LettuceConnectionFactory(configuration);
}
 
Example 28
Source Project: alcor   Source File: RedisConfiguration.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
LettuceConnectionFactory lettuceConnectionFactory() {
    RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration();
    configuration.setHostName(redisHostName);
    configuration.setPort(redisHostPort);
    return new LettuceConnectionFactory(configuration);
}
 
Example 29
Source Project: unimall   Source File: RedisAutoConfig.java    License: Apache License 2.0 5 votes vote down vote up
/**** 缓存专用数据源 ****/
@Bean
public LettuceConnectionFactory defaultLettuceConnectionFactory(
        RedisStandaloneConfiguration defaultRedisConfig,GenericObjectPoolConfig defaultPoolConfig) {
    LettuceClientConfiguration clientConfig =
            LettucePoolingClientConfiguration.builder().commandTimeout(Duration.ofMillis(5000))
                    .poolConfig(defaultPoolConfig).build();
    return new LettuceConnectionFactory(defaultRedisConfig, clientConfig);
}
 
Example 30
Source Project: unimall   Source File: RedisAutoConfig.java    License: Apache License 2.0 5 votes vote down vote up
/**** 用户SESSION专用数据源 ****/
@Bean
public LettuceConnectionFactory userLettuceConnectionFactory(
        RedisStandaloneConfiguration userRedisConfig,GenericObjectPoolConfig userPoolConfig) {
    LettuceClientConfiguration clientConfig =
            LettucePoolingClientConfiguration.builder().commandTimeout(Duration.ofMillis(5000))
                    .poolConfig(userPoolConfig).build();
    return new LettuceConnectionFactory(userRedisConfig, clientConfig);
}