项目作者: biezhi

项目描述 :
redis base delay queue
高级语言: Java
项目地址: git://github.com/biezhi/redis-dqueue.git
创建时间: 2019-11-25T14:13:59Z
项目社区:https://github.com/biezhi/redis-dqueue

开源协议:

下载


redis-dqueue

redis-dqueue is a redis + Java 8 base delayed queue library.

Document

Travis Build

License
Twitter URL

Feature

  • Push message delay
  • Consumer allowed to try again
  • Based on the message separation of the topic
  • Integrated SpringBoot

Normal Java Application

With Maven

  1. <dependency>
  2. <groupId>io.github.biezhi</groupId>
  3. <artifactId>redis-dqueue-core</artifactId>
  4. <version>0.0.3.ALPHA</version>
  5. </dependency>

Push message and subscribe topic

  1. RDQueue rdQueue = new RDQueue(new Config());
  2. // "hello world" messages sent after 10 seconds
  3. Message<String> message = new Message<>("TEST_TOPIC", "hello world", 10);
  4. // async push delay message
  5. rdQueue.asyncPush(message, (key, throwable) -> log.info("key send ok:" + key));
  6. // subscribe topic
  7. rdQueue.subscribe("TEST_TOPIC", callback());

Callback

  1. private static Callback<String> callback() {
  2. return new Callback<String>() {
  3. @Override
  4. public ConsumeStatus execute(String data) {
  5. log.info("消费数据:: {}", data);
  6. return ConsumeStatus.CONSUMED;
  7. }
  8. };
  9. }

Spring Boot Application

With Maven

  1. <dependency>
  2. <groupId>io.github.biezhi</groupId>
  3. <artifactId>redis-dqueue-spring-boot-starter</artifactId>
  4. <version>0.0.3.ALPHA</version>
  5. </dependency>

Push message

  1. @Autowired
  2. private RDQueueTemplate rdQueueTemplate;
  3. @GetMapping("/push")
  4. public String push(String id) throws RDQException {
  5. Message<String> message = new Message<>();
  6. message.setTopic("order-cancel");
  7. message.setPayload(id);
  8. message.setDelayTime(10);
  9. rdQueueTemplate.asyncPush(message, (s, throwable) -> {
  10. // TODO async push result
  11. });
  12. return "推送成功";
  13. }

Subscribe topic

You need to implement MessageListener, subscribe to the related topic, process delay messages in the execute method.

Ensure that the class was Spring managed.

  1. @Component
  2. public class OrderCancelListener implements MessageListener<String> {
  3. @Override
  4. public String topic() {
  5. return "order-cancel";
  6. }
  7. @Override
  8. public ConsumeStatus execute(String data) {
  9. log.info("取消订单: {}", data);
  10. return ConsumeStatus.CONSUMED;
  11. }
  12. }

Lisence

Apache2