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

The following examples show how to use org.springframework.data.redis.listener.ChannelTopic. These examples are extracted from open source projects. 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 Project: rqueue   Source File: DelayedMessageSchedulerTest.java    License: 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
@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 3
Source Project: redisson   Source File: RedissonSubscribeReactiveTest.java    License: 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 Project: redisson   Source File: RedissonSubscribeReactiveTest.java    License: 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 Project: redisson   Source File: RedissonSubscribeReactiveTest.java    License: 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
@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 7
Source Project: rqueue   Source File: MessageScheduler.java    License: 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 8
/**
 * 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 9
Source Project: tac   Source File: RedisConfig.java    License: 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
@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 Project: JetfireCloud   Source File: RedisSender.java    License: 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
@Bean
ChannelTopic channelTopic() {
  if (LOG.isDebugEnabled()) {
    LOG.debug("build channel topic = [{}]", topic);
  }
  return new ChannelTopic(topic);
}
 
Example 13
Source Project: SpringCloud   Source File: RedisSender.java    License: 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
@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 15
@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 16
Source Project: spring-boot-demo   Source File: RedisMessagePublisher.java    License: MIT License 4 votes vote down vote up
public RedisMessagePublisher(
        RedisTemplate<String, Object> redisTemplate, ChannelTopic topic) {
    this.redisTemplate = redisTemplate;
    this.topic = topic;
}
 
Example 17
Source Project: spring-boot-demo   Source File: RedisConfig.java    License: MIT License 4 votes vote down vote up
@Bean
ChannelTopic topic() {
    return new ChannelTopic("messageQueue");
}
 
Example 18
Source Project: alcor   Source File: RedisConfiguration.java    License: Apache License 2.0 4 votes vote down vote up
@Bean
ChannelTopic topic() {
    return new ChannelTopic("pubsub:queue");
}
 
Example 19
Source Project: alcor   Source File: RedisPublisher.java    License: 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 20
Source Project: alcor   Source File: RedisPublisher.java    License: 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 21
Source Project: alcor   Source File: RedisPublisher.java    License: 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 22
Source Project: alcor   Source File: RedisPublisherServiceImpl.java    License: 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 23
Source Project: servicecomb-pack   Source File: RedisMessagePublisher.java    License: 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 24
Source Project: DataM   Source File: RedisConfig.java    License: Apache License 2.0 4 votes vote down vote up
@Bean
ChannelTopic topic() {
    return new ChannelTopic(topic);
}
 
Example 25
Source Project: DataM   Source File: RedisInfoPublisher.java    License: Apache License 2.0 4 votes vote down vote up
public RedisInfoPublisher(StringRedisTemplate redisTemplate, ChannelTopic topic) {
    this.redisTemplate = redisTemplate;
    this.topic = topic;
}
 
Example 26
Source Project: DataM   Source File: RedisConfig.java    License: Apache License 2.0 4 votes vote down vote up
@Bean
ChannelTopic topic() {
    return new ChannelTopic(topic);
}
 
Example 27
Source Project: tutorials   Source File: RedisConfig.java    License: MIT License 4 votes vote down vote up
@Bean
ChannelTopic topic() {
    return new ChannelTopic("pubsub:queue");
}
 
Example 28
Source Project: tutorials   Source File: RedisMessagePublisher.java    License: 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 29
Source Project: tac   Source File: RedisMsSubscriber.java    License: MIT License 3 votes vote down vote up
@Override
public void subscribe() {

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

    this.loadAllMsCode();

}