Using a Java program as a working node for celery

Source: Internet
Author: User

Celery is a python implementation of the distributed scheduling framework, sometimes want to use celery to invoke the Java service, there is a Celery-java library can be used, to achieve this effect, record:

Add Dependencies First:

        <dependency>            <groupId>com.geneea.celery</groupId>            <artifactId>celery-java</artifactId>            <version>1.10.4</version>        </dependency>

Define task Classes FirstTestTask.java

import com.geneea.celery.CeleryTask;@CeleryTaskpublic class TestTask {    public int sum(int x, int y){        return x + y;    }}

Re-defining the work entry classMyWorker.java

import com.geneea.celery.CeleryWorkerCLI;public class MyWorker {    public static void main(String[] args) throws Exception {        //注意broker的url的末尾/必须用转义符%2F        args = new String[]{"--queue","celery",                            "--concurrency","2",                            "--broker","amqp://guest:[email protected]:5672/%2F"};        CeleryWorkerCLI.main(args);    }}

Run MyWorker a bootable java version of the celery work node:

Started consuming tasks from queue celery.Known tasks:  - cn.main.celery.TestTask

Then define the celery Task Scheduler test.py :

import celeryapp = celery.Celery(broker="amqp://guest:[email protected]:5672//", backend="rpc://guest:[email protected]:5672//")#调度任务格式为pacakage.class#func result = app.signature("cn.main.celery.TestTask#sum", [1, 2]).delay().get()print("result is %s" % result)

The running test.py program responds as follows:

C:\ProgramData\Anaconda3\python.exe D:/Test/python/celerys/javatest.pyresult is 3Process finished with exit code 0
19:06:43.685 INFO  [pool-1-thread-1] c.g.c.CeleryWorkerCore:126 - CeleryTask cn.main.celery.TestTask#sum[2c1476a0-0d64-47c8-afce-6ddc99915ea9] succeeded in 338.9 μs

Reference Links:

Https://bitbucket.org/geneea/celery-java

Using a Java program as a working node for celery

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.