監聽redis消息隊列的示例:
springBoot實現redis發布式監聽消息隊列,定時器監聽不同功能的消息,代碼:
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
import org.springframework.stereotype.Component;
@Component
public class RedisMessageListener {
private static final String RECEIVE_NAME = "test";
/**
* 監聽quest1信息
*/
@Bean
public MessageListenerAdapter quest1() {
return new MessageListenerAdapter(new quest1(), RECEIVE_NAME);
}
/**
* 定義消息監聽者容器
* @param connectionFactory 連接工廠
* @param quest1 監聽quest1
* @return RedisMessageListenerContainer
*/
@Bean
public RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,
MessageListenerAdapter quest1) {
RedisMessageListenerContainer listenerContainer = new RedisMessageListenerContainer();
listenerContainer.setConnectionFactory(connectionFactory);
listenerContainer.addMessageListener(接收消息--quest1, new PatternTopic("消息key"));
return listenerContainer;
}
}