Created
November 25, 2013 12:09
-
-
Save akkinenivijay/7640364 to your computer and use it in GitHub Desktop.
An example to publish a message to RabbitMQ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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