forked from rabbitmq/rabbitmq-tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EmitLogHeader.java
61 lines (46 loc) · 2.61 KB
/
EmitLogHeader.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import com.rabbitmq.client.*;
import java.util.HashMap;
import java.util.Map;
public class EmitLogHeader {
private static final String EXCHANGE_NAME = "header_test";
public static void main(String[] argv) throws Exception {
if (argv.length < 1) {
System.err.println("Usage: EmitLogHeader message queueName [headers]...");
System.exit(1);
}
// The API requires a routing key, but in fact if you are using a header exchange the
// value of the routing key is not used in the routing. You can store information
// for the receiver here as the routing key is still available in the received message.
String routingKey = "ourTestRoutingKey";
// Argument processing: the first arg is the message, the rest are
// key value pairs for headers.
String message = argv[0];
// The map for the headers.
Map<String, Object> headers = new HashMap<String, Object>();
// The rest of the arguments are key value header pairs. For the purpose of this
// example, we are assuming they are all strings, but that is not required by RabbitMQ
for (int i = 1; i < argv.length; i++) {
System.out.println("Adding header " + argv[i] + " with value " + argv[i + 1] + " to Map");
headers.put(argv[i], argv[i + 1]);
i++;
}
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
try (Connection connection = factory.newConnection();
Channel channel = connection.createChannel()) {
channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.HEADERS);
AMQP.BasicProperties.Builder builder = new AMQP.BasicProperties.Builder();
// MessageProperties.PERSISTENT_TEXT_PLAIN is a static instance of AMQP.BasicProperties
// that contains a delivery mode and a priority. So we pass them to the builder.
builder.deliveryMode(MessageProperties.PERSISTENT_TEXT_PLAIN.getDeliveryMode());
builder.priority(MessageProperties.PERSISTENT_TEXT_PLAIN.getPriority());
// Add the headers to the builder.
builder.headers(headers);
// Use the builder to create the BasicProperties object.
AMQP.BasicProperties theProps = builder.build();
// Now we add the headers. This example only uses string headers, but they can also be integers
channel.basicPublish(EXCHANGE_NAME, routingKey, theProps, message.getBytes("UTF-8"));
System.out.println(" [x] Sent message: '" + message + "'");
}
}
}