Skip to content

Commit 54bf8a1

Browse files
committed
Correction of Java examples to preserve utf-8 string encoding
1 parent a008f34 commit 54bf8a1

13 files changed

+83
-83
lines changed

java/EmitLog.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,19 @@ public static void main(String[] argv) throws Exception {
1717

1818
String message = getMessage(argv);
1919

20-
channel.basicPublish(EXCHANGE_NAME, "", null, message.getBytes());
20+
channel.basicPublish(EXCHANGE_NAME, "", null, message.getBytes("UTF-8"));
2121
System.out.println(" [x] Sent '" + message + "'");
2222

2323
channel.close();
2424
connection.close();
2525
}
26-
26+
2727
private static String getMessage(String[] strings){
2828
if (strings.length < 1)
2929
return "info: Hello World!";
3030
return joinStrings(strings, " ");
3131
}
32-
32+
3333
private static String joinStrings(String[] strings, String delimiter) {
3434
int length = strings.length;
3535
if (length == 0) return "";

java/EmitLogDirect.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,25 @@ public static void main(String[] argv) throws Exception {
1818
String severity = getSeverity(argv);
1919
String message = getMessage(argv);
2020

21-
channel.basicPublish(EXCHANGE_NAME, severity, null, message.getBytes());
21+
channel.basicPublish(EXCHANGE_NAME, severity, null, message.getBytes("UTF-8"));
2222
System.out.println(" [x] Sent '" + severity + "':'" + message + "'");
2323

2424
channel.close();
2525
connection.close();
2626
}
27-
27+
2828
private static String getSeverity(String[] strings){
2929
if (strings.length < 1)
3030
return "info";
3131
return strings[0];
3232
}
3333

34-
private static String getMessage(String[] strings){
34+
private static String getMessage(String[] strings){
3535
if (strings.length < 2)
3636
return "Hello World!";
3737
return joinStrings(strings, " ", 1);
3838
}
39-
39+
4040
private static String joinStrings(String[] strings, String delimiter, int startIndex) {
4141
int length = strings.length;
4242
if (length == 0 ) return "";

java/EmitLogHeader.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public static void main(String[] argv) {
1515
Connection connection = null;
1616
Channel channel = null;
1717
try {
18-
18+
1919
if (argv.length < 1){
2020
System.err.println("Usage: EmitLogHeader message queueName [headers]...");
2121
System.exit(1);
@@ -43,18 +43,18 @@ public static void main(String[] argv) {
4343

4444
ConnectionFactory factory = new ConnectionFactory();
4545
factory.setHost("localhost");
46-
46+
4747
connection = factory.newConnection();
4848
channel = connection.createChannel();
4949

5050
channel.exchangeDeclare(EXCHANGE_NAME, "headers");
5151

5252
AMQP.BasicProperties.Builder builder = new AMQP.BasicProperties.Builder();
5353

54-
// MessageProperties.PERSISTENT_TEXT_PLAIN is a static instance of AMQP.BasicProperties
54+
// MessageProperties.PERSISTENT_TEXT_PLAIN is a static instance of AMQP.BasicProperties
5555
// that contains a delivery mode and a priority. So we pass them to the builder.
56-
builder.deliveryMode(MessageProperties.PERSISTENT_TEXT_PLAIN.getDeliveryMode());
57-
builder.priority(MessageProperties.PERSISTENT_TEXT_PLAIN.getPriority());
56+
builder.deliveryMode(MessageProperties.PERSISTENT_TEXT_PLAIN.getDeliveryMode());
57+
builder.priority(MessageProperties.PERSISTENT_TEXT_PLAIN.getPriority());
5858

5959
// Add the headers to the builder.
6060
builder.headers(headers);
@@ -63,7 +63,7 @@ public static void main(String[] argv) {
6363
AMQP.BasicProperties theProps = builder.build();
6464

6565
// Now we add the headers. This example only uses string headers, but they can also be integers
66-
channel.basicPublish(EXCHANGE_NAME, routingKey, theProps, message.getBytes());
66+
channel.basicPublish(EXCHANGE_NAME, routingKey, theProps, message.getBytes("UTF-8"));
6767
System.out.println(" [x] Sent message: '" + message + "'");
6868

6969
}

java/EmitLogTopic.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public static void main(String[] argv) {
1212
try {
1313
ConnectionFactory factory = new ConnectionFactory();
1414
factory.setHost("localhost");
15-
15+
1616
connection = factory.newConnection();
1717
channel = connection.createChannel();
1818

@@ -21,7 +21,7 @@ public static void main(String[] argv) {
2121
String routingKey = getRouting(argv);
2222
String message = getMessage(argv);
2323

24-
channel.basicPublish(EXCHANGE_NAME, routingKey, null, message.getBytes());
24+
channel.basicPublish(EXCHANGE_NAME, routingKey, null, message.getBytes("UTF-8"));
2525
System.out.println(" [x] Sent '" + routingKey + "':'" + message + "'");
2626

2727
}
@@ -37,19 +37,19 @@ public static void main(String[] argv) {
3737
}
3838
}
3939
}
40-
40+
4141
private static String getRouting(String[] strings){
4242
if (strings.length < 1)
4343
return "anonymous.info";
4444
return strings[0];
4545
}
4646

47-
private static String getMessage(String[] strings){
47+
private static String getMessage(String[] strings){
4848
if (strings.length < 2)
4949
return "Hello World!";
5050
return joinStrings(strings, " ", 1);
5151
}
52-
52+
5353
private static String joinStrings(String[] strings, String delimiter, int startIndex) {
5454
int length = strings.length;
5555
if (length == 0 ) return "";

java/NewTask.java

+10-10
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import com.rabbitmq.client.MessageProperties;
55

66
public class NewTask {
7-
7+
88
private static final String TASK_QUEUE_NAME = "task_queue";
99

1010
public static void main(String[] argv) throws Exception {
@@ -13,26 +13,26 @@ public static void main(String[] argv) throws Exception {
1313
factory.setHost("localhost");
1414
Connection connection = factory.newConnection();
1515
Channel channel = connection.createChannel();
16-
16+
1717
channel.queueDeclare(TASK_QUEUE_NAME, true, false, false, null);
18-
18+
1919
String message = getMessage(argv);
20-
21-
channel.basicPublish( "", TASK_QUEUE_NAME,
20+
21+
channel.basicPublish( "", TASK_QUEUE_NAME,
2222
MessageProperties.PERSISTENT_TEXT_PLAIN,
23-
message.getBytes());
23+
message.getBytes("UTF-8"));
2424
System.out.println(" [x] Sent '" + message + "'");
25-
25+
2626
channel.close();
2727
connection.close();
2828
}
29-
29+
3030
private static String getMessage(String[] strings){
3131
if (strings.length < 1)
3232
return "Hello World!";
3333
return joinStrings(strings, " ");
34-
}
35-
34+
}
35+
3636
private static String joinStrings(String[] strings, String delimiter) {
3737
int length = strings.length;
3838
if (length == 0) return "";

java/RPCClient.java

+15-15
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,38 @@
44
import com.rabbitmq.client.QueueingConsumer;
55
import com.rabbitmq.client.AMQP.BasicProperties;
66
import java.util.UUID;
7-
7+
88
public class RPCClient {
9-
9+
1010
private Connection connection;
1111
private Channel channel;
1212
private String requestQueueName = "rpc_queue";
1313
private String replyQueueName;
1414
private QueueingConsumer consumer;
15-
15+
1616
public RPCClient() throws Exception {
1717
ConnectionFactory factory = new ConnectionFactory();
1818
factory.setHost("localhost");
1919
connection = factory.newConnection();
2020
channel = connection.createChannel();
2121

22-
replyQueueName = channel.queueDeclare().getQueue();
22+
replyQueueName = channel.queueDeclare().getQueue();
2323
consumer = new QueueingConsumer(channel);
2424
channel.basicConsume(replyQueueName, true, consumer);
2525
}
26-
27-
public String call(String message) throws Exception {
26+
27+
public String call(String message) throws Exception {
2828
String response = null;
2929
String corrId = UUID.randomUUID().toString();
30-
30+
3131
BasicProperties props = new BasicProperties
3232
.Builder()
3333
.correlationId(corrId)
3434
.replyTo(replyQueueName)
3535
.build();
36-
37-
channel.basicPublish("", requestQueueName, props, message.getBytes());
38-
36+
37+
channel.basicPublish("", requestQueueName, props, message.getBytes("UTF-8"));
38+
3939
while (true) {
4040
QueueingConsumer.Delivery delivery = consumer.nextDelivery();
4141
if (delivery.getProperties().getCorrelationId().equals(corrId)) {
@@ -44,20 +44,20 @@ public String call(String message) throws Exception {
4444
}
4545
}
4646

47-
return response;
47+
return response;
4848
}
49-
49+
5050
public void close() throws Exception {
5151
connection.close();
5252
}
53-
53+
5454
public static void main(String[] argv) {
5555
RPCClient fibonacciRpc = null;
5656
String response = null;
5757
try {
5858
fibonacciRpc = new RPCClient();
59-
60-
System.out.println(" [x] Requesting fib(30)");
59+
60+
System.out.println(" [x] Requesting fib(30)");
6161
response = fibonacciRpc.call("30");
6262
System.out.println(" [.] Got '" + response + "'");
6363
}

java/ReceiveLogHeader.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ public static void main(String[] argv) {
1717
System.err.println("Usage: ReceiveLogsHeader queueName [headers]...");
1818
System.exit(1);
1919
}
20-
20+
2121
ConnectionFactory factory = new ConnectionFactory();
2222
factory.setHost("localhost");
23-
23+
2424
connection = factory.newConnection();
2525
channel = connection.createChannel();
2626

@@ -30,14 +30,14 @@ public static void main(String[] argv) {
3030
// value of the routing key is not used in the routing. You can receive information
3131
// from the sender here as the routing key is still available in the received message.
3232
String routingKeyFromUser = "ourTestRoutingKey";
33-
33+
3434
// Argument processing: the first arg is the local queue name, the rest are
3535
// key value pairs for headers.
3636
String queueInputName = argv[0];
3737

3838
// The map for the headers.
3939
Map<String, Object> headers = new HashMap<String, Object>();
40-
40+
4141
// The rest of the arguments are key value header pairs. For the purpose of this
4242
// example, we are assuming they are all strings, but that is not required by RabbitMQ
4343
// Note that when you run this code you should include the x-match header on the command
@@ -51,18 +51,18 @@ public static void main(String[] argv) {
5151

5252
String queueName = channel.queueDeclare(queueInputName, true, false, false, null).getQueue();
5353
channel.queueBind(queueName, EXCHANGE_NAME, routingKeyFromUser, headers);
54-
54+
5555
System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
5656

5757
QueueingConsumer consumer = new QueueingConsumer(channel);
5858
channel.basicConsume(queueName, true, consumer);
5959

6060
while (true) {
6161
QueueingConsumer.Delivery delivery = consumer.nextDelivery();
62-
String message = new String(delivery.getBody());
62+
String message = new String(delivery.getBody(),"UTF-8");
6363
String routingKeyFromSender = delivery.getEnvelope().getRoutingKey();
6464

65-
System.out.println(" [x] Received '" + routingKeyFromSender + "':'" + message + "'");
65+
System.out.println(" [x] Received '" + routingKeyFromSender + "':'" + message + "'");
6666
}
6767
}
6868
catch (Exception e) {

java/ReceiveLogs.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,17 @@ public static void main(String[] argv) throws Exception {
1717
channel.exchangeDeclare(EXCHANGE_NAME, "fanout");
1818
String queueName = channel.queueDeclare().getQueue();
1919
channel.queueBind(queueName, EXCHANGE_NAME, "");
20-
20+
2121
System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
2222

2323
QueueingConsumer consumer = new QueueingConsumer(channel);
2424
channel.basicConsume(queueName, true, consumer);
2525

2626
while (true) {
2727
QueueingConsumer.Delivery delivery = consumer.nextDelivery();
28-
String message = new String(delivery.getBody());
28+
String message = new String(delivery.getBody(),"UTF-8");
2929

30-
System.out.println(" [x] Received '" + message + "'");
30+
System.out.println(" [x] Received '" + message + "'");
3131
}
3232
}
3333
}

java/ReceiveLogsDirect.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,27 @@ public static void main(String[] argv) throws Exception {
1616

1717
channel.exchangeDeclare(EXCHANGE_NAME, "direct");
1818
String queueName = channel.queueDeclare().getQueue();
19-
19+
2020
if (argv.length < 1){
2121
System.err.println("Usage: ReceiveLogsDirect [info] [warning] [error]");
2222
System.exit(1);
2323
}
24-
25-
for(String severity : argv){
24+
25+
for(String severity : argv){
2626
channel.queueBind(queueName, EXCHANGE_NAME, severity);
2727
}
28-
28+
2929
System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
3030

3131
QueueingConsumer consumer = new QueueingConsumer(channel);
3232
channel.basicConsume(queueName, true, consumer);
3333

3434
while (true) {
3535
QueueingConsumer.Delivery delivery = consumer.nextDelivery();
36-
String message = new String(delivery.getBody());
36+
String message = new String(delivery.getBody(),"UTF-8");
3737
String routingKey = delivery.getEnvelope().getRoutingKey();
3838

39-
System.out.println(" [x] Received '" + routingKey + "':'" + message + "'");
39+
System.out.println(" [x] Received '" + routingKey + "':'" + message + "'");
4040
}
4141
}
4242
}

0 commit comments

Comments
 (0)