org.springframework.data.redis.connection.MessageListener Java Examples

The following examples show how to use org.springframework.data.redis.connection.MessageListener. 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: RedissonSubscribeTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testSubscribe() {
    RedissonConnection connection = new RedissonConnection(redisson);
    AtomicReference<byte[]> msg = new AtomicReference<byte[]>();
    connection.subscribe(new MessageListener() {
        @Override
        public void onMessage(Message message, byte[] pattern) {
            msg.set(message.getBody());
        }
    }, "test".getBytes());
    
    connection.publish("test".getBytes(), "msg".getBytes());
    Awaitility.await().atMost(Duration.ONE_SECOND)
                .until(() -> Arrays.equals("msg".getBytes(), msg.get()));
    
    connection.getSubscription().unsubscribe();
    
    connection.publish("test".getBytes(), "msg".getBytes());
}
 
Example #2
Source File: RedissonSubscribeTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testUnSubscribe() {
    RedissonConnection connection = new RedissonConnection(redisson);
    AtomicReference<byte[]> msg = new AtomicReference<byte[]>();
    connection.subscribe(new MessageListener() {
        @Override
        public void onMessage(Message message, byte[] pattern) {
            msg.set(message.getBody());
        }
    }, "test".getBytes());
    
    connection.publish("test".getBytes(), "msg".getBytes());
    Awaitility.await().atMost(Duration.ONE_SECOND)
                .until(() -> Arrays.equals("msg".getBytes(), msg.get()));
    
    connection.getSubscription().unsubscribe();
    
    
}
 
Example #3
Source File: RedissonSubscribeTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testSubscribe() {
    RedissonConnection connection = new RedissonConnection(redisson);
    AtomicReference<byte[]> msg = new AtomicReference<byte[]>();
    connection.subscribe(new MessageListener() {
        @Override
        public void onMessage(Message message, byte[] pattern) {
            msg.set(message.getBody());
        }
    }, "test".getBytes());
    
    connection.publish("test".getBytes(), "msg".getBytes());
    Awaitility.await().atMost(Duration.ONE_SECOND)
                .until(() -> Arrays.equals("msg".getBytes(), msg.get()));
    
    connection.getSubscription().unsubscribe();
    
    connection.publish("test".getBytes(), "msg".getBytes());
}
 
Example #4
Source File: RedissonSubscribeTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testUnSubscribe() {
    RedissonConnection connection = new RedissonConnection(redisson);
    AtomicReference<byte[]> msg = new AtomicReference<byte[]>();
    connection.subscribe(new MessageListener() {
        @Override
        public void onMessage(Message message, byte[] pattern) {
            msg.set(message.getBody());
        }
    }, "test".getBytes());
    
    connection.publish("test".getBytes(), "msg".getBytes());
    Awaitility.await().atMost(Duration.ONE_SECOND)
                .until(() -> Arrays.equals("msg".getBytes(), msg.get()));
    
    connection.getSubscription().unsubscribe();
    
    
}
 
Example #5
Source File: RedissonSubscribeTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testSubscribe() {
    RedissonConnection connection = new RedissonConnection(redisson);
    AtomicReference<byte[]> msg = new AtomicReference<byte[]>();
    connection.subscribe(new MessageListener() {
        @Override
        public void onMessage(Message message, byte[] pattern) {
            msg.set(message.getBody());
        }
    }, "test".getBytes());
    
    connection.publish("test".getBytes(), "msg".getBytes());
    Awaitility.await().atMost(Duration.ONE_SECOND)
                .until(() -> Arrays.equals("msg".getBytes(), msg.get()));
    
    connection.getSubscription().unsubscribe();
    
    connection.publish("test".getBytes(), "msg".getBytes());
}
 
Example #6
Source File: RedissonSubscribeTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testUnSubscribe() {
    RedissonConnection connection = new RedissonConnection(redisson);
    AtomicReference<byte[]> msg = new AtomicReference<byte[]>();
    connection.subscribe(new MessageListener() {
        @Override
        public void onMessage(Message message, byte[] pattern) {
            msg.set(message.getBody());
        }
    }, "test".getBytes());
    
    connection.publish("test".getBytes(), "msg".getBytes());
    Awaitility.await().atMost(Duration.ONE_SECOND)
                .until(() -> Arrays.equals("msg".getBytes(), msg.get()));
    
    connection.getSubscription().unsubscribe();
    
    
}
 
Example #7
Source File: RedissonSubscribeTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testSubscribe() {
    RedissonConnection connection = new RedissonConnection(redisson);
    AtomicReference<byte[]> msg = new AtomicReference<byte[]>();
    connection.subscribe(new MessageListener() {
        @Override
        public void onMessage(Message message, byte[] pattern) {
            msg.set(message.getBody());
        }
    }, "test".getBytes());
    
    connection.publish("test".getBytes(), "msg".getBytes());
    Awaitility.await().atMost(Duration.ONE_SECOND)
                .until(() -> Arrays.equals("msg".getBytes(), msg.get()));
    
    connection.getSubscription().unsubscribe();
    
    connection.publish("test".getBytes(), "msg".getBytes());
}
 
Example #8
Source File: RedissonSubscribeTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testUnSubscribe() {
    RedissonConnection connection = new RedissonConnection(redisson);
    AtomicReference<byte[]> msg = new AtomicReference<byte[]>();
    connection.subscribe(new MessageListener() {
        @Override
        public void onMessage(Message message, byte[] pattern) {
            msg.set(message.getBody());
        }
    }, "test".getBytes());
    
    connection.publish("test".getBytes(), "msg".getBytes());
    Awaitility.await().atMost(Duration.ONE_SECOND)
                .until(() -> Arrays.equals("msg".getBytes(), msg.get()));
    
    connection.getSubscription().unsubscribe();
    
    
}
 
Example #9
Source File: RedissonSubscribeTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testSubscribe() {
    RedissonConnection connection = new RedissonConnection(redisson);
    AtomicReference<byte[]> msg = new AtomicReference<byte[]>();
    connection.subscribe(new MessageListener() {
        @Override
        public void onMessage(Message message, byte[] pattern) {
            msg.set(message.getBody());
        }
    }, "test".getBytes());
    
    connection.publish("test".getBytes(), "msg".getBytes());
    Awaitility.await().atMost(Duration.ONE_SECOND)
                .until(() -> Arrays.equals("msg".getBytes(), msg.get()));
    
    connection.getSubscription().unsubscribe();
    
    connection.publish("test".getBytes(), "msg".getBytes());
}
 
Example #10
Source File: RedissonSubscribeTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testUnSubscribe() {
    RedissonConnection connection = new RedissonConnection(redisson);
    AtomicReference<byte[]> msg = new AtomicReference<byte[]>();
    connection.subscribe(new MessageListener() {
        @Override
        public void onMessage(Message message, byte[] pattern) {
            msg.set(message.getBody());
        }
    }, "test".getBytes());
    
    connection.publish("test".getBytes(), "msg".getBytes());
    Awaitility.await().atMost(Duration.ONE_SECOND)
                .until(() -> Arrays.equals("msg".getBytes(), msg.get()));
    
    connection.getSubscription().unsubscribe();
    
    
}
 
Example #11
Source File: RedissonSubscribeTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testUnSubscribe() {
    RedissonConnection connection = new RedissonConnection(redisson);
    AtomicReference<byte[]> msg = new AtomicReference<byte[]>();
    connection.subscribe(new MessageListener() {
        @Override
        public void onMessage(Message message, byte[] pattern) {
            msg.set(message.getBody());
        }
    }, "test".getBytes());
    
    connection.publish("test".getBytes(), "msg".getBytes());
    Awaitility.await().atMost(Duration.ONE_SECOND)
                .until(() -> Arrays.equals("msg".getBytes(), msg.get()));
    
    connection.getSubscription().unsubscribe();
    
    
}
 
Example #12
Source File: RedissonSubscribeTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testSubscribe() {
    RedissonConnection connection = new RedissonConnection(redisson);
    AtomicReference<byte[]> msg = new AtomicReference<byte[]>();
    connection.subscribe(new MessageListener() {
        @Override
        public void onMessage(Message message, byte[] pattern) {
            System.out.println("message " + new String(message.getBody()));
            msg.set(message.getBody());
        }
    }, "test".getBytes());
    
    connection.publish("test".getBytes(), "msg".getBytes());
    Awaitility.await().atMost(Duration.ONE_SECOND)
                .until(() -> Arrays.equals("msg".getBytes(), msg.get()));
    
    connection.getSubscription().unsubscribe();
    
    connection.publish("test".getBytes(), "msg".getBytes());
}
 
Example #13
Source File: RedissonSubscribeTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testUnSubscribe() {
    RedissonConnection connection = new RedissonConnection(redisson);
    AtomicReference<byte[]> msg = new AtomicReference<byte[]>();
    connection.subscribe(new MessageListener() {
        @Override
        public void onMessage(Message message, byte[] pattern) {
            msg.set(message.getBody());
        }
    }, "test".getBytes());
    
    connection.publish("test".getBytes(), "msg".getBytes());
    Awaitility.await().atMost(Duration.ONE_SECOND)
                .until(() -> Arrays.equals("msg".getBytes(), msg.get()));
    
    connection.getSubscription().unsubscribe();
    
    
}
 
Example #14
Source File: CacheConfig.java    From springboot-plus with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Bean
MessageListenerAdapter listenerAdapter(final TowLevelCacheManager cacheManager) {
	return new MessageListenerAdapter(new MessageListener() {

		public void onMessage(Message message, byte[] pattern) {
			byte[] bs = message.getChannel();
			try {
				// Sub 一个消息,通知缓存管理器
				String type = new String(bs, "UTF-8");
				cacheManager.receiver(type);
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
				// 不可能出错,忽略
			}

		}

	});
}
 
Example #15
Source File: RedissonSubscribeTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testSubscribe() {
    RedissonConnection connection = new RedissonConnection(redisson);
    AtomicReference<byte[]> msg = new AtomicReference<byte[]>();
    connection.subscribe(new MessageListener() {
        @Override
        public void onMessage(Message message, byte[] pattern) {
            msg.set(message.getBody());
        }
    }, "test".getBytes());
    
    connection.publish("test".getBytes(), "msg".getBytes());
    Awaitility.await().atMost(Duration.ONE_SECOND)
                .until(() -> Arrays.equals("msg".getBytes(), msg.get()));
    
    connection.getSubscription().unsubscribe();
    
    connection.publish("test".getBytes(), "msg".getBytes());
}
 
Example #16
Source File: TokenRepositoryImpl.java    From authlib-agent with MIT License 5 votes vote down vote up
@PostConstruct
private void registerExpiredEventListener() {
	expiredListener = new MessageListener() {

		@Override
		public void onMessage(Message message, byte[] pattern) {
			byte[] body = message.getBody();
			if (body == null) {
				return;
			}

			String key;
			try {
				key = new String(message.getBody(), "UTF-8");
			} catch (UnsupportedEncodingException e) {
				LOGGER.debug(() -> "failed to decode message body: " + bytesToHex(body), e);
				return;
			}

			if (!key.startsWith(PREFIX_EXPIRE)) {
				return;
			}

			String accessToken = key.substring(PREFIX_EXPIRE.length());
			template.delete(keyAccessToken(accessToken));
			Map<String, String> values = hashOps.entries(keyAccessToken(accessToken));
			if (values != null && !values.isEmpty()) {
				setOps.remove(keyClientToken(values.get(KEY_CLIENT_TOKEN)), accessToken);
				setOps.remove(keyAccount(values.get(KEY_OWNER)), accessToken);
			}
		}
	};
	container.addMessageListener(expiredListener, new PatternTopic("__keyevent@*__:expired"));
}
 
Example #17
Source File: RedissonConnection.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Override
public void pSubscribe(MessageListener listener, byte[]... patterns) {
    checkSubscription();
    
    subscription = new RedissonSubscription(redisson.getConnectionManager(), redisson.getConnectionManager().getSubscribeService(), listener);
    subscription.pSubscribe(patterns);
}
 
Example #18
Source File: RedissonConnection.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Override
public void subscribe(MessageListener listener, byte[]... channels) {
    checkSubscription();
    
    subscription = new RedissonSubscription(redisson.getConnectionManager(), redisson.getConnectionManager().getSubscribeService(), listener);
    subscription.subscribe(channels);
}
 
Example #19
Source File: RedissonConnection.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Override
public void pSubscribe(MessageListener listener, byte[]... patterns) {
    checkSubscription();
    
    subscription = new RedissonSubscription(redisson.getConnectionManager(), redisson.getConnectionManager().getSubscribeService(), listener);
    subscription.pSubscribe(patterns);
}
 
Example #20
Source File: RedissonConnection.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Override
public void subscribe(MessageListener listener, byte[]... channels) {
    checkSubscription();
    
    subscription = new RedissonSubscription(redisson.getConnectionManager(), redisson.getConnectionManager().getSubscribeService(), listener);
    subscription.subscribe(channels);
}
 
Example #21
Source File: DelayedMessageSchedulerTest.java    From rqueue with Apache License 2.0 5 votes vote down vote up
@Test
public void onMessageListenerTest() throws Exception {
  doReturn(5000L).when(rqueueSchedulerConfig).getDelayedMessageTimeInterval();
  doReturn(1).when(rqueueSchedulerConfig).getDelayedMessageThreadPoolSize();
  doReturn(true).when(rqueueSchedulerConfig).isAutoStart();
  doReturn(true).when(rqueueSchedulerConfig).isRedisEnabled();
  doReturn(redisMessageListenerContainer)
      .when(rqueueRedisListenerContainerFactory)
      .getContainer();
  messageScheduler.onApplicationEvent(new RqueueBootstrapEvent("Test", true));

  MessageListener messageListener =
      (MessageListener) FieldUtils.readField(messageScheduler, "messageSchedulerListener", true);
  // invalid channel
  messageListener.onMessage(new DefaultMessage(slowQueue.getBytes(), "312".getBytes()), null);
  Thread.sleep(50);
  assertEquals(2, messageScheduler.scheduleList.stream().filter(e -> !e).count());

  // invalid body
  messageListener.onMessage(
      new DefaultMessage(
          slowQueueDetail.getDelayedQueueChannelName().getBytes(), "sss".getBytes()),
      null);
  Thread.sleep(50);
  assertEquals(2, messageScheduler.scheduleList.stream().filter(e -> !e).count());

  // both are correct
  messageListener.onMessage(
      new DefaultMessage(
          slowQueueDetail.getDelayedQueueChannelName().getBytes(),
          String.valueOf(System.currentTimeMillis()).getBytes()),
      null);
  Thread.sleep(50);
  assertEquals(3, messageScheduler.scheduleList.stream().filter(e -> !e).count());
  messageScheduler.destroy();
}
 
Example #22
Source File: RedissonConnection.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Override
public void pSubscribe(MessageListener listener, byte[]... patterns) {
    checkSubscription();
    
    subscription = new RedissonSubscription(redisson.getConnectionManager(), redisson.getConnectionManager().getSubscribeService(), listener);
    subscription.pSubscribe(patterns);
}
 
Example #23
Source File: RedissonConnection.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Override
public void subscribe(MessageListener listener, byte[]... channels) {
    checkSubscription();
    
    subscription = new RedissonSubscription(redisson.getConnectionManager(), redisson.getConnectionManager().getSubscribeService(), listener);
    subscription.subscribe(channels);
}
 
Example #24
Source File: RedissonSubscription.java    From redisson with Apache License 2.0 4 votes vote down vote up
public RedissonSubscription(ConnectionManager connectionManager, PublishSubscribeService subscribeService, MessageListener listener) {
    super(listener, null, null);
    this.connectionManager = connectionManager;
    this.subscribeService = subscribeService;
}
 
Example #25
Source File: RedissonSubscription.java    From redisson with Apache License 2.0 4 votes vote down vote up
public RedissonSubscription(ConnectionManager connectionManager, PublishSubscribeService subscribeService, MessageListener listener) {
    super(listener, null, null);
    this.connectionManager = connectionManager;
    this.subscribeService = subscribeService;
}
 
Example #26
Source File: RedissonSubscription.java    From redisson with Apache License 2.0 4 votes vote down vote up
public RedissonSubscription(ConnectionManager connectionManager, PublishSubscribeService subscribeService, MessageListener listener) {
    super(listener, null, null);
    this.connectionManager = connectionManager;
    this.subscribeService = subscribeService;
}
 
Example #27
Source File: RedissonSubscription.java    From redisson with Apache License 2.0 4 votes vote down vote up
public RedissonSubscription(ConnectionManager connectionManager, PublishSubscribeService subscribeService, MessageListener listener) {
    super(listener, null, null);
    this.connectionManager = connectionManager;
    this.subscribeService = subscribeService;
}
 
Example #28
Source File: TracingRedisConnection.java    From java-redis-client with Apache License 2.0 4 votes vote down vote up
@Override
public void pSubscribe(MessageListener listener, byte[]... patterns) {
  helper.doInScope(RedisCommand.PSUBSCRIBE, () -> connection.pSubscribe(listener, patterns));
}
 
Example #29
Source File: TracingRedisConnection.java    From java-redis-client with Apache License 2.0 4 votes vote down vote up
@Override
public void subscribe(MessageListener listener, byte[]... channels) {
  helper.doInScope(RedisCommand.SUBSCRIBE, () -> connection.subscribe(listener, channels));
}
 
Example #30
Source File: RedissonSubscription.java    From redisson with Apache License 2.0 4 votes vote down vote up
public RedissonSubscription(ConnectionManager connectionManager, PublishSubscribeService subscribeService, MessageListener listener) {
    super(listener, null, null);
    this.connectionManager = connectionManager;
    this.subscribeService = subscribeService;
}