當前位置: 首頁>>代碼示例>>Java>>正文


Java JedisPubSub類代碼示例

本文整理匯總了Java中redis.clients.jedis.JedisPubSub的典型用法代碼示例。如果您正苦於以下問題:Java JedisPubSub類的具體用法?Java JedisPubSub怎麽用?Java JedisPubSub使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


JedisPubSub類屬於redis.clients.jedis包,在下文中一共展示了JedisPubSub類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: MessageConsumerRedisImpl

import redis.clients.jedis.JedisPubSub; //導入依賴的package包/類
/**
 *
 * @param redisConnection redis 連接類
 * @param channels 訂閱的頻道列表
 */
public MessageConsumerRedisImpl(RedisConnection redisConnection, String[] channels) {
    Jedis jedis = null;
    try {
        if (channels != null && channels.length > 0) {
            jedis = redisConnection.getJedis();
            jedis.subscribe(new JedisPubSub() {
                @Override
                public void onMessage(String channel, String message) {
                    System.out.println("receive " + message + " from " + channel);
                    handleMessage(message);
                }
            }, channels);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (jedis != null) {
            jedis.close();
        }
    }
}
 
開發者ID:gaochao2000,項目名稱:redis_util,代碼行數:27,代碼來源:MessageConsumerRedisImpl.java

示例2: pubSubNumPat

import redis.clients.jedis.JedisPubSub; //導入依賴的package包/類
@Test
public void pubSubNumPat() {
  jedis.psubscribe(new JedisPubSub() {
    private int count = 0;

    @Override
    public void onPSubscribe(String pattern, int subscribedChannels) {
      count++;
      if (count == 3) {
        Jedis otherJedis = createJedis();
        Long numPatterns = otherJedis.pubsubNumPat();
        assertEquals(new Long(2l), numPatterns);
        punsubscribe();
      }
    }

  }, "test*", "test*", "chan*");
}
 
開發者ID:qq1588518,項目名稱:JRediClients,代碼行數:19,代碼來源:PublishSubscribeCommandsTest.java

示例3: pubSubChannels

import redis.clients.jedis.JedisPubSub; //導入依賴的package包/類
@Test
public void pubSubChannels() {
  final List<String> expectedActiveChannels = Arrays
      .asList("testchan1", "testchan2", "testchan3");
  jedis.subscribe(new JedisPubSub() {
    private int count = 0;

    @Override
    public void onSubscribe(String channel, int subscribedChannels) {
      count++;
      // All channels are subscribed
      if (count == 3) {
        Jedis otherJedis = createJedis();
        List<String> activeChannels = otherJedis.pubsubChannels("test*");
        assertTrue(expectedActiveChannels.containsAll(activeChannels));
        unsubscribe();
      }
    }
  }, "testchan1", "testchan2", "testchan3");
}
 
開發者ID:qq1588518,項目名稱:JRediClients,代碼行數:21,代碼來源:PublishSubscribeCommandsTest.java

示例4: pubSubNumSub

import redis.clients.jedis.JedisPubSub; //導入依賴的package包/類
@Test
public void pubSubNumSub() {
  final Map<String, String> expectedNumSub = new HashMap<String, String>();
  expectedNumSub.put("testchannel2", "1");
  expectedNumSub.put("testchannel1", "1");
  jedis.subscribe(new JedisPubSub() {
    private int count = 0;

    @Override
    public void onSubscribe(String channel, int subscribedChannels) {
      count++;
      if (count == 2) {
        Jedis otherJedis = createJedis();
        Map<String, String> numSub = otherJedis.pubsubNumSub("testchannel1", "testchannel2");
        assertEquals(expectedNumSub, numSub);
        unsubscribe();
      }
    }
  }, "testchannel1", "testchannel2");
}
 
開發者ID:qq1588518,項目名稱:JRediClients,代碼行數:21,代碼來源:PublishSubscribeCommandsTest.java

示例5: subscribe

import redis.clients.jedis.JedisPubSub; //導入依賴的package包/類
public void subscribe(JedisPubSub jedisPubSub, String channel) {
    Collection<JedisPool> poolCollection = jedisCluster.getClusterNodes().values();
    Iterator iterator = poolCollection.iterator();

    while(iterator.hasNext()) {
        JedisPool jedisPool = (JedisPool)iterator.next();
        Jedis jedis = null;

        try {
            jedis = jedisPool.getResource();
            jedis.subscribe(jedisPubSub, new String[]{channel});
        } catch (Exception var11) {
            throw new RuntimeException(var11);
        } finally {
            jedisPool.returnResourceObject(jedis);
        }
    }

}
 
開發者ID:bitstd,項目名稱:bitstd,代碼行數:20,代碼來源:RedisCacheWithCluster.java

示例6: startSubscribe

import redis.clients.jedis.JedisPubSub; //導入依賴的package包/類
public void startSubscribe() {

        // 支持短線重連
        new Thread(() -> {
            while (true) {
                try {
                    Jedis jedisSub = this.createJedis();
                    jedisSub.psubscribe(new JedisPubSub() {
                        public void onPMessage(String pattern, String channel, String message) {
                            vertx.eventBus().publish(channel, message);
                        }
                    }, "*");
                } catch (Exception e) {
                    e.printStackTrace();
                    try {
                        Thread.sleep(1000l);
                    } catch (InterruptedException e1) {
                        e1.printStackTrace();
                    }
                }
            }
        }, "vertx-jedis-pubsub").start();
    }
 
開發者ID:kuangcao,項目名稱:ninja-vertx-standalone,代碼行數:24,代碼來源:PubSub.java

示例7: createSubscriptionListener

import redis.clients.jedis.JedisPubSub; //導入依賴的package包/類
private void createSubscriptionListener() {
    subscriptions = new JedisPubSub() {
        @Override
        public void onMessage(String channel, String message) {
            Collection<Function<byte[], Boolean>> currentActions = actionMap.get(channel);
            if(currentActions != null) {
                for(Function<byte[], Boolean> currentAction : currentActions) {
                    if(!currentAction.apply(toStringCodex.decode(message))) {
                        log.warning("attempt to not ack " + channel + " however this is not supported");
                    }
                }
            }

        }
    };
}
 
開發者ID:miniature-queue,項目名稱:miniature-queue,代碼行數:17,代碼來源:JedisServerImplementation.java

示例8: init

import redis.clients.jedis.JedisPubSub; //導入依賴的package包/類
@PostConstruct
public void init() {
    new Thread() {
        @Override
        public void run() {
            redisDao.subscribe(new JedisPubSub() {
                @Override
                public void onMessage(String pchannel, String message) {
                    logger.info("redis通知,channel={},message={}", pchannel, message);
                    if (channel.equals(pchannel)) {
                        updateCache();
                    }
                }
            }, channel);
        }
    }.start();

    updateCache();
}
 
開發者ID:ysrc,項目名稱:Liudao,代碼行數:20,代碼來源:ConfigService.java

示例9: setupPubSub

import redis.clients.jedis.JedisPubSub; //導入依賴的package包/類
private JedisPubSub setupPubSub() {
    final JedisPubSub jedisPubSub = new JedisPubSub() {
        @Override
        public void onMessage(String channel, String message) {
            RedisSubscriber.this.onMessage(channel, message);
        }
    };
    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                Jedis jedis = SimpleAPI.getInstance().getManager(RedisManager.class).getRedisPool().getResource();
                jedis.subscribe(jedisPubSub, channel);
                jedis.quit();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }, "subscriberThread").start();
    SimpleAPI.getInstance().addOnDisable(() -> jedisPubSub.unsubscribe());
    return jedisPubSub;
}
 
開發者ID:toonsevrin,項目名稱:SimpleAPI,代碼行數:23,代碼來源:RedisSubscriber.java

示例10: run

import redis.clients.jedis.JedisPubSub; //導入依賴的package包/類
@SuppressWarnings("static-access")
@Override
public void run() {
	Thread.currentThread().setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() {
		@Override
		public void uncaughtException(Thread arg0, Throwable arg1) {
			logger.error("[error]{}:{}",slaveRedis.getIp(), slaveRedis.getPort(), arg1);
			Cat.logError(arg1);
			redisStatCheckResult.put(slaveRedis, Boolean.FALSE);
			if (null != slave) {
				slave.close();
			}
		}
	});

	logger.debug("[Psubscribe]{}:{}", slaveRedis.getIp(), slaveRedis.getPort());
	slave.psubscribe(new JedisPubSub() {
		@Override
		public void onPMessage(String pattern, String channel, String msg) {
			logger.debug("[OnPMessage]{}:{}", slaveRedis.getIp(), slaveRedis.getPort());
			redisStatCheckResult.put(slaveRedis, Boolean.TRUE);
		}
	}, generateURL(masterRedis.getIp(), masterRedis.getPort()));
}
 
開發者ID:ctripcorp,項目名稱:x-pipe,代碼行數:25,代碼來源:StandaloneStatMonitor.java

示例11: subscribe

import redis.clients.jedis.JedisPubSub; //導入依賴的package包/類
public void subscribe(final JedisPubSub jedisPubSub, final String... channels) {
    if (cluster) {
        jedisCluster.subscribe(jedisPubSub, channels);
    } else {
        jedisOperator.subscribe(jedisPubSub, channels);
    }
}
 
開發者ID:yrain,項目名稱:smart-cache,代碼行數:8,代碼來源:JedisTemplate.java

示例12: subscribe

import redis.clients.jedis.JedisPubSub; //導入依賴的package包/類
public void subscribe(final JedisPubSub jedisPubSub, final String... channels) {
    execute(new JedisExecutor<Void>() {
        @Override
        Void doInJedis(Jedis jedis) {
            jedis.subscribe(jedisPubSub, channels);
            return null;
        }
    });
}
 
開發者ID:yrain,項目名稱:smart-cache,代碼行數:10,代碼來源:JedisOperator.java

示例13: startSubscribe

import redis.clients.jedis.JedisPubSub; //導入依賴的package包/類
/**
 * <message_type xmlns="message:extension"><type>groupChat</type></message_type>
 */
private void startSubscribe() {
  String type = "pushType";
  String msg = "message";
  String allType = "all";
  String listType = "list";
  getJedis().subscribe(new JedisPubSub() {
    @Override public void onMessage(String channel, String message) {
      super.onMessage(channel, message);
      System.out.println("得到消息為:" + message);
      TigPubsub pubsub = new Gson().fromJson(message,TigPubsub.class);
      try {
        pubSubRepository.insertPubsubMessage(pubsub);
        if (pubsub.getPushType().equals(allType)) {
          List<BareJID> users = userRepository.getUsers();
          System.out.println("打印出所有的用戶" + users.size());
          for (BareJID user : users) {
            Packet push_message = Message.getMessage(JID.jidInstance(
                "[email protected]" + DISCO_DESCRIPTION + "." + "dc-a4b8eb92-xmpp.jiangtao.tech."),
                JID.jidInstance(user.toString()),
                StanzaType.chat, pubsub.getMessage(), null, null, null);
            Element event =
                new Element(MESSAGE_SENDER, new String[] {"xmlns"},
                    new String[] {SENDER_EXTENSION});
            event.addChild(new Element(TYPE, "push"));
            //需要加上sender,發送者是誰
            push_message.getElement().removeChild(event);
            push_message.getElement().addChild(event);
            System.out.println(push_message.toString());
            addOutPacket(push_message);
          }
        }else if (pubsub.getPushType().equals(listType)){
          log.log(Level.SEVERE,"針對單個或者多個用戶,但不是全部用戶....");
        }
      } catch (TigaseDBException | TigaseStringprepException e) {
        e.printStackTrace();
      }
    }
  }, channel);
}
 
開發者ID:BosCattle,項目名稱:PushServer,代碼行數:43,代碼來源:PushServiceComponent.java

示例14: subscribe

import redis.clients.jedis.JedisPubSub; //導入依賴的package包/類
@Override
public void subscribe(JedisPubSub jedisPubSub, String... channels) {
  Span span = helper.buildSpan("subscribe");
  span.setTag("channels", Arrays.toString(channels));
  try {
    super.subscribe(jedisPubSub, channels);
  } catch (Exception e) {
    onError(e, span);
    throw e;
  } finally {
    span.finish();
  }
}
 
開發者ID:opentracing-contrib,項目名稱:java-redis-client,代碼行數:14,代碼來源:TracingJedisCluster.java

示例15: psubscribe

import redis.clients.jedis.JedisPubSub; //導入依賴的package包/類
@Override
public void psubscribe(JedisPubSub jedisPubSub, String... patterns) {
  Span span = helper.buildSpan("psubscribe");
  span.setTag("patterns", Arrays.toString(patterns));
  try {
    super.psubscribe(jedisPubSub, patterns);
  } catch (Exception e) {
    onError(e, span);
    throw e;
  } finally {
    span.finish();
  }
}
 
開發者ID:opentracing-contrib,項目名稱:java-redis-client,代碼行數:14,代碼來源:TracingJedisCluster.java


注:本文中的redis.clients.jedis.JedisPubSub類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。