juforg / celery-spring-boot-starter

Springboot starter for celery using celery-java, which is Java implementation of Celery client and worker.
MIT License
5 stars 0 forks source link
asynctask celery celery-java java java-python springboot-starter

celery-spring-boot-starter

Springboot starter for celery using celery-java, which is Java implementation of Celery client and worker. Quoting from the project website:

Celery is an asynchronous task queue/job queue based on distributed message passing. It is focused on real-time operation, but supports scheduling as well.

The execution units, called tasks, are executed concurrently on a single or more worker servers using multiprocessing, Eventlet, or gevent. Tasks can execute asynchronously (in the background) or synchronously (wait until ready).

Celery is used in production systems to process millions of tasks a day.

For more info, celery-java

The aim is to be compatible with existing Python Celery implementation. That means you should be able to run a Java client with a Python worker or vice-versa. Tested with Python Celery 5.1.

Maven dependency

Releases are available from Maven Central. Latest version: Maven
Central

<dependency>
    <groupId>vip.appcity</groupId>
    <artifactId>celery-spring-boot-starter</artifactId>
    <version>...</version>
</dependency>

Snapshots are available from Sonatype OSRH:

<repository>
    <id>sonatype</id>
    <url>https://oss.sonatype.org/content/groups/public</url>
    <snapshots>
        <enabled>true</enabled>
        <updatePolicy>always</updatePolicy>
    </snapshots>
</repository>

init config in application.yml

celery:
  queue: "demo:celery"
  broker: amqp://guest:guest@localhost:5672/vhost
#  backend:

Calling Python task from Java

  1. Start a celery worker as described in First Steps with Celery.
  2. define a worker task with python
    @celery.task(name='test.dummy_task')
    def dummy_task(num):
        print(num)
        return "finished"
  3. Call the task by name in java

    
        @Resource
        private Celery celery;
        celery.submit("test.dummy_task", new Object[]{1});

    enable multi queue

  4. init config in application.yml
    celery:
    enabled: true
    enableMultiQueue: true
    queue: "demo:celery"
    broker: amqp://guest:guest@localhost:5672/vhost
    #  backend:
    taskQueueMaps:
     "test.dummy_task": "celery"
     "test.dummy_task2": "celery"
     "test.dummy_task3": "celery1"
     "test.dummy_task4": "celery2"
  5. Call the task by name in java

    
        @Resource
        private CeleryTaskDistributor celeryTaskDistributor;
        celeryTaskDistributor.submit("test.dummy_task", new Object[]{1});

Relase notes