Skip to content

Commit b579201

Browse files
author
Ann
committed
Tutorial two for java.
1 parent 9bc8e4c commit b579201

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

java/NewTask.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import com.rabbitmq.client.ConnectionFactory;
2+
import com.rabbitmq.client.Connection;
3+
import com.rabbitmq.client.Channel;
4+
import com.rabbitmq.client.MessageProperties;
5+
6+
public class NewTask {
7+
public static void main(String[] argv)
8+
throws java.io.IOException {
9+
Connection conn = null;
10+
ConnectionFactory factory = new ConnectionFactory();
11+
factory.setHost("localhost");
12+
conn = factory.newConnection();
13+
Channel chan = conn.createChannel();
14+
15+
// make durable
16+
chan.queueDeclare("task_queue", true, false, false, null);
17+
18+
String message = joinStrings(argv);
19+
if (message == "") message = "Hello World!";
20+
21+
// make persistent
22+
chan.basicPublish("", "task_queue", MessageProperties.PERSISTENT_TEXT_PLAIN, message.getBytes());
23+
System.out.println(" [x] Sent '" + message + "'");
24+
chan.close();
25+
conn.close();
26+
}
27+
28+
private static String joinStrings(String[] strings){
29+
String words = "";
30+
for (String astring: strings)
31+
words = words + astring + " ";
32+
return words.trim();
33+
}
34+
}

java/Worker.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import com.rabbitmq.client.ConnectionFactory;
2+
import com.rabbitmq.client.Connection;
3+
import com.rabbitmq.client.Channel;
4+
import com.rabbitmq.client.QueueingConsumer;
5+
6+
public class Worker {
7+
public static void main(String[] argv)
8+
throws java.io.IOException,
9+
java.lang.InterruptedException {
10+
Connection conn = null;
11+
ConnectionFactory factory = new ConnectionFactory();
12+
factory.setHost("localhost");
13+
conn = factory.newConnection();
14+
Channel chan = conn.createChannel();
15+
16+
// make durable
17+
chan.queueDeclare("task_queue", true, false, false, null);
18+
System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
19+
20+
// prefetch 1
21+
chan.basicQos(1);
22+
QueueingConsumer consumer = new QueueingConsumer(chan);
23+
chan.basicConsume("task_queue", false, consumer);
24+
25+
while (true) {
26+
QueueingConsumer.Delivery delivery = consumer.nextDelivery();
27+
String body = new String(delivery.getBody());
28+
System.out.println(" [x] Received " + body);
29+
Thread.sleep(doDots(body)); // simulate action
30+
System.out.println(" [x] Done");
31+
// acknowledge
32+
chan.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
33+
}
34+
}
35+
36+
37+
private static int doDots(String body){
38+
// just do something to get a number for simulation
39+
int x = body.indexOf('.') ;
40+
if (x < 0) return 0;
41+
body = body.substring(x);
42+
return body.length() ;
43+
}
44+
}

0 commit comments

Comments
 (0)