Skip to content

Instantly share code, notes, and snippets.

@debraj-manna
Last active November 22, 2015 11:34
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 debraj-manna/c470073496ddff1f26e4 to your computer and use it in GitHub Desktop.
Save debraj-manna/c470073496ddff1f26e4 to your computer and use it in GitHub Desktop.
Priority Queue in RabbitMq which sends message with priority and having type "HELLO"
package helloWorld;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.BasicProperties;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.MessageProperties;
public class PrioritySend {
private static final String QUEUE = "hello_priority";
public static void main(String arg[]) throws Exception {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
Map<String, Object> args = new HashMap<String, Object>();
args.put("x-max-priority", 10);
channel.queueDeclare(QUEUE, true, false, false, args);
for (int i = 0; i < 10; i++) {
publish(channel, i, "Message" + i);
}
channel.close();
connection.close();
}
private static void publish(Channel ch, int priority, String msg)
throws IOException {
// AMQP.BasicProperties props =
// MessageProperties.PERSISTENT_BASIC.builder().priority(priority).build();
AMQP.BasicProperties props = new AMQP.BasicProperties.Builder()
.priority(priority).type("HELLO").build();
String body = msg + " message with priority " + priority;
System.out.println("Sent " + body);
ch.basicPublish("", QUEUE, props, body.getBytes());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment