Java Code Examples for org.springframework.data.redis.connection.jedis.JedisConnectionFactory

The following examples show how to use org.springframework.data.redis.connection.jedis.JedisConnectionFactory. 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: demo-project   Source File: RedisConfig.java    License: MIT License 6 votes vote down vote up
@Bean
public JedisConnectionFactory jedisConnectionFactory() {
    logger.info("jedisConnectionFactory:初始化了");
    JedisPoolConfig config = new JedisPoolConfig();
    config.setMaxIdle(maxIdle);
    config.setMinIdle(minIdle);
    config.setMaxWaitMillis(maxWaitMillis);
    config.setMaxTotal(maxActive);
    //链接耗尽时是否阻塞,默认true
    config.setBlockWhenExhausted(true);
    //是否启用pool的jmx管理功能,默认true
    config.setJmxEnabled(true);
    JedisConnectionFactory factory = new JedisConnectionFactory();
    factory.setPoolConfig(config);
    factory.setHostName(host);
    factory.setPort(port);
    factory.setPassword(password);
    factory.setDatabase(database);
    factory.setTimeout(timeout);
    return factory;
}
 
Example 3
Source Project: storm_spring_boot_demo   Source File: RedisConfUtils.java    License: MIT License 6 votes vote down vote up
public static RedisTemplate buildRedisTemplate(byte[] redisProperties){
    JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(
            RedisConfUtils.getClusterConfiguration(
                    (RedisProperties) Serializer.INSTANCE.deserialize(redisProperties)));
    RedisTemplate<String, Long> redisTemplate = new RedisTemplate<>();
    redisTemplate.setConnectionFactory(jedisConnectionFactory);
    jedisConnectionFactory.afterPropertiesSet();

    GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
    StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
    redisTemplate.setKeySerializer(stringRedisSerializer);
    redisTemplate.setValueSerializer(genericJackson2JsonRedisSerializer);
    redisTemplate.setHashKeySerializer(stringRedisSerializer);
    redisTemplate.setHashValueSerializer(genericJackson2JsonRedisSerializer);
    redisTemplate.afterPropertiesSet();
    return redisTemplate;
}
 
Example 4
Source Project: redis-admin   Source File: RedisApplication.java    License: Apache License 2.0 6 votes vote down vote up
protected void createRedisConnection(String name, String host, int port, String password) {
	JedisConnectionFactory connectionFactory = new JedisConnectionFactory();
	connectionFactory.setHostName(host);
	connectionFactory.setPort(port);
	if(!StringUtils.isEmpty(password))
		connectionFactory.setPassword(password);
	connectionFactory.afterPropertiesSet();
	RedisTemplate redisTemplate = new MyStringRedisTemplate();
	redisTemplate.setConnectionFactory(connectionFactory);
	redisTemplate.afterPropertiesSet();
	RedisApplication.redisTemplatesMap.put(name, redisTemplate);
	
	Map<String, Object> redisServerMap = new HashMap<String, Object>();
	redisServerMap.put("name", name);
	redisServerMap.put("host", host);
	redisServerMap.put("port", port);
	redisServerMap.put("password", password);
	RedisApplication.redisServerCache.add(redisServerMap);
	
	initRedisKeysCache(redisTemplate, name);
	
	RedisZtreeUtil.initRedisNavigateZtree(name);
}
 
Example 5
Source Project: hot-crawler   Source File: RedisConfig.java    License: MIT License 5 votes vote down vote up
@Bean
JedisConnectionFactory jedisConnectionFactory()
{
    RedisProperties properties = redisProperties();
    RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration();
    configuration.setHostName(properties.getHost());
    configuration.setPort(properties.getPort());
    configuration.setPassword(properties.getPassword());
    configuration.setDatabase(properties.getDatabase());
    return new JedisConnectionFactory(configuration);
}
 
Example 6
Source Project: charging_pile_cloud   Source File: RedisConfig.java    License: MIT License 5 votes vote down vote up
@Bean
@Primary
JedisConnectionFactory jedisConnectionFactory() {
    RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration ();
    redisStandaloneConfiguration.setHostName(redisProperties.getHost());
    redisStandaloneConfiguration.setPort(redisProperties.getPort());
    redisStandaloneConfiguration.setPassword(RedisPassword.of(redisProperties.getPassword()));
    redisStandaloneConfiguration.setDatabase(RedisDbIndexConst.DB_INDEX_DEFAULT);
    JedisClientConfiguration.JedisClientConfigurationBuilder jedisClientConfiguration = JedisClientConfiguration.builder();

    JedisConnectionFactory factory = new JedisConnectionFactory(redisStandaloneConfiguration,
            jedisClientConfiguration.usePooling().poolConfig(jedisPoolConfig()).build());
    return factory;
}
 
Example 7
Source Project: charging_pile_cloud   Source File: RedisConfig.java    License: MIT License 5 votes vote down vote up
@Bean
JedisConnectionFactory jedisConnectionFactory1() {
    RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration ();
    redisStandaloneConfiguration.setHostName(redisProperties.getHost());
    redisStandaloneConfiguration.setPort(redisProperties.getPort());
    redisStandaloneConfiguration.setPassword(RedisPassword.of(redisProperties.getPassword()));
    redisStandaloneConfiguration.setDatabase(RedisDbIndexConst.DB_INDEX_1);
    JedisClientConfiguration.JedisClientConfigurationBuilder jedisClientConfiguration = JedisClientConfiguration.builder();

    JedisConnectionFactory factory = new JedisConnectionFactory(redisStandaloneConfiguration,
            jedisClientConfiguration.usePooling().poolConfig(jedisPoolConfig()).build());
    return factory;
}
 
Example 8
Source Project: charging_pile_cloud   Source File: RedisConfig.java    License: MIT License 5 votes vote down vote up
@Bean
JedisConnectionFactory jedisConnectionFactory2() {
    RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration ();
    redisStandaloneConfiguration.setHostName(redisProperties.getHost());
    redisStandaloneConfiguration.setPort(redisProperties.getPort());
    redisStandaloneConfiguration.setPassword(RedisPassword.of(redisProperties.getPassword()));
    redisStandaloneConfiguration.setDatabase(RedisDbIndexConst.DB_INDEX_2);
    JedisClientConfiguration.JedisClientConfigurationBuilder jedisClientConfiguration = JedisClientConfiguration.builder();

    JedisConnectionFactory factory = new JedisConnectionFactory(redisStandaloneConfiguration,
            jedisClientConfiguration.usePooling().poolConfig(jedisPoolConfig()).build());
    return factory;
}
 
Example 9
Source Project: charging_pile_cloud   Source File: RedisConfig.java    License: MIT License 5 votes vote down vote up
@Bean
JedisConnectionFactory jedisConnectionFactory3() {
    RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration ();
    redisStandaloneConfiguration.setHostName(redisProperties.getHost());
    redisStandaloneConfiguration.setPort(redisProperties.getPort());
    redisStandaloneConfiguration.setPassword(RedisPassword.of(redisProperties.getPassword()));
    redisStandaloneConfiguration.setDatabase(RedisDbIndexConst.DB_INDEX_3);
    JedisClientConfiguration.JedisClientConfigurationBuilder jedisClientConfiguration = JedisClientConfiguration.builder();

    JedisConnectionFactory factory = new JedisConnectionFactory(redisStandaloneConfiguration,
            jedisClientConfiguration.usePooling().poolConfig(jedisPoolConfig()).build());
    return factory;
}
 
Example 10
Source Project: spring4-sandbox   Source File: RedisCacheConfig.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
public JedisConnectionFactory jedisConnectionFactory() {
	// JedisPoolConfig poolConfig=new JedisPoolConfig();
	// poolConfig.set

	JedisConnectionFactory connectionFactory = new JedisConnectionFactory();
	connectionFactory.setHostName("192.168.1.103");
	connectionFactory.setUsePool(true);
	return connectionFactory;
}
 
Example 11
Source Project: Project   Source File: CachingConfig2.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
public RedisConnectionFactory redisConnectionFactory() {
	JedisConnectionFactory connectionFactory = new JedisConnectionFactory();
	connectionFactory.setHostName("192.168.198.129");
	connectionFactory.setPassword("123456");
	return connectionFactory;
}
 
Example 12
Source Project: summerframework   Source File: RedisPoolMeterBinder.java    License: Apache License 2.0 5 votes vote down vote up
public RedisPoolMeterBinder(Iterable<Tag> tags, JedisConnectionFactory jedisConnectionFactory) {
    this.tags = tags;
    Field poolField = ReflectionUtils.findField(JedisConnectionFactory.class, "pool");
    ReflectionUtils.makeAccessible(poolField);
    try {
        this.pool = (Pool<Jedis>)poolField.get(jedisConnectionFactory);
    } catch (Throwable e) {
        log.error(e.getMessage(), e);
    }
}
 
Example 13
Source Project: spring-boot-vue-admin   Source File: RedisConfig.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
@ConfigurationProperties(prefix = "spring.redis")
public RedisConnectionFactory redisConnectionFactory(
    @Qualifier(value = "jedisPoolConfig") final JedisPoolConfig jedisPoolConfig) {
  // 方法上的 @ConfigurationProperties 不生效
  // 未知 bug,暂时这样手动设置
  // fixme
  // 单机版 jedis
  final RedisStandaloneConfiguration redisStandaloneConfiguration =
      new RedisStandaloneConfiguration(
          this.redisProperties.getHost(), this.redisProperties.getPort());
  redisStandaloneConfiguration.setDatabase(this.redisProperties.getDatabase());
  redisStandaloneConfiguration.setPassword(this.redisProperties.getPassword());

  // 获得默认的连接池构造器
  final JedisClientConfiguration.JedisPoolingClientConfigurationBuilder jpcb =
      (JedisClientConfiguration.JedisPoolingClientConfigurationBuilder)
          JedisClientConfiguration.builder();
  // 指定 jedisPoolConifig 来修改默认的连接池构造器
  jpcb.poolConfig(jedisPoolConfig);
  // 连接超时
  jpcb.and().connectTimeout(Duration.ofSeconds(10));
  // 通过构造器来构造 jedis 客户端配置
  final JedisClientConfiguration jedisClientConfiguration = jpcb.build();
  // 单机配置 + 客户端配置 = jedis 连接工厂
  return new JedisConnectionFactory(redisStandaloneConfiguration, jedisClientConfiguration);
}
 
Example 14
@Bean
  public JedisConnectionFactory connectionFactory() {

JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
jedisConnectionFactory.setHostName(env.getProperty("redis.host"));
jedisConnectionFactory.setPort(Integer.parseInt(env.getProperty("redis.port")));
jedisConnectionFactory.setUsePool(true);
return jedisConnectionFactory;
  }
 
Example 15
Source Project: tac   Source File: RedisBeansConfig.java    License: MIT License 5 votes vote down vote up
/**
 * @param jedisConnectionFactory
 * @return
 */
public static RedisTemplate getCounterRedisTemplate(JedisConnectionFactory jedisConnectionFactory) {
    RedisTemplate redisTemplate = new RedisTemplate();
    redisTemplate.setConnectionFactory(jedisConnectionFactory);
    // the conter should use  StringRedisSerializer
    redisTemplate.setValueSerializer(new StringRedisSerializer());
    return redisTemplate;
}
 
Example 16
Source Project: tac   Source File: RedisBeansConfig.java    License: MIT License 5 votes vote down vote up
@Bean
public RedisMessageListenerContainer redisMessageListenerContainer(JedisConnectionFactory jedisConnectionFactory) {
    RedisMessageListenerContainer container = new RedisMessageListenerContainer();
    container.setConnectionFactory(jedisConnectionFactory);
    // set thread pool
    container.setTaskExecutor(ThreadPoolUtils.createThreadPool(10, "tac-redis-subscribe-pool"));
    return container;
}
 
Example 17
Source Project: tac   Source File: RedisConfig.java    License: MIT License 5 votes vote down vote up
@Bean
public RedisMessageListenerContainer redisMessageListenerContainer(JedisConnectionFactory jedisConnectionFactory,
                                                                   MessageListenerAdapter listenerAdapter) {

    RedisMessageListenerContainer container = new RedisMessageListenerContainer();
    container.setConnectionFactory(jedisConnectionFactory);
    container.addMessageListener(listenerAdapter, new ChannelTopic("topicA"));
    // 设置线程池
    //container.setTaskExecutor(null);
    return container;
}
 
Example 18
@Bean("j2CacheRedisTemplate")
@ConditionalOnBean(name = "j2CahceRedisConnectionFactory")
public RedisTemplate<String, Serializable> j2CacheRedisTemplate(
		@Qualifier("j2CahceRedisConnectionFactory") JedisConnectionFactory j2CahceRedisConnectionFactory) {
	RedisTemplate<String, Serializable> template = new RedisTemplate<String, Serializable>();
	template.setKeySerializer(new StringRedisSerializer());
	template.setHashKeySerializer(new StringRedisSerializer());
	template.setDefaultSerializer(new J2CacheSerializer());
	template.setConnectionFactory(j2CahceRedisConnectionFactory);
	return template;
}
 
Example 19
/**
 * redis 数据库连接池
 * @return
 */

@Bean
public JedisConnectionFactory redisConnectionFactory() {
    JedisConnectionFactory factory = new JedisConnectionFactory();
    factory.setHostName(redisConn.getHost());
    factory.setPort(redisConn.getPort());
    factory.setTimeout(redisConn.getTimeout()); // 设置连接超时时间
    return factory;
}
 
Example 20
public static void assertNoPoolProperties(RedisConnectionFactory connector) {
	if (connector instanceof JedisConnectionFactory) {
		assertFalse(((JedisConnectionFactory) connector).getUsePool());
	} else if (connector instanceof LettuceConnectionFactory) {
		LettuceClientConfiguration config = ((LettuceConnectionFactory) connector).getClientConfiguration();
		assertThat(config, instanceOf(LettuceClientConfiguration.class));
	}
}
 
Example 21
public static void assertPoolProperties(RedisConnectionFactory connector, int maxActive, int minIdle, long maxWait) {
	GenericObjectPoolConfig poolConfig = null;
	if (connector instanceof JedisConnectionFactory) {
		poolConfig = ((JedisConnectionFactory) connector).getPoolConfig();
	} else if (connector instanceof LettuceConnectionFactory) {
		LettuceClientConfiguration config = ((LettuceConnectionFactory) connector).getClientConfiguration();
		assertThat(config, instanceOf(LettucePoolingClientConfiguration.class));
		poolConfig = ((LettucePoolingClientConfiguration) config).getPoolConfig();
	}
	assertCommonsPoolProperties(poolConfig, maxActive, minIdle, maxWait);
}
 
Example 22
Source Project: Mykit   Source File: RedisCacheConfig.java    License: Apache License 2.0 5 votes vote down vote up
public RedisCacheConfig(JedisConnectionFactory mJedisConnectionFactory, RedisTemplate<String,String> mRedisTemplate,
		RedisCacheManager mRedisCacheManager) {
	super();
	this.mJedisConnectionFactory = mJedisConnectionFactory;
	this.mRedisTemplate = mRedisTemplate;
	this.mRedisCacheManager = mRedisCacheManager;
}
 
Example 23
Source Project: Redis-4.x-Cookbook   Source File: AppConfig.java    License: MIT License 5 votes vote down vote up
@Bean
   JedisConnectionFactory jedisConnectionFactory(){
	JedisConnectionFactory factory = new JedisConnectionFactory();
	factory.setHostName(redisHost);
	factory.setPort(redisPort);
	factory.setUsePool(true);
	return factory;
}
 
Example 24
Source Project: watchdog-framework   Source File: RedisCacheConfig.java    License: MIT License 5 votes vote down vote up
@Bean
public RedisTemplate<Object, Object> redisTemplate(JedisConnectionFactory jedisConnectionFactory) {
    RedisTemplate<Object, Object> template = new RedisTemplate<>();
    template.setConnectionFactory(jedisConnectionFactory);
    template.setKeySerializer(new StringRedisSerializer());
    template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
    template.afterPropertiesSet();
    return template;
}
 
Example 25
/**
 * Build the connection to Redis server.
 * 
 * @return  The object of {@code JedisConnectionFactory}.
 */
@Bean
public JedisConnectionFactory jedisConnectionFactory() {
    JedisConnectionFactory jedisConnFactory = new JedisConnectionFactory();
    jedisConnFactory.setHostName(serviceConfig.getRedisServer());
    jedisConnFactory.setPort(serviceConfig.getRedisPort());
    return jedisConnFactory;
}
 
Example 26
Source Project: howsun-javaee-framework   Source File: SpringDataRedis.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * @param args
 */
public static void main(String[] args) {
	JedisShardInfo jedisShardInfo1 = new JedisShardInfo(ip1);
	jedisShardInfo1.setPassword(JedisConstant.password);
	JedisShardInfo jedisShardInfo2 = new JedisShardInfo(ip2);
	jedisShardInfo2.setPassword(JedisConstant.password);

	List<JedisShardInfo> jedisShardInfos = new ArrayList<JedisShardInfo>();
	jedisShardInfos.add(jedisShardInfo1);
	jedisShardInfos.add(jedisShardInfo2);

	JedisPoolConfig poolConfig = new JedisPoolConfig();
	poolConfig.setMaxActive(JedisConstant.maxActive);
	poolConfig.setMaxIdle(JedisConstant.maxIdle);
	poolConfig.setMaxWait(JedisConstant.maxWait);
	poolConfig.setTestOnBorrow(JedisConstant.testOnBorrow);
	poolConfig.setTestOnReturn(JedisConstant.testOnReturn);


	ShardedJedisPool shardedJedisPool = new ShardedJedisPool(poolConfig, jedisShardInfos);

	JedisConnectionFactory factory = new JedisConnectionFactory(jedisShardInfo1);
	StringRedisTemplate template = new StringRedisTemplate(factory);
	for (int i = 0; i < 2000; i++) {
		String key = "howsun_" + i;
		BoundValueOperations<String, String> v = template.boundValueOps(key);
		//jedis.set(key, UUID.randomUUID().toString());
		System.out.println(key + "\t" + v.get() + "\t" + factory.getHostName());
	}

}
 
Example 27
Source Project: heimdall   Source File: RedisConfiguration.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Configures and returns a {@link JedisConnectionFactory}.
 * 
 * @return {@link JedisConnectionFactory}
 */
@Bean
public JedisConnectionFactory jedisConnectionFactory() {
     
     JedisConnectionFactory factory = new JedisConnectionFactory();
     
     factory.setHostName(property.getRedis().getHost());
     factory.setPort(property.getRedis().getPort());
     factory.setUsePool(true);
     factory.setPoolConfig(jediPoolConfig());
     return factory;
}
 
Example 28
Source Project: WTFDYUM   Source File: RedisConfiguration.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
public RedisConnectionFactory redisConnectionFactory() {
    final JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
    jedisConnectionFactory.setHostName(env.getProperty("wtfdyum.redis.server"));
    jedisConnectionFactory.setPort(env.getProperty("wtfdyum.redis.port", Integer.class));
    jedisConnectionFactory.setDatabase(env.getProperty("wtfdyum.redis.database", Integer.class));
    return jedisConnectionFactory;
}
 
Example 29
Source Project: demo-project   Source File: RedisConfig.java    License: MIT License 5 votes vote down vote up
/**
 * 设置缓存管理器,这里可以配置默认过期时间等
 *
 * @param connectionFactory 连接池
 * @return
 */
@Bean
public CacheManager cacheManager(JedisConnectionFactory jedisConnectionFactory) {
    RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration
            .defaultCacheConfig()
            .entryTtl(Duration.ofSeconds(60));
    RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(jedisConnectionFactory);
    RedisCacheManager manager = new RedisCacheManager(redisCacheWriter, redisCacheConfiguration);
    return manager;
}
 
Example 30
Source Project: tx-lcn   Source File: RedisConfig.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
@ConfigurationProperties(prefix = "spring.redis")
public JedisConnectionFactory getConnectionFactory() {
    JedisConnectionFactory factory = new JedisConnectionFactory();
    JedisPoolConfig config = getRedisConfig();
    factory.setPoolConfig(config);
    logger.info("JedisConnectionFactory bean init success.");
    return factory;
}