Search in sources :

Example 1 with AutorecoveringChannel

use of com.rabbitmq.client.impl.recovery.AutorecoveringChannel in project LinkAgent by shulieTech.

the class ChannelNProcessDeliveryInterceptor method getAllConsumersFromConnection.

private List<ConsumerDetail> getAllConsumersFromConnection(Connection connection) {
    List<ConsumerDetail> consumerDetails = new ArrayList<ConsumerDetail>();
    Set<Channel> channels = new HashSet<Channel>();
    if (connection instanceof AMQConnection) {
        ChannelManager _channelManager = Reflect.on(connection).get("_channelManager");
        Map<Integer, ChannelN> _channelMap = Reflect.on(_channelManager).get("_channelMap");
        channels.addAll(_channelMap.values());
    } else if (connection instanceof AutorecoveringConnection) {
        Map<Integer, AutorecoveringChannel> _channels = Reflect.on(connection).get("channels");
        channels.addAll(_channels.values());
    } else {
        logger.error("[RabbitMQ] SIMULATOR unsupport rabbitmqConnection");
    }
    AMQConnection amqConnection = RabbitMqUtils.unWrapConnection(connection);
    SocketFrameHandler frameHandler = Reflect.on(amqConnection).get("_frameHandler");
    String localIp = frameHandler.getLocalAddress().getHostAddress();
    if (isLocalHost(localIp)) {
        localIp = AddressUtils.getLocalAddress();
        logger.warn("[RabbitMQ] SIMULATOR get localIp from connection is localIp use {} instead", localIp);
    }
    int localPort = frameHandler.getLocalPort();
    for (Channel channel : channels) {
        ChannelN channelN = RabbitMqUtils.unWrapChannel(channel);
        Map<String, Consumer> _consumers = Reflect.on(channelN).get("_consumers");
        for (Entry<String, Consumer> entry : _consumers.entrySet()) {
            consumerDetails.add(new ConsumerDetail(connection, entry.getKey(), channel, entry.getValue(), localIp, localPort));
        }
    }
    return consumerDetails;
}
Also used : ChannelManager(com.rabbitmq.client.impl.ChannelManager) AMQConnection(com.rabbitmq.client.impl.AMQConnection) Channel(com.rabbitmq.client.Channel) AutorecoveringChannel(com.rabbitmq.client.impl.recovery.AutorecoveringChannel) ArrayList(java.util.ArrayList) ConsumerDetail(com.pamirs.attach.plugin.rabbitmq.common.ConsumerDetail) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Consumer(com.rabbitmq.client.Consumer) ChannelN(com.rabbitmq.client.impl.ChannelN) Map(java.util.Map) HashMap(java.util.HashMap) SocketFrameHandler(com.rabbitmq.client.impl.SocketFrameHandler) AutorecoveringConnection(com.rabbitmq.client.impl.recovery.AutorecoveringConnection) HashSet(java.util.HashSet)

Example 2 with AutorecoveringChannel

use of com.rabbitmq.client.impl.recovery.AutorecoveringChannel in project LinkAgent by shulieTech.

the class AutorecoveringChannelConsumerMetaDataBuilder method tryBuild.

@Override
public ConsumerMetaData tryBuild(ConsumerDetail consumerDetail) {
    String consumerTag = consumerDetail.getConsumerTag();
    Consumer consumer = consumerDetail.getConsumer();
    Channel channel = unWrapChannel(consumerDetail.getChannel(), consumerTag, consumer);
    if (!(channel instanceof AutorecoveringChannel)) {
        return null;
    }
    /*
            如果一个connection有多个channel,这多个channel之间又有相同的consumer tag订阅不同的queue,那么这里就有问题。
            因为consumer_tag对于connection来说不是唯一的,对于channel才是唯一的,最早订阅的conusmer会被覆盖
            rabbitmq client的内部实现就是有bug,see : AutorecoveringConnection#recordConsumer,
            所以应该不太会出现重复consumer tag的情况,这里就先不考虑相同consumer tag在同一个connection的情况
         */
    RecordedConsumer recordedConsumer = getRecordedConsumer(channel, consumerTag);
    return new ConsumerMetaData((String) Reflect.on(recordedConsumer).get("queue"), consumerTag, consumer, (Boolean) Reflect.on(recordedConsumer).get("exclusive"), (Boolean) Reflect.on(recordedConsumer).get("autoAck"), (Integer) Reflect.on(channel).get("prefetchCountConsumer"), false);
}
Also used : RecordedConsumer(com.rabbitmq.client.impl.recovery.RecordedConsumer) DefaultConsumer(com.rabbitmq.client.DefaultConsumer) Consumer(com.rabbitmq.client.Consumer) AutorecoveringChannel(com.rabbitmq.client.impl.recovery.AutorecoveringChannel) Channel(com.rabbitmq.client.Channel) AutorecoveringChannel(com.rabbitmq.client.impl.recovery.AutorecoveringChannel) RecordedConsumer(com.rabbitmq.client.impl.recovery.RecordedConsumer)

Aggregations

Channel (com.rabbitmq.client.Channel)2 Consumer (com.rabbitmq.client.Consumer)2 AutorecoveringChannel (com.rabbitmq.client.impl.recovery.AutorecoveringChannel)2 ConsumerDetail (com.pamirs.attach.plugin.rabbitmq.common.ConsumerDetail)1 DefaultConsumer (com.rabbitmq.client.DefaultConsumer)1 AMQConnection (com.rabbitmq.client.impl.AMQConnection)1 ChannelManager (com.rabbitmq.client.impl.ChannelManager)1 ChannelN (com.rabbitmq.client.impl.ChannelN)1 SocketFrameHandler (com.rabbitmq.client.impl.SocketFrameHandler)1 AutorecoveringConnection (com.rabbitmq.client.impl.recovery.AutorecoveringConnection)1 RecordedConsumer (com.rabbitmq.client.impl.recovery.RecordedConsumer)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1