如何从非python语言(如Java)调用芹菜任务延迟函数?
我已在3群集计算机上为设置芹菜+rabbitmq。我还创建了一个任务,该任务根据文件中的数据生成一个正则表达式,并使用该信息解析文本如何从非python语言(如Java)调用芹菜任务延迟函数?,java,python,rabbitmq,celery,celery-task,Java,Python,Rabbitmq,Celery,Celery Task,我已在3群集计算机上为设置芹菜+rabbitmq。我还创建了一个任务,该任务根据文件中的数据生成一个正则表达式,并使用该信息解析文本 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
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)
我可以使用以下python代码非常轻松地调用此任务:-
from tasks import analyse_tweet_json
x = tweet ## load from a file (x is a json)
analyse_tweet_json.delay(x)
但是,现在我想从Java而不是python发出相同的调用。我不知道做同样的事情最简单的方法是什么
我编写了此代码,用于向AMQP代理发送消息。代码运行正常,但任务未执行。我不知道如何指定应该执行的任务的名称
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();
}
}
这是rabbitMq的错误日志中的输出:-
connection <0.14627.0>, channel 1 - error:
{amqp_error,not_found,
"no exchange 'amq.gen-gEV47GX9pF_oZ-0bEnOazE' in vhost '/'",
'basic.publish'}
连接,通道1-错误:
{amqp_错误,未找到,
“vhost'/'中没有交换'amq.gen-gEV47GX9pF_oZ-0bEnOazE'”,
'basic.publish'}
任何帮助都将不胜感激
谢谢,
Amit芹菜隐式声明了一个交换,使用Java您必须自己声明一个交换
参见有两个问题 1) 字符串queueName=channel.queueDeclare().getQueue()命令返回了错误的队列名称。我把名字改为“芹菜”,效果很好。 2) json的格式必须为以下类型:- {“id”:“4cc7438e-afd4-4f8f-a2f3-f46567e7ca77”, “任务”:“芹菜。任务。任务”, “args”:[], “kwargs”:{}, “重试次数”:0, “eta”:“2009-11-17T12:30:56.527191” 如图所示 在这两个变化之后,它工作得很好
-Amit事实上,还有其他问题。请看我的解决方案。