Skip to content

Instantly share code, notes, and snippets.

@akkinenivijay
Created November 25, 2013 12:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akkinenivijay/7640364 to your computer and use it in GitHub Desktop.
Save akkinenivijay/7640364 to your computer and use it in GitHub Desktop.
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 java.io.IOException {
//opens a new connection with the broker
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
factory.setUsername("guest");
factory.setPassword("guest");
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.
channel.close();
connection.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment