5

I am using Jedis to connect with a Redis server in a REST service.

When I am calling the web service I want to do operations like jedis.hmget , jedis.exits and hgetALL.

For example:

jedis.hmget("employee:data:" + emp_user_id, "employee_id").get(0);

The configuration that I am using for Redis is:

Jedis jedis;

    JedisShardInfo shardInfo;

    @PostConstruct
    public void init() {

        try {

            shardInfo = new JedisShardInfo(Config.getRedisHost(), Config.getRedisPort());
            shardInfo.setPassword(Config.getRedisPassword());
            jedis = new Jedis(shardInfo);
            jedis.select(2);
        //jedis.se
        } catch (Exception e) {
            logger.error("Exception in init ------- > " + e);
        }

    }

I know that Jedis is NOT thread safe. When I am using 1000 threads at once to call the service at that time I am getting an exception as Unexpected end of stream. I want to know Jedis pool is thread safe? Unable to find a specific solution for it.

Thanks. Any Help would be appreciated.

caffreyd
  • 1,151
  • 1
  • 17
  • 25
Abhijeet Behare
  • 597
  • 1
  • 7
  • 21

2 Answers2

5
JedisPool pool = new JedisPool(new JedisPoolConfig(), "localhost", portno, 10000,
            "password");

See here: https://github.com/xetorthio/jedis/wiki/Getting-started

KC Baltz
  • 1,498
  • 1
  • 13
  • 22
Radhika Mantri
  • 197
  • 1
  • 15
0

Check out Spring-data-redis.

When you add a JedisConnectionFactory you get a connectionFactory which has connection pooling capability by default.

JedisConnectionFactory() Constructs a new JedisConnectionFactory instance with default settings (default connection pooling, no shard information). See docs.

<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:p="http://www.springframework.org/schema/p"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" p:use-pool="true" p:host-name="server" p:port="6379"/>

</beans>

For further information, see the documentation.

Nico Van Belle
  • 4,911
  • 4
  • 32
  • 49
  • With JedisConnectionFactory we have to use RedisTemplate and Redistemplate does not support the operation which i am doing like: jedis.hmget("employee:data:" + emp_user_id, "employee_id").get(0); and Jedis.exits(string key); How this can be achieved ? – Abhijeet Behare Jun 16 '17 at 06:40
  • Check http://docs.spring.io/spring-data/redis/docs/1.8.4.RELEASE/reference/html/#_supported_commands and http://docs.spring.io/spring-data/redis/docs/current/api/org/springframework/data/redis/connection/RedisCommands.html . They are all supported. – Nico Van Belle Jun 16 '17 at 06:49
  • How we can implement the jedis.hmget("employee:data:" + emp_user_id, "employee_id").get(0); and Jedis.exits(string key); using redistemplate in java – Radhika Mantri Jun 16 '17 at 07:05
  • It's perfectly possible to use `JedisConnectionFactory` and `RedisTemplate` side by side. In fact, your `RedisTemplate` is probably already configured with a `JedisConnectionFactory`so why not use it?? Why is using the `JedisConnectionFactory` any worse from using the `xetorthio/jedis` internal classes to solve your initial question? – Nico Van Belle Jun 16 '17 at 07:35
  • I provided the direction you should search for, as asked. I'm not going to write the code for you as Stack Overflow is not a coding service. – Nico Van Belle Jun 16 '17 at 08:26