Java Code Examples for com.rabbitmq.client.impl.AMQConnection

The following examples show how to use com.rabbitmq.client.impl.AMQConnection. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: rabbitmq-mock   Source File: MockConnectionTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
void connectionParams_are_default_ones() {
    Connection connection = new MockConnectionFactory().newConnection();

    SoftAssertions softly = new SoftAssertions();
    softly.assertThat(connection.getAddress().getHostAddress()).isEqualTo("127.0.0.1");
    softly.assertThat(connection.getPort()).isEqualTo(ConnectionFactory.DEFAULT_AMQP_PORT);
    softly.assertThat(connection.getChannelMax()).isEqualTo(0);
    softly.assertThat(connection.getFrameMax()).isEqualTo(0);
    softly.assertThat(connection.getHeartbeat()).isEqualTo(0);
    softly.assertThat(connection.getClientProperties()).isEqualTo(AMQConnection.defaultClientProperties());
    softly.assertThat(connection.getClientProvidedName()).isNull();
    softly.assertThat(connection.getServerProperties().get("version"))
        .isEqualTo(LongStringHelper.asLongString(new Version(AMQP.PROTOCOL.MAJOR, AMQP.PROTOCOL.MINOR).toString()));
    softly.assertThat(connection.getExceptionHandler()).isExactlyInstanceOf(DefaultExceptionHandler.class);
    softly.assertAll();
}
 
Example 2
Source Project: rxrabbit   Source File: DefaultChannelFactory.java    License: MIT License 5 votes vote down vote up
@Override
public String toString() {
    return "{" +
            "channelType=" + channelType +
            ", channelNo=" + delegate.getChannelNumber() +
            ", localPort=" + ((AMQConnection) delegate.getConnection()).getLocalPort() +
            '}';
}
 
Example 3
private Trace createTrace(AMQChannel amqChannel, AMQCommand amqCommand) {
    AMQConnection connection = amqChannel.getConnection();
    if (connection == null) {
        logger.debug("connection is null, skipping trace");
    }

    Method method = amqCommand.getMethod();
    AMQP.Basic.GetOk getOk = (AMQP.Basic.GetOk) method;
    String exchange = getOk.getExchange();
    if (RabbitMQClientPluginConfig.isExchangeExcluded(exchange, excludeExchangeFilter)) {
        if (isDebug) {
            logger.debug("exchange {} is excluded", exchange);
        }
        return null;
    }
    String routingKey = getOk.getRoutingKey();

    Map<String, Object> headers = getHeadersFromContentHeader(amqCommand.getContentHeader());

    // If this transaction is not traceable, mark as disabled.
    if (headers.get(RabbitMQClientConstants.META_SAMPLED) != null) {
        return traceContext.disableSampling();
    }

    final TraceId traceId = populateTraceIdFromRequest(headers);
    // If there's no trasanction id, a new trasaction begins here.
    final Trace trace = traceId == null ? traceContext.newTraceObject() : traceContext.continueTraceObject(traceId);
    if (trace.canSampled()) {
        final SpanRecorder recorder = trace.getSpanRecorder();
        recordRootSpan(recorder, connection, exchange, routingKey, headers);
    }
    return trace;
}
 
Example 4
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 = exchange;
    if (StringUtils.isEmpty(convertedExchange)) {
        convertedExchange = 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.isEmpty(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()));
        }
    }
}
 
Example 5
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 = envelope.getExchange();
    if (StringUtils.isEmpty(exchange)) {
        exchange = 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.isEmpty(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()));
        }
    }
}
 
Example 6
Source Project: rabbitmq-mock   Source File: MockConnection.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public Map<String, Object> getClientProperties() {
    return AMQConnection.defaultClientProperties();
}
 
Example 7
Source Project: rxrabbit   Source File: DefaultChannelFactory.java    License: MIT License 4 votes vote down vote up
private synchronized Connection createConnection(ChannelType connectionType) throws ConnectionFailureException {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date startTime = new Date();
    sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
    settings.getClient_properties().put("connection_type", connectionType.toString());
    settings.getClient_properties().put("connect_time", sdf.format(startTime)+"Z");

    ConnectionFactory cf = new ConnectionFactory();
    cf.setRequestedHeartbeat(settings.getHeartbeat());
    cf.setConnectionTimeout(settings.getConnection_timeout_millis());
    cf.setShutdownTimeout(settings.getShutdown_timeout_millis());
    cf.setRequestedFrameMax(settings.getFrame_max());
    cf.setHandshakeTimeout(settings.getHandshake_timeout_millis());
    cf.setClientProperties((Map)settings.getClient_properties());
    //cf.setSocketConfigurator(); NOTE is this worth investigating??
    cf.setRequestedChannelMax(0);//Hard coded ..
    cf.setAutomaticRecoveryEnabled(false);//Hard coded ..
    cf.setTopologyRecoveryEnabled(false);//Hard coded ..

    Exception lastException = null;
    Connection connection = null;
    for (BrokerAddresses.BrokerAddress address : addresses) {
        cf.setPassword(address.password);
        cf.setUsername(address.username);
        cf.setPort(address.port);
        cf.setHost(address.host);
        cf.setVirtualHost(address.virtualHost);
        try {
            if(address.scheme.toLowerCase().equals("amqps")){
                cf.useSslProtocol();
                cf.setSocketFactory(SSLSocketFactory.getDefault()); //Because rabbit uses NoopTrustStore by default...
            }
            log.infoWithParams("Creating "+connectionType+" connection to broker ...",
                    "address", address.toString(),
                    "settings", settings.toString());
            connection = cf.newConnection();
            boolean isOpen = connection.isOpen();
            if(!isOpen){
                continue;
            }
            break;
        } catch (Exception e) {
            log.debugWithParams("Failed to createConnection to broker",
                    "address", address.toString());
            lastException = e;
        }
    }

    if(connection == null){
        throw new ConnectionFailureException(cf, lastException);
    }

    conToChannel.put(connectionType,
            new ConnectionInfo(
                    connection,
                    new ArrayList<ChannelImpl>(),
                    settings.getClient_properties(),
                    connectionType)
    );
    log.infoWithParams("Successfully created "+connectionType+" connection to broker.",
            "address", addresses.get(0).toString(),
            "localPort", ((AMQConnection) connection).getLocalPort(),
            "settings", settings.toString());

    return connection;

}
 
Example 8
Source Project: pinpoint   Source File: ChannelBasicPublishInterceptor.java    License: Apache License 2.0 4 votes vote down vote up
@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();
    }
}