Skip to content

Commit 6b1e114

Browse files
author
Ann
committed
QA fixes: use StringBuilder, doWork, constant queue name
1 parent ce8c6c6 commit 6b1e114

File tree

2 files changed

+37
-29
lines changed

2 files changed

+37
-29
lines changed

java/NewTask.java

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,46 @@
1+
import java.io.IOException;
12
import com.rabbitmq.client.ConnectionFactory;
23
import com.rabbitmq.client.Connection;
34
import com.rabbitmq.client.Channel;
45
import com.rabbitmq.client.MessageProperties;
56

67
public class NewTask {
8+
9+
private static final String TASK_QUEUE_NAME = "task_queue";
10+
711
public static void main(String[] argv) throws java.io.IOException {
8-
Connection connection = null;
12+
913
ConnectionFactory factory = new ConnectionFactory();
1014
factory.setHost("localhost");
11-
connection = factory.newConnection();
15+
Connection connection = factory.newConnection();
1216
Channel channel = connection.createChannel();
1317

14-
channel.queueDeclare("task_queue", true, false, false, null);
18+
channel.queueDeclare(TASK_QUEUE_NAME, true, false, false, null);
1519

1620
String message = getMessage(argv);
1721

18-
channel.basicPublish( "", "task_queue",
22+
channel.basicPublish( "", TASK_QUEUE_NAME,
1923
MessageProperties.PERSISTENT_TEXT_PLAIN,
2024
message.getBytes());
2125
System.out.println(" [x] Sent '" + message + "'");
26+
2227
channel.close();
2328
connection.close();
2429
}
2530

2631
private static String getMessage(String[] strings){
2732
if (strings.length < 1)
2833
return "Hello World!";
29-
return joinStrings(strings);
34+
return joinStrings(strings, " ");
3035
}
3136

32-
private static String joinStrings(String[] strings){
33-
String words = "";
34-
for (String astring: strings)
35-
words = words.concat(astring).concat(" ");
36-
return words.trim();
37+
private static String joinStrings(String[] strings, String delimiter) {
38+
int length = strings.length;
39+
if (length == 0) return "";
40+
StringBuilder words = new StringBuilder(strings[0]);
41+
for (int i = 1; i < length; i++) {
42+
words.append(delimiter).append(strings[i]);
43+
}
44+
return words.toString();
3745
}
38-
}
39-
46+
}

java/Worker.java

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,45 @@
1+
import java.io.IOException;
12
import com.rabbitmq.client.ConnectionFactory;
23
import com.rabbitmq.client.Connection;
34
import com.rabbitmq.client.Channel;
45
import com.rabbitmq.client.QueueingConsumer;
56

67
public class Worker {
8+
9+
private static final String TASK_QUEUE_NAME = "task_queue";
10+
711
public static void main(String[] argv)
812
throws java.io.IOException,
913
java.lang.InterruptedException {
10-
Connection connection = null;
14+
1115
ConnectionFactory factory = new ConnectionFactory();
1216
factory.setHost("localhost");
13-
connection = factory.newConnection();
17+
Connection connection = factory.newConnection();
1418
Channel channel = connection.createChannel();
1519

16-
channel.queueDeclare("task_queue", true, false, false, null);
20+
channel.queueDeclare(TASK_QUEUE_NAME, true, false, false, null);
1721
System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
1822

1923
channel.basicQos(1);
2024

2125
QueueingConsumer consumer = new QueueingConsumer(channel);
22-
channel.basicConsume("task_queue", false, consumer);
26+
channel.basicConsume(TASK_QUEUE_NAME, false, consumer);
2327

2428
while (true) {
2529
QueueingConsumer.Delivery delivery = consumer.nextDelivery();
2630
String body = new String(delivery.getBody());
27-
System.out.println(" [x] Received " + body);
28-
29-
Thread.sleep( charCount(body, '.') * 1000);
30-
System.out.println(" [x] Done" );
31+
32+
System.out.println(" [x] Received '" + body + "'");
33+
doWork(body);
34+
System.out.println(" [x] Done");
3135

3236
channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
33-
}
37+
}
3438
}
3539

36-
private static int charCount(String body, char theChar){
37-
int count = 0;
38-
for (int index = 0; index < body.length(); index++){
39-
if (theChar == body.charAt(index)) count++;
40+
private static void doWork(String task) throws InterruptedException {
41+
for (char ch: task.toCharArray()) {
42+
if (ch == '.') Thread.sleep(1000);
43+
}
4044
}
41-
return count;
42-
}
43-
}
44-
45+
}

0 commit comments

Comments
 (0)