Search in sources :

Example 1 with FrameHandler

use of com.rabbitmq.client.impl.FrameHandler in project rabbitmq-java-client by rabbitmq.

the class ConnectionFactoryTest method tryNextAddressIfTimeoutExceptionNoAutoRecovery.

// see https://github.com/rabbitmq/rabbitmq-java-client/issues/262
@Test
public void tryNextAddressIfTimeoutExceptionNoAutoRecovery() throws IOException, TimeoutException {
    final AMQConnection connectionThatThrowsTimeout = mock(AMQConnection.class);
    final AMQConnection connectionThatSucceeds = mock(AMQConnection.class);
    final Queue<AMQConnection> connections = new ArrayBlockingQueue<AMQConnection>(10);
    connections.add(connectionThatThrowsTimeout);
    connections.add(connectionThatSucceeds);
    ConnectionFactory connectionFactory = new ConnectionFactory() {

        @Override
        protected AMQConnection createConnection(ConnectionParams params, FrameHandler frameHandler, MetricsCollector metricsCollector) {
            return connections.poll();
        }

        @Override
        protected synchronized FrameHandlerFactory createFrameHandlerFactory() throws IOException {
            return mock(FrameHandlerFactory.class);
        }
    };
    connectionFactory.setAutomaticRecoveryEnabled(false);
    doThrow(TimeoutException.class).when(connectionThatThrowsTimeout).start();
    doNothing().when(connectionThatSucceeds).start();
    Connection returnedConnection = connectionFactory.newConnection(new Address[] { new Address("host1"), new Address("host2") });
    assertSame(connectionThatSucceeds, returnedConnection);
}
Also used : MetricsCollector(com.rabbitmq.client.MetricsCollector) FrameHandler(com.rabbitmq.client.impl.FrameHandler) ConnectionFactory(com.rabbitmq.client.ConnectionFactory) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) Address(com.rabbitmq.client.Address) AMQConnection(com.rabbitmq.client.impl.AMQConnection) AMQConnection(com.rabbitmq.client.impl.AMQConnection) Connection(com.rabbitmq.client.Connection) ConnectionParams(com.rabbitmq.client.impl.ConnectionParams) Test(org.junit.Test)

Example 2 with FrameHandler

use of com.rabbitmq.client.impl.FrameHandler in project pinpoint by naver.

the class RabbitMQConsumerHandleCompleteInboundCommandInterceptor method recordRootSpan.

private void recordRootSpan(SpanRecorder recorder, AMQConnection amqConnection, String exchange, String routingKey, Map<String, Object> headers) {
    recorder.recordServiceType(RabbitMQClientConstants.RABBITMQ_CLIENT);
    recorder.recordApi(CONSUMER_ENTRY_METHOD_DESCRIPTOR);
    String endPoint = RabbitMQClientConstants.UNKNOWN;
    String remoteAddress = RabbitMQClientConstants.UNKNOWN;
    if (amqConnection != null) {
        FrameHandler frameHandler = amqConnection.getFrameHandler();
        // Endpoint should be the local socket address of the consumer.
        if (frameHandler instanceof LocalAddressAccessor) {
            endPoint = ((LocalAddressAccessor) frameHandler)._$PINPOINT$_getLocalAddress();
        }
        // Remote address is the socket address of where the consumer is connected to.
        if (frameHandler instanceof RemoteAddressAccessor) {
            remoteAddress = ((RemoteAddressAccessor) frameHandler)._$PINPOINT$_getRemoteAddress();
        }
    }
    recorder.recordEndPoint(endPoint);
    recorder.recordRemoteAddress(remoteAddress);
    String convertedExchange = StringUtils.defaultIfEmpty(exchange, RabbitMQClientConstants.UNKNOWN);
    recorder.recordRpcName("rabbitmq://exchange=" + convertedExchange);
    recorder.recordAcceptorHost("exchange-" + convertedExchange);
    if (isDebug) {
        logger.debug("endPoint={}->{}", exchange, convertedExchange);
    }
    recorder.recordAttribute(RabbitMQClientConstants.RABBITMQ_ROUTINGKEY_ANNOTATION_KEY, routingKey);
    if (MapUtils.hasLength(headers)) {
        Object parentApplicationName = headers.get(RabbitMQClientConstants.META_PARENT_APPLICATION_NAME);
        if (!recorder.isRoot() && parentApplicationName != null) {
            Object parentApplicationType = headers.get(RabbitMQClientConstants.META_PARENT_APPLICATION_TYPE);
            recorder.recordParentApplication(parentApplicationName.toString(), NumberUtils.parseShort(parentApplicationType.toString(), ServiceType.UNDEFINED.getCode()));
        }
    }
}
Also used : LocalAddressAccessor(com.navercorp.pinpoint.plugin.rabbitmq.client.field.accessor.LocalAddressAccessor) FrameHandler(com.rabbitmq.client.impl.FrameHandler) RemoteAddressAccessor(com.navercorp.pinpoint.plugin.rabbitmq.client.field.accessor.RemoteAddressAccessor)

Example 3 with FrameHandler

use of com.rabbitmq.client.impl.FrameHandler in project rabbitmq-java-client by rabbitmq.

the class RecoveryAwareAMQConnectionFactory method newConnection.

/**
 * @return an interface to the connection
 * @throws java.io.IOException if it encounters a problem
 */
// package protected API, made public for testing only
public RecoveryAwareAMQConnection newConnection() throws IOException, TimeoutException {
    Exception lastException = null;
    List<Address> resolved = addressResolver.getAddresses();
    List<Address> shuffled = addressResolver.maybeShuffle(resolved);
    for (Address addr : shuffled) {
        try {
            FrameHandler frameHandler = factory.create(addr, connectionName());
            RecoveryAwareAMQConnection conn = createConnection(params, frameHandler, metricsCollector);
            conn.start();
            metricsCollector.newConnection(conn);
            return conn;
        } catch (IOException e) {
            lastException = e;
        } catch (TimeoutException te) {
            lastException = te;
        }
    }
    if (lastException != null) {
        if (lastException instanceof IOException) {
            throw (IOException) lastException;
        } else if (lastException instanceof TimeoutException) {
            throw (TimeoutException) lastException;
        }
    }
    throw new IOException("failed to connect");
}
Also used : FrameHandler(com.rabbitmq.client.impl.FrameHandler) IOException(java.io.IOException) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) TimeoutException(java.util.concurrent.TimeoutException)

Example 4 with FrameHandler

use of com.rabbitmq.client.impl.FrameHandler in project pinpoint by naver.

the class RabbitMQConsumerDispatchInterceptor method recordRootSpan.

private void recordRootSpan(SpanRecorder recorder, Connection connection, Envelope envelope, Map<String, Object> headers) {
    recorder.recordServiceType(RabbitMQClientConstants.RABBITMQ_CLIENT);
    recorder.recordApi(CONSUMER_ENTRY_METHOD_DESCRIPTOR);
    String endPoint = RabbitMQClientConstants.UNKNOWN;
    String remoteAddress = RabbitMQClientConstants.UNKNOWN;
    if (connection instanceof AMQConnection) {
        AMQConnection amqConnection = (AMQConnection) connection;
        FrameHandler frameHandler = amqConnection.getFrameHandler();
        // Endpoint should be the local socket address of the consumer.
        if (frameHandler instanceof LocalAddressAccessor) {
            endPoint = ((LocalAddressAccessor) frameHandler)._$PINPOINT$_getLocalAddress();
        }
        // Remote address is the socket address of where the consumer is connected to.
        if (frameHandler instanceof RemoteAddressAccessor) {
            remoteAddress = ((RemoteAddressAccessor) frameHandler)._$PINPOINT$_getRemoteAddress();
        }
    }
    recorder.recordEndPoint(endPoint);
    recorder.recordRemoteAddress(remoteAddress);
    String exchange = StringUtils.defaultIfEmpty(envelope.getExchange(), RabbitMQClientConstants.UNKNOWN);
    recorder.recordRpcName("rabbitmq://exchange=" + exchange);
    recorder.recordAcceptorHost("exchange-" + exchange);
    if (isDebug) {
        logger.debug("endPoint={}->{}", envelope.getExchange(), exchange);
    }
    recorder.recordAttribute(RabbitMQClientConstants.RABBITMQ_ROUTINGKEY_ANNOTATION_KEY, envelope.getRoutingKey());
    if (MapUtils.hasLength(headers)) {
        Object parentApplicationName = headers.get(RabbitMQClientConstants.META_PARENT_APPLICATION_NAME);
        if (!recorder.isRoot() && parentApplicationName != null) {
            Object parentApplicationType = headers.get(RabbitMQClientConstants.META_PARENT_APPLICATION_TYPE);
            recorder.recordParentApplication(parentApplicationName.toString(), NumberUtils.parseShort(parentApplicationType.toString(), ServiceType.UNDEFINED.getCode()));
        }
    }
}
Also used : LocalAddressAccessor(com.navercorp.pinpoint.plugin.rabbitmq.client.field.accessor.LocalAddressAccessor) FrameHandler(com.rabbitmq.client.impl.FrameHandler) AMQConnection(com.rabbitmq.client.impl.AMQConnection) RemoteAddressAccessor(com.navercorp.pinpoint.plugin.rabbitmq.client.field.accessor.RemoteAddressAccessor)

Example 5 with FrameHandler

use of com.rabbitmq.client.impl.FrameHandler in project pinpoint by naver.

the class ChannelBasicPublishInterceptor method after.

@Override
public void after(Object target, Object[] args, Object result, Throwable throwable) {
    if (isDebug) {
        logger.afterInterceptor(target, args);
    }
    if (!validate(target, args)) {
        return;
    }
    final Trace trace = traceContext.currentTraceObject();
    if (trace == null) {
        return;
    }
    try {
        String exchange = (String) args[0];
        String routingKey = (String) args[1];
        if (exchange == null || exchange.equals("")) {
            exchange = RabbitMQClientConstants.UNKNOWN;
        }
        SpanEventRecorder recorder = trace.currentSpanEventRecorder();
        recorder.recordApi(descriptor);
        String endPoint = RabbitMQClientConstants.UNKNOWN;
        // Producer's endPoint should be the socket address of where the producer is actually connected to.
        final Connection connection = ((Channel) target).getConnection();
        if (connection instanceof AMQConnection) {
            AMQConnection amqConnection = (AMQConnection) connection;
            FrameHandler frameHandler = amqConnection.getFrameHandler();
            if (frameHandler instanceof RemoteAddressAccessor) {
                endPoint = ((RemoteAddressAccessor) frameHandler)._$PINPOINT$_getRemoteAddress();
            }
        }
        recorder.recordEndPoint(endPoint);
        // DestinationId is used to render the virtual queue node.
        // We choose the exchange name as the logical name of the queue node.
        recorder.recordDestinationId("exchange-" + exchange);
        recorder.recordAttribute(RabbitMQClientConstants.RABBITMQ_EXCHANGE_ANNOTATION_KEY, exchange);
        recorder.recordAttribute(RabbitMQClientConstants.RABBITMQ_ROUTINGKEY_ANNOTATION_KEY, routingKey);
        recorder.recordException(throwable);
    } finally {
        trace.traceBlockEnd();
    }
}
Also used : Trace(com.navercorp.pinpoint.bootstrap.context.Trace) FrameHandler(com.rabbitmq.client.impl.FrameHandler) AMQConnection(com.rabbitmq.client.impl.AMQConnection) SpanEventRecorder(com.navercorp.pinpoint.bootstrap.context.SpanEventRecorder) Channel(com.rabbitmq.client.Channel) AMQConnection(com.rabbitmq.client.impl.AMQConnection) Connection(com.rabbitmq.client.Connection) RemoteAddressAccessor(com.navercorp.pinpoint.plugin.rabbitmq.client.field.accessor.RemoteAddressAccessor)

Aggregations

FrameHandler (com.rabbitmq.client.impl.FrameHandler)6 RemoteAddressAccessor (com.navercorp.pinpoint.plugin.rabbitmq.client.field.accessor.RemoteAddressAccessor)3 Connection (com.rabbitmq.client.Connection)3 AMQConnection (com.rabbitmq.client.impl.AMQConnection)3 LocalAddressAccessor (com.navercorp.pinpoint.plugin.rabbitmq.client.field.accessor.LocalAddressAccessor)2 Address (com.rabbitmq.client.Address)2 MetricsCollector (com.rabbitmq.client.MetricsCollector)2 ConnectionParams (com.rabbitmq.client.impl.ConnectionParams)2 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)2 Test (org.junit.Test)2 SpanEventRecorder (com.navercorp.pinpoint.bootstrap.context.SpanEventRecorder)1 Trace (com.navercorp.pinpoint.bootstrap.context.Trace)1 AddressResolver (com.rabbitmq.client.AddressResolver)1 Channel (com.rabbitmq.client.Channel)1 ConnectionFactory (com.rabbitmq.client.ConnectionFactory)1 FrameHandlerFactory (com.rabbitmq.client.impl.FrameHandlerFactory)1 RecoveryAwareAMQConnection (com.rabbitmq.client.impl.recovery.RecoveryAwareAMQConnection)1 RecoveryAwareAMQConnectionFactory (com.rabbitmq.client.impl.recovery.RecoveryAwareAMQConnectionFactory)1 IOException (java.io.IOException)1 TimeoutException (java.util.concurrent.TimeoutException)1