Giter Club home page Giter Club logo

spring-boot-starter-jesque's Introduction

任务延时队列使用

基于Jesque封装的springboot starter,使用注解的方式方便使用。

使用方法

  • pom
<dependencies>
    <dependency>
        <groupId>io.patamon.jesque</groupId>
        <artifactId>spring-boot-starter-jesque</artifactId>
        <version>1.2</version>
    </dependency>
</dependencies>

<repositories>
    <repository>
        <id>patamon.release.repository</id>
        <name>github release repository</name>
        <url>https://raw.github.com/icemimosa/maven/release/</url>
    </repository>
</repositories>
  • Configuration (可以参照spring-data-redis)
spring:
  redis:
    host: 127.0.0.1
    port: 6379

Message Producer

  • 新建一个任务(队列)类型
/**
 * Desc: 队列任务类型常量
 *
 * 也可在其他地方声明, 这里作为测试. 随意...
 */
public class JesqueType {

    /**
     * 测试JOB队列类型
     */
    public static final String JOB_TEST_TYPE = "JOB_TEST";
}
  • Producer example
/**
 * Desc: 队列任务生产者例子
 * 
 * [使用方式]: 使用 {@link JobClient} 接口的方法进行任务的提交
 */
@Slf4j
@RestController
@RequestMapping("/api/common/test/job")
public class DemoProvider {
    @Autowired
    private JobClient jobClient;

    @RequestMapping(value = "/{itemId}", method = RequestMethod.GET)
    public void test(@PathVariable Long itemId) {
        log.info("[Jesque Start]");
        for (int i = 1; i <= 10; i++) {
            jobClient.submit(itemId + "" + i, JesqueType.JOB_TEST_TYPE, i * 1000);
        }
        log.info("[Jesque End]");
    }

    @RequestMapping(value = "/now/{itemId}", method = RequestMethod.GET)
    public void testNow(@PathVariable Long itemId) {
        log.info("[Jesque Start]");
        jobClient.submit(itemId + "", JesqueType.JOB_TEST_TYPE);
        log.info("[Jesque End]");
    }

}

Message Consumer

/**
 * Desc: 队列任务消费者例子
 *
 * [使用方式]:
 *  1. 声明一个类, 用 {@link JobConsumer} 修饰
 *  2. 定义一个public方法, 用 {@link JobType} 修饰, 并传入任务类型常量, 如 {@link JesqueType}
 *  3. 方法需要给定一个String类型参数, 这个参数就是Provider提供的业务ID
 *
 * [注意]:
 *  需要注意的是, 同一个 {@link JobType} 的多个会被多次执行. 这里的消费方法与队列的消费者有本质的区别.
 *  队列消费者只会有一个抢到任务并消费, 这里针对部署了多份服务(多个JVM)
 *  方法消费是指在一个服务中声明了多个相同的 {@link JobType}, 所以每个方法都会被执行, 而不是原则上的多次消费.
 *
 */
@Slf4j
@JobConsumer
public class DemoConsumer {

    @JobType(JesqueType.JOB_TEST_TYPE)
    public void consume(String itemId) {
        log.info("JOB_TEST consume {}", itemId);
    }

    @JobType(JesqueType.JOB_TEST_TYPE)
    public void consume2(String itemId) {
        log.info("JOB_TEST consume2 {}", itemId);
    }
}

change log

  • 2018-09
    • Jesque因为redis服务挂掉, 而导致worker线程挂掉. 目前使用自检测重启的方式解决.
  • 2019-04
    • Jesque2.1.3修复了之前的问题

spring-boot-starter-jesque's People

Contributors

icemimosa avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.