Skip to content

Commit 703eaff

Browse files
author
Rob Harrop
committed
A few examples for testing per-queue ttl, especially over broker restarts
1 parent 3b3a332 commit 703eaff

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.rabbitmq.examples;
2+
3+
import com.rabbitmq.client.Channel;
4+
import com.rabbitmq.client.Connection;
5+
import com.rabbitmq.client.ConnectionFactory;
6+
import com.rabbitmq.client.GetResponse;
7+
8+
/**
9+
* @author robharrop
10+
*/
11+
public class PerQueueTTLGetter {
12+
13+
public static void main(String[] args) throws Exception {
14+
ConnectionFactory factory = new ConnectionFactory();
15+
Connection connection = factory.newConnection();
16+
Channel channel = connection.createChannel();
17+
18+
String queue = "ttl.queue";
19+
20+
// exchange
21+
GetResponse response = channel.basicGet(queue, false);
22+
if(response == null) {
23+
System.out.println("Got no message...");
24+
} else {
25+
System.out.println("Got message: " + new String(response.getBody()));
26+
channel.basicAck(response.getEnvelope().getDeliveryTag(), false);
27+
}
28+
channel.close();
29+
connection.close();
30+
}
31+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.rabbitmq.examples;
2+
3+
import com.rabbitmq.client.AMQP;
4+
import com.rabbitmq.client.Channel;
5+
import com.rabbitmq.client.Connection;
6+
import com.rabbitmq.client.ConnectionFactory;
7+
8+
import java.util.Collections;
9+
10+
/**
11+
* @author robharrop
12+
*/
13+
public class PerQueueTTLPublisher {
14+
15+
public static void main(String[] args) throws Exception {
16+
ConnectionFactory factory = new ConnectionFactory();
17+
Connection connection = factory.newConnection();
18+
Channel channel = connection.createChannel();
19+
20+
String exchange = "ttl.exchange";
21+
String queue = "ttl.queue";
22+
23+
// exchange
24+
channel.exchangeDeclare(exchange, "direct");
25+
26+
// queue
27+
channel.queueDeclare(queue, true, false, false, Collections.singletonMap("x-message-ttl", (Object) 30000L));
28+
channel.queueBind(queue, exchange, queue, null);
29+
30+
// send a message
31+
AMQP.BasicProperties props = new AMQP.BasicProperties();
32+
props.setDeliveryMode(2);
33+
for(int x = 0; x < 10; x++) {
34+
channel.basicPublish(exchange, queue, props, ("Msg [" + x + "]").getBytes());
35+
}
36+
37+
System.out.println("Done");
38+
channel.close();
39+
connection.close();
40+
}
41+
}

0 commit comments

Comments
 (0)