tidy up the code for tut 2
Signed-off-by: Ann <awitbrock@vmware.com>
This commit is contained in:
parent
8f4b1edb82
commit
914f22cd21
@ -4,32 +4,36 @@ import com.rabbitmq.client.Channel;
|
||||
import com.rabbitmq.client.MessageProperties;
|
||||
|
||||
public class NewTask {
|
||||
public static void main(String[] argv) throws java.io.IOException {
|
||||
Connection connection = null;
|
||||
ConnectionFactory factory = new ConnectionFactory();
|
||||
factory.setHost("localhost");
|
||||
connection = factory.newConnection();
|
||||
Channel channel = connection.createChannel();
|
||||
|
||||
boolean durable = true;
|
||||
channel.queueDeclare("task_queue", durable, false, false, null);
|
||||
|
||||
String message = joinStrings(argv);
|
||||
if (message == "") message = "Hello World!";
|
||||
|
||||
// make message props persistent
|
||||
channel.basicPublish( "", "task_queue",
|
||||
MessageProperties.PERSISTENT_TEXT_PLAIN,
|
||||
public static void main(String[] argv) throws java.io.IOException {
|
||||
Connection connection = null;
|
||||
ConnectionFactory factory = new ConnectionFactory();
|
||||
factory.setHost("localhost");
|
||||
connection = factory.newConnection();
|
||||
Channel channel = connection.createChannel();
|
||||
|
||||
channel.queueDeclare("task_queue", true, false, false, null);
|
||||
|
||||
String message = getMessage(argv);
|
||||
|
||||
channel.basicPublish( "", "task_queue",
|
||||
MessageProperties.PERSISTENT_TEXT_PLAIN,
|
||||
message.getBytes());
|
||||
System.out.println(" [x] Sent '" + message + "'");
|
||||
channel.close();
|
||||
connection.close();
|
||||
}
|
||||
System.out.println(" [x] Sent '" + message + "'");
|
||||
channel.close();
|
||||
connection.close();
|
||||
}
|
||||
|
||||
private static String getMessage(String[] strings){
|
||||
if (strings.length < 1)
|
||||
return "Hello World!";
|
||||
return joinStrings(strings);
|
||||
}
|
||||
|
||||
private static String joinStrings(String[] strings){
|
||||
String words = "";
|
||||
for (String astring: strings)
|
||||
words = words + astring + " ";
|
||||
return words.trim();
|
||||
String words = "";
|
||||
for (String astring: strings)
|
||||
words = words.concat(astring).concat(" ");
|
||||
return words.trim();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -2,46 +2,43 @@ import com.rabbitmq.client.ConnectionFactory;
|
||||
import com.rabbitmq.client.Connection;
|
||||
import com.rabbitmq.client.Channel;
|
||||
import com.rabbitmq.client.QueueingConsumer;
|
||||
|
||||
|
||||
public class Worker {
|
||||
public static void main(String[] argv)
|
||||
throws java.io.IOException,
|
||||
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();
|
||||
Channel channel = connection.createChannel();
|
||||
Connection connection = null;
|
||||
ConnectionFactory factory = new ConnectionFactory();
|
||||
factory.setHost("localhost");
|
||||
connection = factory.newConnection();
|
||||
Channel channel = connection.createChannel();
|
||||
|
||||
channel.queueDeclare("task_queue", 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);
|
||||
|
||||
while (true) {
|
||||
QueueingConsumer.Delivery delivery = consumer.nextDelivery();
|
||||
String body = new String(delivery.getBody());
|
||||
System.out.println(" [x] Received " + body);
|
||||
|
||||
boolean durable = true;
|
||||
channel.queueDeclare("task_queue", durable, false, false, null);
|
||||
System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
|
||||
Thread.sleep( charCount(body, '.') * 1000);
|
||||
System.out.println(" [x] Done" );
|
||||
|
||||
int prefetchCount = 1;
|
||||
channel.basicQos(prefetchCount);
|
||||
|
||||
QueueingConsumer consumer = new QueueingConsumer(channel);
|
||||
boolean autoAck = false;
|
||||
channel.basicConsume("task_queue", autoAck, consumer);
|
||||
|
||||
while (true) {
|
||||
QueueingConsumer.Delivery delivery = consumer.nextDelivery();
|
||||
String body = new String(delivery.getBody());
|
||||
System.out.println(" [x] Received " + body);
|
||||
Thread.sleep(doDots(body)); // simulate action
|
||||
System.out.println(" [x] Done");
|
||||
// acknowledge
|
||||
channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
|
||||
}
|
||||
channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
|
||||
}
|
||||
|
||||
|
||||
|
||||
private static int doDots(String body){
|
||||
// just do something to get a number for simulation
|
||||
int x = body.indexOf('.') ;
|
||||
if (x < 0) return 0;
|
||||
body = body.substring(x);
|
||||
return body.length() ;
|
||||
}
|
||||
|
||||
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++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user