Last active
November 22, 2015 11:34
-
-
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"
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
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