Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
An example to publish a message to RabbitMQ
public class Producer {
private final static String QUEUE_NAME = "Greet";
public static void main(String[] argumentVariables)
throws {
//opens a new connection with the broker
ConnectionFactory factory = new ConnectionFactory();
Connection connection = factory.newConnection();
//create a new channel and uses and internal channel number
Channel channel = connection.createChannel();
//declaring a queue with given String queue, boolean durable, boolean exclusive, boolean autoDelete,
// Map<String, Object> arguments. AMQP Command queue.declare
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
String message = "Hello World!";
//basic.publish command to put a message in the default exchange
channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
System.out.println(" [x] Sent '" + message + "'");
//Lets close the channel and connection once we are finished with the job.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment