springboot 基于 redis redisson的消息队列

springboot 基于 redis redisson的消息队列

消息接收方

@Slf4j
@Component
@Order(200)
public class ClickQueueMessageReceive implements ApplicationRunner {
          
   

    @Resource
    private RedissonClient redissonClient;

    @Async("birdsExecutor")
    @Override
    public void run(ApplicationArguments args) throws Exception {
          
   
        while (true){
          
   
        
            try {
          
   
                RBlockingQueue<NovelIdTopicDto> blockingQueue =
    redissonClient.getBlockingQueue(TopicAndQueueKeyConst.CLICK_QUEUE);
                NovelIdTopicDto novelIdTopicDto = blockingQueue.take();
               //do some 
               
            } catch (InterruptedException e) {
          
   
                log.warn(e.getMessage(),e);
            }
            try {
          
   
                TimeUnit.MILLISECONDS.sleep(500);
            } catch (InterruptedException e) {
          
   
                log.info(e.getMessage(),e);
            }
        }
    }
}

消息发送方

@Slf4j
@Service
public class TopicAndQueuePushService {
          
   

    @Resource
    private RedissonClient redissonClient;

    @Async
    public void sendRead(String novelId){
          
   
        RBlockingQueue<NovelIdTopicDto> blockingQueue =
                redissonClient.getBlockingQueue(TopicAndQueueKeyConst.CLICK_QUEUE);
        blockingQueue.putAsync(getNovelIdTopicDto(novelId));
    }
}

完整代码查看

经验分享 程序员 微信小程序 职场和发展