-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathWorkerProcess.java
More file actions
35 lines (29 loc) · 1.08 KB
/
WorkerProcess.java
File metadata and controls
35 lines (29 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import com.rabbitmq.client.*;
import java.io.IOException;
public class WorkerProcess {
private final static String QUEUE_NAME = "hello";
public static void main(String[] argv) throws Exception {
String uri = System.getenv("CLOUDAMQP_URL");
if (uri == null)
uri = "amqp://guest:guest@localhost";
ConnectionFactory factory = new ConnectionFactory();
factory.setUri(uri);
factory.setConnectionTimeout(30000);
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
System.out.println(" [*] Waiting for messages");
DefaultConsumer consumer = new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag,
Envelope envelope,
AMQP.BasicProperties properties,
byte[] body)
throws IOException {
String message = new String(body);
System.out.println(" [x] Received '" + message + "'");
}
};
channel.basicConsume(QUEUE_NAME, true, consumer);
}
}