org.springframework.data.redis.listener.ChannelTopic Java Examples

The following examples show how to use org.springframework.data.redis.listener.ChannelTopic. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DelayedMessageSchedulerTest.java    From rqueue with Apache License 2.0 6 votes vote down vote up
@Test
public void startAddsChannelToMessageListener() throws Exception {
  doReturn(1000L).when(rqueueSchedulerConfig).getDelayedMessageTimeInterval();
  doReturn(1).when(rqueueSchedulerConfig).getDelayedMessageThreadPoolSize();
  doReturn(true).when(rqueueSchedulerConfig).isAutoStart();
  doReturn(true).when(rqueueSchedulerConfig).isRedisEnabled();
  doReturn(redisMessageListenerContainer)
      .when(rqueueRedisListenerContainerFactory)
      .getContainer();
  doNothing()
      .when(redisMessageListenerContainer)
      .addMessageListener(
          any(), eq(new ChannelTopic(slowQueueDetail.getDelayedQueueChannelName())));
  doNothing()
      .when(redisMessageListenerContainer)
      .addMessageListener(
          any(), eq(new ChannelTopic(fastQueueDetail.getDelayedQueueChannelName())));
  messageScheduler.onApplicationEvent(new RqueueBootstrapEvent("Test", true));
  Thread.sleep(500L);
  messageScheduler.destroy();
}
 
Example #2
Source File: RedisHttpSessionConfiguration.java    From spring-session with Apache License 2.0 6 votes vote down vote up
@Bean
public RedisMessageListenerContainer springSessionRedisMessageListenerContainer(
		RedisIndexedSessionRepository sessionRepository) {
	RedisMessageListenerContainer container = new RedisMessageListenerContainer();
	container.setConnectionFactory(this.redisConnectionFactory);
	if (this.redisTaskExecutor != null) {
		container.setTaskExecutor(this.redisTaskExecutor);
	}
	if (this.redisSubscriptionExecutor != null) {
		container.setSubscriptionExecutor(this.redisSubscriptionExecutor);
	}
	container.addMessageListener(sessionRepository,
			Arrays.asList(new ChannelTopic(sessionRepository.getSessionDeletedChannel()),
					new ChannelTopic(sessionRepository.getSessionExpiredChannel())));
	container.addMessageListener(sessionRepository,
			Collections.singletonList(new PatternTopic(sessionRepository.getSessionCreatedChannelPrefix() + "*")));
	return container;
}
 
Example #3
Source File: RedissonSubscribeReactiveTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testTemplate() {
    RedissonConnectionFactory factory = new RedissonConnectionFactory(redisson);
    AtomicLong counter = new AtomicLong();

    ReactiveStringRedisTemplate template = new ReactiveStringRedisTemplate(factory);
    template.listenTo(ChannelTopic.of("test")).flatMap(message -> {
        counter.incrementAndGet();
        return Mono.empty();
    }).subscribe();

    template.listenTo(ChannelTopic.of("test2")).flatMap(message -> {
        counter.incrementAndGet();
        return Mono.empty();
    }).subscribe();

    ReactiveRedisConnection connection = factory.getReactiveConnection();
    connection.pubSubCommands().publish(ByteBuffer.wrap("test".getBytes()), ByteBuffer.wrap("msg".getBytes())).block();

    Awaitility.await().atMost(Duration.ONE_SECOND)
                .until(() -> counter.get() == 1);
}
 
Example #4
Source File: RedissonSubscribeReactiveTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testTemplate() {
    RedissonConnectionFactory factory = new RedissonConnectionFactory(redisson);
    AtomicReference<byte[]> msg = new AtomicReference<byte[]>();

    ReactiveStringRedisTemplate template = new ReactiveStringRedisTemplate(factory);
    template.listenTo(ChannelTopic.of("test")).flatMap(message -> {
        msg.set(message.getMessage().getBytes());
        return template.delete("myobj");
    }).subscribe();

    ReactiveRedisConnection connection = factory.getReactiveConnection();
    connection.pubSubCommands().publish(ByteBuffer.wrap("test".getBytes()), ByteBuffer.wrap("msg".getBytes())).block();

    Awaitility.await().atMost(Duration.ONE_SECOND)
                .until(() -> Arrays.equals("msg".getBytes(), msg.get()));
}
 
Example #5
Source File: RedissonSubscribeReactiveTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testTemplate() {
    RedissonConnectionFactory factory = new RedissonConnectionFactory(redisson);
    AtomicLong counter = new AtomicLong();

    ReactiveStringRedisTemplate template = new ReactiveStringRedisTemplate(factory);
    template.listenTo(ChannelTopic.of("test")).flatMap(message -> {
        counter.incrementAndGet();
        return Mono.empty();
    }).subscribe();

    template.listenTo(ChannelTopic.of("test2")).flatMap(message -> {
        counter.incrementAndGet();
        return Mono.empty();
    }).subscribe();

    ReactiveRedisConnection connection = factory.getReactiveConnection();
    connection.pubSubCommands().publish(ByteBuffer.wrap("test".getBytes()), ByteBuffer.wrap("msg".getBytes())).block();

    Awaitility.await().atMost(Duration.ONE_SECOND)
                .until(() -> counter.get() == 1);
}
 
Example #6
Source File: RedisConfig.java    From spring-reactive-sample with GNU General Public License v3.0 6 votes vote down vote up
@Bean
public ReactiveRedisMessageListenerContainer redisMessageListenerContainer(PostRepository posts, ReactiveRedisConnectionFactory connectionFactory) {
    ReactiveRedisMessageListenerContainer container = new ReactiveRedisMessageListenerContainer(connectionFactory);
    ObjectMapper objectMapper = new ObjectMapper();
    container.receive(ChannelTopic.of("posts"))
        .map(p->p.getMessage())
        .map(m -> {
            try {
                Post post= objectMapper.readValue(m, Post.class);
                post.setId(UUID.randomUUID().toString());
                return post;
            } catch (IOException e) {
                return null;
            }
        })
        .switchIfEmpty(Mono.error(new IllegalArgumentException()))
        .flatMap(p-> posts.save(p))
        .subscribe(c-> log.info(" count:" + c), null , () -> log.info("saving post."));
    return container;
}
 
Example #7
Source File: CasbinRedisWatcherAutoConfiguration.java    From casbin-spring-boot-starter with Apache License 2.0 5 votes vote down vote up
/**
 * redis消息监听器容器
 * 可以添加多个监听不同话题的redis监听器,只需要把消息监听器和相应的消息订阅处理器绑定,该消息监听器
 * 通过反射技术调用消息订阅处理器的相关方法进行一些业务处理
 */
@Bean
@ConditionalOnMissingBean
@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
public RedisMessageListenerContainer redisMessageListenerContainer(
        RedisConnectionFactory connectionFactory,
        MessageListenerAdapter listenerAdapter
) {
    RedisMessageListenerContainer container = new RedisMessageListenerContainer();
    container.setConnectionFactory(connectionFactory);
    //订阅CASBIN_POLICY_TOPIC通道
    container.addMessageListener(listenerAdapter, new ChannelTopic(CASBIN_POLICY_TOPIC));
    return container;
}
 
Example #8
Source File: MessageScheduler.java    From rqueue with Apache License 2.0 5 votes vote down vote up
private void subscribeToRedisTopic(String queueName) {
  if (isRedisEnabled()) {
    String channelName = getChannelName(queueName);
    getLogger().debug("Queue {} subscribe to channel {}", queueName, channelName);
    this.rqueueRedisListenerContainerFactory
        .getContainer()
        .addMessageListener(messageSchedulerListener, new ChannelTopic(channelName));
    channelNameToQueueName.put(channelName, queueName);
  }
}
 
Example #9
Source File: RedisConfig.java    From tac with MIT License 5 votes vote down vote up
@Bean
public RedisMessageListenerContainer redisMessageListenerContainer(JedisConnectionFactory jedisConnectionFactory,
                                                                   MessageListenerAdapter listenerAdapter) {

    RedisMessageListenerContainer container = new RedisMessageListenerContainer();
    container.setConnectionFactory(jedisConnectionFactory);
    container.addMessageListener(listenerAdapter, new ChannelTopic("topicA"));
    // 设置线程池
    //container.setTaskExecutor(null);
    return container;
}
 
Example #10
Source File: RedisListenerProcessor.java    From faster-framework-project with Apache License 2.0 5 votes vote down vote up
@Override
public Object postProcessAfterInitialization(Object bean, final String beanName) {
    Class<?> targetClass = AopUtils.getTargetClass(bean);
    Map<Method, RedisListener> annotatedMethods = MethodIntrospector.selectMethods(targetClass,
            (MethodIntrospector.MetadataLookup<RedisListener>) method -> AnnotatedElementUtils.findMergedAnnotation(method, RedisListener.class));
    annotatedMethods.forEach((method, v) -> {
        MessageListenerAdapter messageListenerAdapter = new MessageListenerAdapter(bean, method.getName());
        messageListenerAdapter.afterPropertiesSet();
        String[] channels = v.value();
        for (String channel : channels) {
            redisMessageListenerContainer.addMessageListener(messageListenerAdapter, channel.contains("*") ? new PatternTopic(channel) : new ChannelTopic(channel));
        }
    });
    return bean;
}
 
Example #11
Source File: RedisSender.java    From JetfireCloud with Apache License 2.0 5 votes vote down vote up
@Bean
RedisMessageListenerContainer redisContainer(RedisConnectionFactory connectionFactory, MessageListenerAdapter listenerAdapter) {
    RedisMessageListenerContainer container = new RedisMessageListenerContainer();
    container.setConnectionFactory(connectionFactory);
    container.addMessageListener(listenerAdapter, new ChannelTopic("chat"));
    logger.info("init container:{}", listenerAdapter);
    return container;
}
 
Example #12
Source File: RedisChannelAutoConfiguration.java    From servicecomb-pack with Apache License 2.0 5 votes vote down vote up
@Bean
ChannelTopic channelTopic() {
  if (LOG.isDebugEnabled()) {
    LOG.debug("build channel topic = [{}]", topic);
  }
  return new ChannelTopic(topic);
}
 
Example #13
Source File: RedisSender.java    From SpringCloud with Apache License 2.0 5 votes vote down vote up
@Bean
RedisMessageListenerContainer redisContainer(RedisConnectionFactory connectionFactory, MessageListenerAdapter listenerAdapter) {
    RedisMessageListenerContainer container = new RedisMessageListenerContainer();
    container.setConnectionFactory(connectionFactory);
    container.addMessageListener(listenerAdapter, new ChannelTopic("chat"));
    logger.info("init container:{}", listenerAdapter);
    return container;
}
 
Example #14
Source File: RedisListenerConfigure.java    From spring-boot-tutorials with Apache License 2.0 5 votes vote down vote up
@Bean
public RedisMessageListenerContainer simpleListenerContainer(RedisConnectionFactory redisConnectionFactory){
    RedisMessageListenerContainer container = new RedisMessageListenerContainer();
    container.setConnectionFactory(redisConnectionFactory);
    List<ChannelTopic> topics = Arrays.asList(new ChannelTopic(PubSubConstant.NEWS_CHANNEL));
    container.addMessageListener(simpleMessageListenerAdapter(), topics);
    return container;
}
 
Example #15
Source File: RedisListenerConfigure.java    From spring-boot-tutorials with Apache License 2.0 5 votes vote down vote up
@Bean
public RedisMessageListenerContainer objectListenerContainer(RedisConnectionFactory redisConnectionFactory){
    RedisMessageListenerContainer container = new RedisMessageListenerContainer();
    container.setConnectionFactory(redisConnectionFactory);
    List<ChannelTopic> topics = Arrays.asList(new ChannelTopic(PubSubConstant.CHAT_ROOM));
    container.addMessageListener(objectMessageListenerAdapter(), topics);
    return container;
}
 
Example #16
Source File: RedisMessagePublisher.java    From tutorials with MIT License 4 votes vote down vote up
public RedisMessagePublisher(final RedisTemplate<String, Object> redisTemplate, final ChannelTopic topic) {
    this.redisTemplate = redisTemplate;
    this.topic = topic;
}
 
Example #17
Source File: RedisPublisher.java    From alcor with Apache License 2.0 4 votes vote down vote up
public RedisPublisher(final RedisTemplate<String, VpcEntity> redisTemplate, final ChannelTopic topic) {
    this.redisTemplate = redisTemplate;
    this.topic = topic;
}
 
Example #18
Source File: RedisConfig.java    From tutorials with MIT License 4 votes vote down vote up
@Bean
ChannelTopic topic() {
    return new ChannelTopic("pubsub:queue");
}
 
Example #19
Source File: RedisMessagePublisher.java    From spring-boot-demo with MIT License 4 votes vote down vote up
public RedisMessagePublisher(
        RedisTemplate<String, Object> redisTemplate, ChannelTopic topic) {
    this.redisTemplate = redisTemplate;
    this.topic = topic;
}
 
Example #20
Source File: RedisConfig.java    From spring-boot-demo with MIT License 4 votes vote down vote up
@Bean
ChannelTopic topic() {
    return new ChannelTopic("messageQueue");
}
 
Example #21
Source File: RedisConfiguration.java    From alcor with Apache License 2.0 4 votes vote down vote up
@Bean
ChannelTopic topic() {
    return new ChannelTopic("pubsub:queue");
}
 
Example #22
Source File: RedisConfig.java    From DataM with Apache License 2.0 4 votes vote down vote up
@Bean
ChannelTopic topic() {
    return new ChannelTopic(topic);
}
 
Example #23
Source File: RedisInfoPublisher.java    From DataM with Apache License 2.0 4 votes vote down vote up
public RedisInfoPublisher(StringRedisTemplate redisTemplate, ChannelTopic topic) {
    this.redisTemplate = redisTemplate;
    this.topic = topic;
}
 
Example #24
Source File: RedisConfig.java    From DataM with Apache License 2.0 4 votes vote down vote up
@Bean
ChannelTopic topic() {
    return new ChannelTopic(topic);
}
 
Example #25
Source File: RedisMessagePublisher.java    From servicecomb-pack with Apache License 2.0 4 votes vote down vote up
public RedisMessagePublisher(RedisTemplate<String, Object> redisTemplate,
    ChannelTopic channelTopic) {
  this.redisTemplate = redisTemplate;
  this.channelTopic = channelTopic;
}
 
Example #26
Source File: RedisPublisher.java    From alcor with Apache License 2.0 4 votes vote down vote up
public RedisPublisher(final RedisTemplate<String, VpcState> redisTemplate, final ChannelTopic topic) {
    this.redisTemplate = redisTemplate;
    this.topic = topic;
}
 
Example #27
Source File: RedisPublisherServiceImpl.java    From alcor with Apache License 2.0 4 votes vote down vote up
public RedisPublisherServiceImpl(final RedisTemplate<String, SubnetEntity> redisTemplate, final ChannelTopic topic) {
    this.redisTemplate = redisTemplate;
    this.topic = topic;
}
 
Example #28
Source File: RedisPublisher.java    From alcor with Apache License 2.0 4 votes vote down vote up
public RedisPublisher(final RedisTemplate<String, RouteEntity> redisTemplate, final ChannelTopic topic) {
    this.redisTemplate = redisTemplate;
    this.topic = topic;
}
 
Example #29
Source File: RedisMsSubscriber.java    From tac with MIT License 3 votes vote down vote up
@Override
public void subscribe() {

    container.addMessageListener(messageListenerAdapter,
        new ChannelTopic(tacRedisConfigProperties.getPublishEventChannel()));

    this.loadAllMsCode();

}