Skip to content

Instantly share code, notes, and snippets.

@kiichi
Created February 20, 2016 17:26
Show Gist options
  • Select an option

  • Save kiichi/ce8a59ed9fb4539c4ba2 to your computer and use it in GitHub Desktop.

Select an option

Save kiichi/ce8a59ed9fb4539c4ba2 to your computer and use it in GitHub Desktop.
RabbitMQ Sample in Java
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Channel;
// See python code comment for username / password settings
//javac -cp rabbitmq-client.jar Send.java
//java -cp .:./rabbitmq-client.jar Send
public class Send {
private final static String QUEUE_NAME = "javahello";
public static void main(String[] argv) throws Exception {
String server = "192.168.64.2";
ConnectionFactory factory = new ConnectionFactory();
factory.setHost(server);
factory.setUsername("test");
factory.setPassword("test");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
String message = "Hello From Java!";
channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
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