Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Celery to manage java tasks

I have a java client that communicates with celery using rabbitmq and sends the task to celery server for adding 2 numbers x and y

String QUEUE_NAME = "celery";
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
String x = "5";
String y = "10";
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
String ID = UUID.randomUUID().toString();
channel.queueDeclare(QUEUE_NAME, true, false, false, null);
String message = "{\"id\":\""+ID+"\", \"task\": \"tasks.add\", \"args\": ["+x+","+y+"], \"kwargs\": {}, \"retries\": 0, \"eta\": \"2009-11-17T12:30:56.527191\"}";
channel.basicPublish("", QUEUE_NAME, new AMQP.BasicProperties.Builder()
        .contentType("application/json").contentEncoding("utf-8")
        .build(), message.getBytes("utf-8"));
System.out.println(" [x] Sent '" + message + "'");
channel.close();
connection.close();

We have a python api add to add these number and that is managed by celery.

from celery import Celery

app = Celery('tasks', broker='amqp://guest@localhost//')

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

I would like to write this add(x,y) function in java that celery somehow identify a java add(x,y) method and manage it.

Note: I am looking for a solution without webhooks.

Thanks in advance.

like image 614
Rupesh Avatar asked Mar 18 '26 08:03

Rupesh


1 Answers

Check out https://crabhi.github.io/celery-java/

It's a (very alpha at the moment) implementation of Celery client and worker for JVM.

You can annotate your task class:

 import org.sedlakovi.celery.Task;

 @Task
 public class TestTask {

     public int sum(int x, int y) {
         return x + y;
     }
 }

And then call the task like this:

Client client = new Client(rabbitConnectionChannel, rabbitBackend);

Integer result = TestTaskProxy.with(client).sum(1, 7).get();
like image 94
Krab Avatar answered Mar 19 '26 22:03

Krab



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!