how to set the basic message properties for message in Rabbitmq?
i am using Rabbitmq J开发者_JAVA百科ava client API.i want to set the Basic Properties for message and also get the message Id of the message.if possible please provide some code to understand the things.
Thanks
While sending a message through java client usually it is publish to a channel like
CHANNEL.basicPublish(EXCHANGE_NAME, QUEUE_ROUTING_KEY, MessageProperties.PERSISTENT_TEXT_PLAIN, "message".getBytes)
Here you can set message properties
You can get the msg by using a delivery agent You have to first bind the queue like this
Channel channel = conn.createChannel();
String exchangeName = "myExchange";
String queueName = "myQueue";
String routingKey = "testRoute";
boolean durable = true;
channel.exchangeDeclare(exchangeName, "direct", durable);
channel.queueDeclare(queueName, durable,false,false,durable, null);
channel.queueBind(queueName, exchangeName, routingKey);
boolean noAck = false;
QueueingConsumer consumer = new QueueingConsumer(channel);
channel.basicConsume(queueName, noAck, consumer);
Then use delivey to get msg
QueueingConsumer.Delivery delivery;
try {
delivery = consumer.nextDelivery();
} catch (InterruptedException ie) {
continue;
}
Here is how it can be done:
int PERSISTENCE_MESSAGE = 2; // Persist message
String TEXT_MESSAGE = "text/plain";
String queueName = "QUE-1";
Channel channel = this.connection.createChannel();
channel.queueDeclare(queueName, true, false, false, null);
// Build message properties
Map messageProps = new HashMap();
//messageProps.put("TIME_MSG_RECEIVED", time);
messageProps.put("SOURCE_SYS", "SRC1");
messageProps.put("DESTINATION_SYS", "DST1");
// Set message properties
AMQP.BasicProperties.Builder basicProperties = new AMQP.BasicProperties.Builder();
basicProperties.contentType(TEXT_MESSAGE).deliveryMode(PERSISTENCE_MESSAGE)
.priority(1).headers(messageProps);
channel.basicPublish("", queueName, basicProperties.build(), message.getBytes());
System.out.println(" Sent message to RabbitMQ: '" + message + "'");
channel.close();
精彩评论