Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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 :-

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. 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. 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 :-

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

like image 362
Amit Gupta Avatar asked Dec 30 '13 16:12

Amit Gupta


1 Answers

There were couple of issues.

1) String queueName = channel.queueDeclare().getQueue() command was returning wrong queue name. I changed the queuename to "celery" and it worked fine. 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"}

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

It worked fine after these two changes.

-Amit

like image 87
Amit Gupta Avatar answered Oct 15 '22 22:10

Amit Gupta