简体   繁体   English

如何从Java等非python语言调用芹菜任务延迟函数?

[英]How to call a celery task delay function from non-python languages such as Java?

I have setup celery + rabbitmq for on a 3 cluster machine. 我已经在3台机器上安装了celery + rabbitmq。 I have also created a task which generates a regular expression based on data from the file and uses the information to parse text. 我还创建了一个任务,它根据文件中的数据生成正则表达式,并使用该信息来解析文本。

from celery import Celery

celery = Celery('tasks', broker='amqp://localhost//')
import re

@celery.task
def add(x, y):
     return x + y


def get_regular_expression():
    with open("text") as fp:
        data = fp.readlines()
    str_re = "|".join([x.split()[2] for x in data ])
    return str_re    



@celery.task
def analyse_json(tw):
    str_re = get_regular_expression()
    re.match(str_re,tw.text) 

I can make the call to this task very easily using the following python code :- 我可以使用以下python代码轻松调用此任务: -

from tasks import analyse_tweet_json
x = tweet ## load from a file (x is a json)
analyse_tweet_json.delay(x) 

However, now I want to make the same call from Java and not python. 但是,现在我想从Java而不是python进行相同的调用。 I am not sure what's the easiest way of doing the same. 我不确定做同样事情的最简单方法是什么。

I've written this code for sending a message to the AMQP broker. 我已经编写了这段代码,用于向AMQP代理发送消息。 The code runs fine, but the task is not carried out. 代码运行正常,但任务没有执行。 I am not sure how to specify the name of the task which should be carried out. 我不知道如何指定应该执行的任务的名称。

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Channel;

class try1 {
public static void main(String[] args) throws Exception {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setUri("amqp://localhost");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();
    String queueName = channel.queueDeclare().getQueue();
    channel.queueBind(queueName, "celery", "celery");
    String messageBody = "{\"text\":\"i am good\"}" ;
    byte[] msgBytes = messageBody.getBytes("ASCII") ;
    channel.basicPublish(queueName, queueName,
            new AMQP.BasicProperties
            ("application/json", null, null, null,
                    null, null, null, null,
                    null, null, null, "guest",
                    null, null),messageBody.getBytes("ASCII")) ;
    connection.close();    

} } }}

this is the output in the errorlog of rabbitMq :- 这是rabbitMq的错误日志中的输出: -

connection <0.14627.0>, channel 1 - error:
{amqp_error,not_found,
"no exchange 'amq.gen-gEV47GX9pF_oZ-0bEnOazE' in vhost '/'",
'basic.publish'}

Any help will be appreciated. 任何帮助将不胜感激。

thanks, Amit 谢谢,阿米特

There were couple of issues. 有几个问题。

1) String queueName = channel.queueDeclare().getQueue() command was returning wrong queue name. 1)String queueName = channel.queueDeclare()。getQueue()命令返回错误的队列名称。 I changed the queuename to "celery" and it worked fine. 我将queuename更改为“芹菜”,它运行良好。 2) The format of json has to be of this type:- {"id": "4cc7438e-afd4-4f8f-a2f3-f46567e7ca77", "task": "celery.task.PingTask", "args": [], "kwargs": {}, "retries": 0, "eta": "2009-11-17T12:30:56.527191"} 2)json的格式必须是这种类型: - {“id”:“4cc7438e-afd4-4f8f-a2f3-f46567e7ca77”,“task”:“celery.task.PingTask”,“args”:[], “kwargs”:{},“retries”:0,“eta”:“2009-11-17T12:30:56.527191”}

as seen in http://docs.celeryproject.org/en/latest/internals/protocol.html http://docs.celeryproject.org/en/latest/internals/protocol.html中所示

It worked fine after these two changes. 在这两个变化之后,它运作良好。

-Amit -Amit

celery implicitly declare an exchange, using Java you'll have to declare one yourself. 芹菜隐含地声明交换,使用Java你必须自己声明一个。

see Interoperating with Django/Celery From Java 请参阅与Java的Django / Celery互操作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM