|
17 | 17 |
|
18 | 18 | package com.rabbitmq.client.amqp.impl;
|
19 | 19 |
|
20 |
| -import com.rabbitmq.client.amqp.AmqpException; |
21 |
| -import java.time.Duration; |
22 | 20 | import java.util.*;
|
23 | 21 | import java.util.concurrent.*;
|
24 | 22 | import java.util.concurrent.atomic.AtomicBoolean;
|
25 | 23 | import java.util.concurrent.atomic.AtomicReference;
|
26 |
| -import org.slf4j.Logger; |
27 |
| -import org.slf4j.LoggerFactory; |
28 | 24 |
|
29 | 25 | final class RecordingTopologyListener implements TopologyListener, AutoCloseable {
|
30 | 26 |
|
31 |
| - private static final Duration TIMEOUT = Duration.ofSeconds(60); |
32 |
| - |
33 |
| - private static final Logger LOGGER = LoggerFactory.getLogger(RecordingTopologyListener.class); |
34 |
| - |
| 27 | + private final EventLoop eventLoop; |
35 | 28 | private final Map<String, ExchangeSpec> exchanges = new LinkedHashMap<>();
|
36 | 29 | private final Map<String, QueueSpec> queues = new LinkedHashMap<>();
|
37 | 30 | private final Set<BindingSpec> bindings = new LinkedHashSet<>();
|
38 | 31 | private final Map<Long, ConsumerSpec> consumers = new LinkedHashMap<>();
|
39 |
| - private final BlockingQueue<Runnable> taskQueue = new ArrayBlockingQueue<>(100); |
40 |
| - private final Future<?> loop; |
41 |
| - private final AtomicReference<Thread> loopThread = new AtomicReference<>(); |
42 | 32 | private final AtomicBoolean closed = new AtomicBoolean(false);
|
43 | 33 |
|
44 | 34 | RecordingTopologyListener(ExecutorService executorService) {
|
45 |
| - CountDownLatch loopThreadSetLatch = new CountDownLatch(1); |
46 |
| - this.loop = |
47 |
| - executorService.submit( |
48 |
| - () -> { |
49 |
| - loopThread.set(Thread.currentThread()); |
50 |
| - loopThreadSetLatch.countDown(); |
51 |
| - while (!Thread.currentThread().isInterrupted()) { |
52 |
| - try { |
53 |
| - Runnable task = this.taskQueue.take(); |
54 |
| - task.run(); |
55 |
| - } catch (InterruptedException e) { |
56 |
| - return; |
57 |
| - } catch (Exception e) { |
58 |
| - LOGGER.warn("Error during processing of topology recording task", e); |
59 |
| - } |
60 |
| - } |
61 |
| - }); |
62 |
| - try { |
63 |
| - if (!loopThreadSetLatch.await(10, TimeUnit.SECONDS)) { |
64 |
| - throw new IllegalStateException("Recording topology loop could not start"); |
65 |
| - } |
66 |
| - } catch (InterruptedException e) { |
67 |
| - Thread.currentThread().interrupt(); |
68 |
| - throw new AmqpException("Error while creating recording topology listener", e); |
69 |
| - } |
| 35 | + this.eventLoop = new EventLoop("topology", executorService); |
70 | 36 | }
|
71 | 37 |
|
72 | 38 | @Override
|
@@ -139,44 +105,13 @@ public void consumerDeleted(long id, String queue) {
|
139 | 105 | @Override
|
140 | 106 | public void close() {
|
141 | 107 | if (this.closed.compareAndSet(false, true)) {
|
142 |
| - this.loop.cancel(true); |
| 108 | + this.eventLoop.close(); |
143 | 109 | }
|
144 | 110 | }
|
145 | 111 |
|
146 | 112 | private void submit(Runnable task) {
|
147 | 113 | if (!this.closed.get()) {
|
148 |
| - if (Thread.currentThread().equals(this.loopThread.get())) { |
149 |
| - task.run(); |
150 |
| - } else { |
151 |
| - CountDownLatch latch = new CountDownLatch(1); |
152 |
| - try { |
153 |
| - boolean added = |
154 |
| - this.taskQueue.offer( |
155 |
| - () -> { |
156 |
| - try { |
157 |
| - task.run(); |
158 |
| - } catch (Exception e) { |
159 |
| - LOGGER.info("Error during topology recording task", e); |
160 |
| - } finally { |
161 |
| - latch.countDown(); |
162 |
| - } |
163 |
| - }, |
164 |
| - TIMEOUT.toMillis(), |
165 |
| - TimeUnit.MILLISECONDS); |
166 |
| - if (!added) { |
167 |
| - throw new AmqpException("Enqueueing of topology task timed out"); |
168 |
| - } |
169 |
| - } catch (InterruptedException e) { |
170 |
| - Thread.currentThread().interrupt(); |
171 |
| - throw new AmqpException("Topology task enqueueing has been interrupted", e); |
172 |
| - } |
173 |
| - try { |
174 |
| - latch.await(TIMEOUT.toMillis(), TimeUnit.MILLISECONDS); |
175 |
| - } catch (InterruptedException e) { |
176 |
| - Thread.currentThread().interrupt(); |
177 |
| - throw new AmqpException("Topology task processing has been interrupted", e); |
178 |
| - } |
179 |
| - } |
| 114 | + this.eventLoop.submit(task); |
180 | 115 | }
|
181 | 116 | }
|
182 | 117 |
|
|
0 commit comments