|
| 1 | +/* |
| 2 | + * Copyright 2018 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.amqp.rabbit.support; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.util.concurrent.ConcurrentHashMap; |
| 21 | +import java.util.concurrent.ConcurrentMap; |
| 22 | +import java.util.concurrent.TimeoutException; |
| 23 | + |
| 24 | +import org.apache.commons.logging.Log; |
| 25 | +import org.apache.commons.logging.LogFactory; |
| 26 | + |
| 27 | +import org.springframework.amqp.rabbit.connection.ChannelProxy; |
| 28 | + |
| 29 | +import com.rabbitmq.client.Channel; |
| 30 | +import com.rabbitmq.client.Recoverable; |
| 31 | +import com.rabbitmq.client.RecoveryListener; |
| 32 | +import com.rabbitmq.client.impl.recovery.AutorecoveringChannel; |
| 33 | + |
| 34 | +/** |
| 35 | + * A {@link RecoveryListener} that closes the recovered channel, to avoid |
| 36 | + * orphaned consumers. |
| 37 | + * |
| 38 | + * @author Gary Russell |
| 39 | + * @since 1.7.10 |
| 40 | + * |
| 41 | + */ |
| 42 | +public final class ClosingRecoveryListener implements RecoveryListener { |
| 43 | + |
| 44 | + private static final Log logger = LogFactory.getLog(ClosingRecoveryListener.class); |
| 45 | + |
| 46 | + private static final RecoveryListener INSTANCE = new ClosingRecoveryListener(); |
| 47 | + |
| 48 | + private static final ConcurrentMap<AutorecoveringChannel, Boolean> hasListener = |
| 49 | + new ConcurrentHashMap<AutorecoveringChannel, Boolean>(); |
| 50 | + |
| 51 | + private ClosingRecoveryListener() { |
| 52 | + super(); |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public void handleRecovery(Recoverable recoverable) { |
| 57 | + // should never get here |
| 58 | + handleRecoveryStarted(recoverable); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public void handleRecoveryStarted(Recoverable recoverable) { |
| 63 | + if (logger.isDebugEnabled()) { |
| 64 | + logger.debug("Closing an autorecovered channel: " + recoverable); |
| 65 | + } |
| 66 | + try { |
| 67 | + ((Channel) recoverable).close(); |
| 68 | + } |
| 69 | + catch (TimeoutException e) { |
| 70 | + logger.error("Error closing an autorecovered channel", e); |
| 71 | + } |
| 72 | + catch (IOException e) { |
| 73 | + logger.error("Error closing an autorecovered channel", e); |
| 74 | + } |
| 75 | + finally { |
| 76 | + hasListener.remove(recoverable); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Add a listener if necessary so we can immediately close an autorecovered |
| 82 | + * channel if necessary since the actual consumer will no longer exist. |
| 83 | + * Idempotent operation. |
| 84 | + * @param channel the channel. |
| 85 | + */ |
| 86 | + public static void addRecoveryListenerIfNecessary(Channel channel) { |
| 87 | + AutorecoveringChannel autorecoveringChannel = null; |
| 88 | + if (channel instanceof ChannelProxy) { |
| 89 | + if (((ChannelProxy) channel).getTargetChannel() instanceof AutorecoveringChannel) { |
| 90 | + autorecoveringChannel = (AutorecoveringChannel) ((ChannelProxy) channel) |
| 91 | + .getTargetChannel(); |
| 92 | + } |
| 93 | + } |
| 94 | + else if (channel instanceof AutorecoveringChannel) { |
| 95 | + autorecoveringChannel = (AutorecoveringChannel) channel; |
| 96 | + } |
| 97 | + if (autorecoveringChannel != null |
| 98 | + && hasListener.putIfAbsent(autorecoveringChannel, Boolean.TRUE) == null) { |
| 99 | + autorecoveringChannel.addRecoveryListener(INSTANCE); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Remove the channel from the set used to ensure that |
| 105 | + * {@link #addRecoveryListenerIfNecessary(Channel)} is idempotent. |
| 106 | + * @param channel the channel to remove. |
| 107 | + */ |
| 108 | + public static void removeChannel(AutorecoveringChannel channel) { |
| 109 | + hasListener.remove(channel); |
| 110 | + } |
| 111 | + |
| 112 | +} |
0 commit comments