Search in sources :

Example 1 with ConsumerDetail

use of com.pamirs.attach.plugin.rabbitmq.common.ConsumerDetail 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 ConsumerDetail

use of com.pamirs.attach.plugin.rabbitmq.common.ConsumerDetail in project LinkAgent by shulieTech.

the class ChannelNProcessDeliveryInterceptor method afterLast.

@Override
public void afterLast(final Advice advice) {
    Channel channel = (Channel) advice.getTarget();
    Connection connection = channel.getConnection();
    Object[] args = advice.getParameterArray();
    AMQP.Basic.Deliver m = (AMQP.Basic.Deliver) args[1];
    String consumerTag = m.getConsumerTag();
    if (Pradar.isClusterTestPrefix(consumerTag) || ChannelHolder.existsConsumer(consumerTag)) {
        return;
    }
    // 每个connection只进来一次,一次流量带所有的影子消费者,如果失败异步任务会自动重试
    OnceExecutor.execute(connection, new OnceExecutor.Consumer<Connection>() {

        @Override
        public void accept(Connection connection) {
            List<ConsumerDetail> consumerDetails = getAllConsumersFromConnection(connection);
            for (ConsumerDetail consumerDetail : consumerDetails) {
                THREAD_POOL_EXECUTOR.execute(new ShadowConsumerRegisterRunnable(consumerDetail));
            }
        }
    });
}
Also used : Deliver(com.rabbitmq.client.impl.AMQImpl.Basic.Deliver) Channel(com.rabbitmq.client.Channel) AutorecoveringChannel(com.rabbitmq.client.impl.recovery.AutorecoveringChannel) Connection(com.rabbitmq.client.Connection) AutorecoveringConnection(com.rabbitmq.client.impl.recovery.AutorecoveringConnection) AMQConnection(com.rabbitmq.client.impl.AMQConnection) OnceExecutor(com.pamirs.attach.plugin.rabbitmq.utils.OnceExecutor) ConsumerDetail(com.pamirs.attach.plugin.rabbitmq.common.ConsumerDetail) AMQP(com.rabbitmq.client.AMQP) List(java.util.List) ArrayList(java.util.ArrayList)

Aggregations

ConsumerDetail (com.pamirs.attach.plugin.rabbitmq.common.ConsumerDetail)2 Channel (com.rabbitmq.client.Channel)2 AMQConnection (com.rabbitmq.client.impl.AMQConnection)2 AutorecoveringChannel (com.rabbitmq.client.impl.recovery.AutorecoveringChannel)2 AutorecoveringConnection (com.rabbitmq.client.impl.recovery.AutorecoveringConnection)2 ArrayList (java.util.ArrayList)2 OnceExecutor (com.pamirs.attach.plugin.rabbitmq.utils.OnceExecutor)1 AMQP (com.rabbitmq.client.AMQP)1 Connection (com.rabbitmq.client.Connection)1 Consumer (com.rabbitmq.client.Consumer)1 Deliver (com.rabbitmq.client.impl.AMQImpl.Basic.Deliver)1 ChannelManager (com.rabbitmq.client.impl.ChannelManager)1 ChannelN (com.rabbitmq.client.impl.ChannelN)1 SocketFrameHandler (com.rabbitmq.client.impl.SocketFrameHandler)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1