|
21 | 21 | import java.net.InetAddress; |
22 | 22 | import java.net.SocketException; |
23 | 23 | import java.net.SocketTimeoutException; |
| 24 | +import java.util.Collection; |
24 | 25 | import java.util.Collections; |
25 | 26 | import java.util.HashMap; |
26 | 27 | import java.util.Map; |
| 28 | +import java.util.concurrent.CopyOnWriteArrayList; |
27 | 29 | import java.util.concurrent.ExecutorService; |
28 | 30 | import java.util.concurrent.TimeoutException; |
29 | 31 |
|
30 | 32 | import com.rabbitmq.client.AMQP; |
| 33 | +import com.rabbitmq.client.BlockedListener; |
31 | 34 | import com.rabbitmq.client.Method; |
32 | 35 | import com.rabbitmq.client.AlreadyClosedException; |
33 | 36 | import com.rabbitmq.client.Channel; |
@@ -81,6 +84,7 @@ public static final Map<String, Object> defaultClientProperties() { |
81 | 84 | capabilities.put("exchange_exchange_bindings", true); |
82 | 85 | capabilities.put("basic.nack", true); |
83 | 86 | capabilities.put("consumer_cancel_notify", true); |
| 87 | + capabilities.put("connection.blocked", true); |
84 | 88 |
|
85 | 89 | props.put("capabilities", capabilities); |
86 | 90 |
|
@@ -130,6 +134,7 @@ public static final Map<String, Object> defaultClientProperties() { |
130 | 134 | private final int requestedFrameMax; |
131 | 135 | private final String username; |
132 | 136 | private final String password; |
| 137 | + private final Collection<BlockedListener> blockedListeners = new CopyOnWriteArrayList<BlockedListener>(); |
133 | 138 |
|
134 | 139 | /* State modified after start - all volatile */ |
135 | 140 |
|
@@ -597,6 +602,25 @@ public boolean processControlCommand(Command c) throws IOException |
597 | 602 | if (method instanceof AMQP.Connection.Close) { |
598 | 603 | handleConnectionClose(c); |
599 | 604 | return true; |
| 605 | + } else if (method instanceof AMQP.Connection.Blocked) { |
| 606 | + AMQP.Connection.Blocked blocked = (AMQP.Connection.Blocked) method; |
| 607 | + try { |
| 608 | + for (BlockedListener l : this.blockedListeners) { |
| 609 | + l.handleBlocked(blocked.getReason()); |
| 610 | + } |
| 611 | + } catch (Throwable ex) { |
| 612 | + getExceptionHandler().handleBlockedListenerException(this, ex); |
| 613 | + } |
| 614 | + return true; |
| 615 | + } else if (method instanceof AMQP.Connection.Unblocked) { |
| 616 | + try { |
| 617 | + for (BlockedListener l : this.blockedListeners) { |
| 618 | + l.handleUnblocked(); |
| 619 | + } |
| 620 | + } catch (Throwable ex) { |
| 621 | + getExceptionHandler().handleBlockedListenerException(this, ex); |
| 622 | + } |
| 623 | + return true; |
600 | 624 | } else { |
601 | 625 | return false; |
602 | 626 | } |
@@ -823,4 +847,16 @@ public AMQCommand transformReply(AMQCommand command) { |
823 | 847 | private String getHostAddress() { |
824 | 848 | return getAddress() == null ? null : getAddress().getHostAddress(); |
825 | 849 | } |
| 850 | + |
| 851 | + public void addBlockedListener(BlockedListener listener) { |
| 852 | + blockedListeners.add(listener); |
| 853 | + } |
| 854 | + |
| 855 | + public boolean removeBlockedListener(BlockedListener listener) { |
| 856 | + return blockedListeners.remove(listener); |
| 857 | + } |
| 858 | + |
| 859 | + public void clearBlockedListeners() { |
| 860 | + blockedListeners.clear(); |
| 861 | + } |
826 | 862 | } |
0 commit comments