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

The following examples show how to use org.springframework.data.redis.connection.MessageListener. 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
@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 2
Source Project: redisson   Source File: RedissonSubscribeTest.java    License: 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 3
Source Project: redisson   Source File: RedissonSubscribeTest.java    License: 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 4
Source Project: redisson   Source File: RedissonSubscribeTest.java    License: 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 5
Source Project: redisson   Source File: RedissonSubscribeTest.java    License: 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 6
Source Project: redisson   Source File: RedissonSubscribeTest.java    License: 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 7
Source Project: redisson   Source File: RedissonSubscribeTest.java    License: 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 8
Source Project: redisson   Source File: RedissonSubscribeTest.java    License: 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 9
Source Project: redisson   Source File: RedissonSubscribeTest.java    License: 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 10
Source Project: redisson   Source File: RedissonSubscribeTest.java    License: 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 11
Source Project: redisson   Source File: RedissonSubscribeTest.java    License: 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 Project: redisson   Source File: RedissonSubscribeTest.java    License: 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 13
Source Project: redisson   Source File: RedissonSubscribeTest.java    License: 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 Project: redisson   Source File: RedissonSubscribeTest.java    License: 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 15
Source Project: redisson   Source File: RedissonSubscribeTest.java    License: 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 16
Source Project: rqueue   Source File: DelayedMessageSchedulerTest.java    License: 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 17
Source Project: authlib-agent   Source File: TokenRepositoryImpl.java    License: 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("[email protected]*__:expired"));
}
 
Example 18
Source Project: redisson   Source File: RedissonConnection.java    License: 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 Project: redisson   Source File: RedissonConnection.java    License: 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 Project: redisson   Source File: RedissonConnection.java    License: 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 Project: redisson   Source File: RedissonConnection.java    License: 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 22
Source Project: redisson   Source File: RedissonConnection.java    License: 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 23
Source Project: redisson   Source File: RedissonConnection.java    License: 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 24
Source Project: java-redis-client   Source File: TracingRedisConnection.java    License: 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 25
Source Project: java-redis-client   Source File: TracingRedisConnection.java    License: 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 26
Source Project: java-redis-client   Source File: TracingRedisConnection.java    License: 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 27
Source Project: java-redis-client   Source File: TracingRedisConnection.java    License: 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 28
Source Project: redisson   Source File: RedissonSubscription.java    License: 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 29
Source Project: redisson   Source File: RedissonSubscription.java    License: 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 30
Source Project: redisson   Source File: RedissonSubscription.java    License: 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;
}