jedis pubsub 和超时:如何作为订阅者无限收听?

发布于 2024-12-22 08:26:34 字数 1079 浏览 6 评论 0原文

我正在努力解决创建 Jedis 客户端的概念,该客户端作为 Redis pubsub 通道的订阅者无限监听并在消息进入时处理消息。

我的问题是,在不活动一段时间后,服务器停止默默响应。我认为这是由于我订阅的 Jedis 客户端发生超时造成的。

情况确实可能如此吗?如果是这样,有没有办法配置这个特定的 Jedis 客户端不超时? (虽然其他 Jedispools 不受某些全局设置超时的影响) 或者,是否有另一种(最佳实践)方式来实现我想要实现的目标?

这是我的代码,(修改/删除以供显示):

在网络服务器启动期间执行:

new Thread(AkkaStarter2.getSingleton()).start();

AkkaStarter2.java

   private Jedis sub;
   private AkkaListener akkaListener;

   public static AkkaStarter2 getSingleton(){
      if(singleton==null){
        singleton = new AkkaStarter2();
      }
      return singleton;
    }

    private AkkaStarter2(){
      sub = new Jedis(REDISHOST, REDISPORT);
      akkaListener = new AkkaListener();
    }

    public void run() {
      //blocking
      sub.psubscribe(akkaListener, AKKAPREFIX + "*");
    }

    class AkkaListener extends JedisPubSub {
        ....
        public void onPMessage(String pattern, String akkaChannel,String jsonSer) {
          ...
        }
    }

谢谢。

I'm struggling with the concept of creating a Jedis-client which listens infinitely as a subscriber to a Redis pubsub channel and handles messages when they come in.

My problem is that after a while of inactivity the server stops responding silently. I think this is due to a timeout occurring on the Jedis-client I subscribe with.

Would this likely indeed be the case? If so, is there a way to configure this particular Jedis-client to not timeout? (While other Jedispools aren't affected with some globally set timeout)
Alternatively, is there another (best practice) way of what I'm trying to achieve?

This is my code, (modified/ stripped for display) :

executed during web-server startup:

new Thread(AkkaStarter2.getSingleton()).start();

AkkaStarter2.java

   private Jedis sub;
   private AkkaListener akkaListener;

   public static AkkaStarter2 getSingleton(){
      if(singleton==null){
        singleton = new AkkaStarter2();
      }
      return singleton;
    }

    private AkkaStarter2(){
      sub = new Jedis(REDISHOST, REDISPORT);
      akkaListener = new AkkaListener();
    }

    public void run() {
      //blocking
      sub.psubscribe(akkaListener, AKKAPREFIX + "*");
    }

    class AkkaListener extends JedisPubSub {
        ....
        public void onPMessage(String pattern, String akkaChannel,String jsonSer) {
          ...
        }
    }

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

静若繁花 2024-12-29 08:26:34

ermmm,下面解决了这一切。确实这是绝地武士的事

private AkkaStarter2(){
  //0 specifying no timeout.. Overlooked this 100 times
  sub = new Jedis(REDISHOST, REDISPORT,0); 
  akkaListener = new AkkaListener();
}

ermmm, the below solves it all. Indeed it was a Jedis thing

private AkkaStarter2(){
  //0 specifying no timeout.. Overlooked this 100 times
  sub = new Jedis(REDISHOST, REDISPORT,0); 
  akkaListener = new AkkaListener();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文