QA fixes: use StringBuilder, doWork, constant queue name
This commit is contained in:
parent
ce8c6c6dad
commit
6b1e114575
@ -1,24 +1,29 @@
|
||||
import java.io.IOException;
|
||||
import com.rabbitmq.client.ConnectionFactory;
|
||||
import com.rabbitmq.client.Connection;
|
||||
import com.rabbitmq.client.Channel;
|
||||
import com.rabbitmq.client.MessageProperties;
|
||||
|
||||
public class NewTask {
|
||||
|
||||
private static final String TASK_QUEUE_NAME = "task_queue";
|
||||
|
||||
public static void main(String[] argv) throws java.io.IOException {
|
||||
Connection connection = null;
|
||||
|
||||
ConnectionFactory factory = new ConnectionFactory();
|
||||
factory.setHost("localhost");
|
||||
connection = factory.newConnection();
|
||||
Connection connection = factory.newConnection();
|
||||
Channel channel = connection.createChannel();
|
||||
|
||||
channel.queueDeclare("task_queue", true, false, false, null);
|
||||
channel.queueDeclare(TASK_QUEUE_NAME, true, false, false, null);
|
||||
|
||||
String message = getMessage(argv);
|
||||
|
||||
channel.basicPublish( "", "task_queue",
|
||||
channel.basicPublish( "", TASK_QUEUE_NAME,
|
||||
MessageProperties.PERSISTENT_TEXT_PLAIN,
|
||||
message.getBytes());
|
||||
System.out.println(" [x] Sent '" + message + "'");
|
||||
|
||||
channel.close();
|
||||
connection.close();
|
||||
}
|
||||
@ -26,14 +31,16 @@ public class NewTask {
|
||||
private static String getMessage(String[] strings){
|
||||
if (strings.length < 1)
|
||||
return "Hello World!";
|
||||
return joinStrings(strings);
|
||||
return joinStrings(strings, " ");
|
||||
}
|
||||
|
||||
private static String joinStrings(String[] strings){
|
||||
String words = "";
|
||||
for (String astring: strings)
|
||||
words = words.concat(astring).concat(" ");
|
||||
return words.trim();
|
||||
private static String joinStrings(String[] strings, String delimiter) {
|
||||
int length = strings.length;
|
||||
if (length == 0) return "";
|
||||
StringBuilder words = new StringBuilder(strings[0]);
|
||||
for (int i = 1; i < length; i++) {
|
||||
words.append(delimiter).append(strings[i]);
|
||||
}
|
||||
return words.toString();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -1,44 +1,45 @@
|
||||
import java.io.IOException;
|
||||
import com.rabbitmq.client.ConnectionFactory;
|
||||
import com.rabbitmq.client.Connection;
|
||||
import com.rabbitmq.client.Channel;
|
||||
import com.rabbitmq.client.QueueingConsumer;
|
||||
|
||||
public class Worker {
|
||||
|
||||
private static final String TASK_QUEUE_NAME = "task_queue";
|
||||
|
||||
public static void main(String[] argv)
|
||||
throws java.io.IOException,
|
||||
java.lang.InterruptedException {
|
||||
Connection connection = null;
|
||||
|
||||
ConnectionFactory factory = new ConnectionFactory();
|
||||
factory.setHost("localhost");
|
||||
connection = factory.newConnection();
|
||||
Connection connection = factory.newConnection();
|
||||
Channel channel = connection.createChannel();
|
||||
|
||||
channel.queueDeclare("task_queue", true, false, false, null);
|
||||
channel.queueDeclare(TASK_QUEUE_NAME, true, false, false, null);
|
||||
System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
|
||||
|
||||
channel.basicQos(1);
|
||||
|
||||
QueueingConsumer consumer = new QueueingConsumer(channel);
|
||||
channel.basicConsume("task_queue", false, consumer);
|
||||
channel.basicConsume(TASK_QUEUE_NAME, false, consumer);
|
||||
|
||||
while (true) {
|
||||
QueueingConsumer.Delivery delivery = consumer.nextDelivery();
|
||||
String body = new String(delivery.getBody());
|
||||
System.out.println(" [x] Received " + body);
|
||||
|
||||
Thread.sleep( charCount(body, '.') * 1000);
|
||||
System.out.println(" [x] Done" );
|
||||
|
||||
System.out.println(" [x] Received '" + body + "'");
|
||||
doWork(body);
|
||||
System.out.println(" [x] Done");
|
||||
|
||||
channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static int charCount(String body, char theChar){
|
||||
int count = 0;
|
||||
for (int index = 0; index < body.length(); index++){
|
||||
if (theChar == body.charAt(index)) count++;
|
||||
private static void doWork(String task) throws InterruptedException {
|
||||
for (char ch: task.toCharArray()) {
|
||||
if (ch == '.') Thread.sleep(1000);
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user