Using Multiple threads to access a Single Jedis client

Viewed 298

I am trying to access a jedis client with multiple threads where one thread can simple read and write to the database while the other thread can subscribe and a listen to a channel . Is this possible in jedis . Or if it is possible to write variables to one client by any other client that would also work.

import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPubSub;

public class Testing1 {
public static void main(String args[])
{
   final Jedis jedis = new Jedis("localhost");
    Runnable runnable =()->
    {
        System.out.println(jedis.clientId());
        jedis.set("acd","acg");
        String str=jedis.get("acd");
        System.out.println(str);

    };
    Runnable runnable2 =()->
    {
        System.out.println(jedis.clientId());
        JedisPubSub jedisPubSub = new JedisPubSub()
        {
            @Override
            public void onMessage(String channel, String message) {
                System.out.println("Channel " + channel + " has sent a message : " + message );
                if(channel.equals("C1")) {
                    /* Unsubscribe from channel C1 after first message is received. */
                    unsubscribe(channel);
                }
            }

            @Override
            public void onSubscribe(String channel, int subscribedChannels) {
                System.out.println("Client is Subscribed to channel : "+ channel);
                System.out.println("Client is Subscribed to "+ subscribedChannels + " no. of channels");
            }

            @Override
            public void onUnsubscribe(String channel, int subscribedChannels) {
                System.out.println("Client is Unsubscribed from channel : "+ channel);
                System.out.println("Client is Subscribed to "+ subscribedChannels + " no. of channels");
            }

        };
        jedis.subscribe(jedisPubSub, "__redis__:invalidate");
    };


  / Thread thread= new Thread(runnable);
    Thread thread1= new Thread(runnable2);
    thread.start();
    thread1.start();
    jedis.set("harry","veer");
    System.out.println(jedis.get("harry"));
}

}

Here is what I am trying basically please help me with this

0 Answers
Related