|
13 | 13 | * Created by eduard on 06.10.2017.
|
14 | 14 | */
|
15 | 15 | public class API {
|
16 |
| - private final Channel _channel; |
| 16 | + private Channel _channel; |
17 | 17 |
|
18 | 18 | private final String _queue;
|
19 | 19 |
|
20 | 20 | private final Connection _connection;
|
21 | 21 |
|
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 { |
23 | 23 | ConnectionFactory factory = new ConnectionFactory();
|
24 | 24 | factory.setHost(host);
|
25 | 25 | factory.setPort(port);
|
26 | 26 | factory.setUsername(user);
|
27 | 27 | factory.setPassword(pass);
|
28 | 28 | factory.setVirtualHost(virtualHost);
|
29 |
| - |
| 29 | + //factory.setAutomaticRecoveryEnabled(true); |
30 | 30 |
|
31 | 31 | _connection = factory.newConnection();
|
32 | 32 | _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 | + |
34 | 56 | _queue = queue;
|
35 | 57 | }
|
36 | 58 |
|
|
0 commit comments