使用多线程访问单个 Jedis 客户端

问题描述

我正在尝试访问具有多个线程的 jedis 客户端,其中一个线程可以简单地读取和写入数据库,而另一个线程可以订阅和侦听频道。这在绝地武士中可能吗?或者,是否可以通过任何其他同样有效的客户端将变量写入一个客户端。

导入redis.clients.jedis.Jedis; 导入 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"));
}

}

这就是我正在尝试的基本方法,请帮助我

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)