Skip to content

Commit cc9e4fd

Browse files
committed
Create Queue only when required, add durable arg to construction.
Values: 1 or 0. Required for queue creation, skipped for existing queues.
1 parent 8f55d09 commit cc9e4fd

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

src/isc/rabbitmq/API.java

+26-4
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,46 @@
1313
* Created by eduard on 06.10.2017.
1414
*/
1515
public class API {
16-
private final Channel _channel;
16+
private Channel _channel;
1717

1818
private final String _queue;
1919

2020
private final Connection _connection;
2121

22-
public API(String host, int port, String user, String pass, String virtualHost, String queue) throws Exception {
22+
public API(String host, int port, String user, String pass, String virtualHost, String queue, int durable) throws Exception {
2323
ConnectionFactory factory = new ConnectionFactory();
2424
factory.setHost(host);
2525
factory.setPort(port);
2626
factory.setUsername(user);
2727
factory.setPassword(pass);
2828
factory.setVirtualHost(virtualHost);
29-
29+
//factory.setAutomaticRecoveryEnabled(true);
3030

3131
_connection = factory.newConnection();
3232
_channel = _connection.createChannel();
33-
_channel.queueDeclare(queue, false, false, false, null);
33+
try {
34+
// Check that queue exists
35+
// Method throws exception if queue does not exist or is exclusive
36+
// Correct exception text: channel error; protocol method: #method<channel.close>(reply-code=404, reply-text=NOT_FOUND - no queue 'queue'
37+
com.rabbitmq.client.AMQP.Queue.DeclareOk declareOk = _channel.queueDeclarePassive(queue);
38+
} catch (java.io.IOException ex) {
39+
// Exception closes the channel.
40+
// So we need to create new one.
41+
// _channel.basicRecover() doesn't do the trick
42+
_channel = _connection.createChannel();
43+
44+
Boolean durableBool = (durable != 0);
45+
Boolean exclusive = false;
46+
Boolean autoDelete = false;
47+
// queue - the name of the queue
48+
// durable - true if we are declaring a durable queue (the queue will survive a server restart)
49+
// exclusive - true if we are declaring an exclusive queue (restricted to this connection)
50+
// autoDelete - true if we are declaring an autodelete queue (server will delete it when no longer in use)
51+
// arguments - other properties (construction arguments) for the queue
52+
_channel.queueDeclare(queue, durableBool, exclusive, autoDelete, null);
53+
54+
}
55+
3456
_queue = queue;
3557
}
3658

0 commit comments

Comments
 (0)