Skip to content

Instantly share code, notes, and snippets.

@nherbaut
Last active October 19, 2017 14:32
Show Gist options
  • Save nherbaut/6db76528693cec7df05f to your computer and use it in GitHub Desktop.
Save nherbaut/6db76528693cec7df05f to your computer and use it in GitHub Desktop.
Sending Celery Task through rabbitmq in Java
package com.mirlitone;
import java.io.IOException;
import org.junit.Test;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
public class Dummy {
@Test
public void testAll() throws IOException {
String QUEUE_NAME = "celery";
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(QUEUE_NAME, true, false, false, null);
String message = "{\"id\": \"4cc7438e-afd4-4f8f-a2f3-f46567e7ca77\", \"task\": \"tasks.add\", \"args\": [1,2], \"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();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment