Search in sources :

Example 51 with ConnectionFactory

use of com.rabbitmq.client.ConnectionFactory in project spring-boot by spring-projects.

the class RabbitMetricsTests method connectionFactoryWithTagsIsInstrumented.

@Test
void connectionFactoryWithTagsIsInstrumented() {
    ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
    SimpleMeterRegistry registry = new SimpleMeterRegistry();
    new RabbitMetrics(connectionFactory, Tags.of("env", "prod")).bindTo(registry);
    assertThat(registry.get("rabbitmq.connections").tags("env", "prod").meter()).isNotNull();
    assertThat(registry.find("rabbitmq.connections").tags("env", "dev").meter()).isNull();
}
Also used : ConnectionFactory(com.rabbitmq.client.ConnectionFactory) SimpleMeterRegistry(io.micrometer.core.instrument.simple.SimpleMeterRegistry) Test(org.junit.jupiter.api.Test)

Example 52 with ConnectionFactory

use of com.rabbitmq.client.ConnectionFactory in project midpoint by Evolveum.

the class EmbeddedBroker method send.

public void send(String queueName, String message, Map<String, Object> headers) throws IOException, TimeoutException {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    try (Connection connection = factory.newConnection();
        Channel channel = connection.createChannel()) {
        channel.basicPublish("", queueName, createProperties(headers), message.getBytes(StandardCharsets.UTF_8));
        LOGGER.trace("Sent '{}'", message);
    }
}
Also used : ConnectionFactory(com.rabbitmq.client.ConnectionFactory) Channel(com.rabbitmq.client.Channel) Connection(com.rabbitmq.client.Connection)

Example 53 with ConnectionFactory

use of com.rabbitmq.client.ConnectionFactory in project graylog2-server by Graylog2.

the class AmqpConsumer method connect.

public void connect() throws IOException {
    final ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(hostname);
    factory.setPort(port);
    factory.setVirtualHost(virtualHost);
    factory.setRequestedHeartbeat(heartbeatTimeout);
    // explicitly setting this, to ensure it is true even if the default changes.
    factory.setAutomaticRecoveryEnabled(true);
    if (tls) {
        try {
            LOG.info("Enabling TLS for AMQP input [{}/{}].", sourceInput.getName(), sourceInput.getId());
            factory.useSslProtocol();
        } catch (NoSuchAlgorithmException | KeyManagementException e) {
            throw new IOException("Couldn't enable TLS for AMQP input.", e);
        }
    }
    // Authenticate?
    if (!isNullOrEmpty(username) && !isNullOrEmpty(password)) {
        factory.setUsername(username);
        factory.setPassword(password);
    }
    try {
        connection = factory.newConnection();
    } catch (TimeoutException e) {
        throw new IOException("Timeout while opening new AMQP connection", e);
    }
    channel = connection.createChannel();
    if (null == channel) {
        LOG.error("No channel descriptor available!");
    }
    if (null != channel && prefetchCount > 0) {
        channel.basicQos(prefetchCount);
        LOG.debug("AMQP prefetch count overriden to <{}>.", prefetchCount);
    }
    connection.addShutdownListener(cause -> {
        if (cause.isInitiatedByApplication()) {
            LOG.info("Shutting down AMPQ consumer.");
            return;
        }
        LOG.warn("AMQP connection lost! Reconnecting ...");
    });
}
Also used : ConnectionFactory(com.rabbitmq.client.ConnectionFactory) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) KeyManagementException(java.security.KeyManagementException) TimeoutException(java.util.concurrent.TimeoutException)

Example 54 with ConnectionFactory

use of com.rabbitmq.client.ConnectionFactory in project canal by alibaba.

the class CanalRabbitMQConsumer method connect.

@Override
public void connect() {
    ConnectionFactory factory = new ConnectionFactory();
    if (accessKey.length() > 0 && secretKey.length() > 0) {
        factory.setCredentialsProvider(new AliyunCredentialsProvider(accessKey, secretKey, resourceOwnerId));
    } else {
        factory.setUsername(username);
        factory.setPassword(password);
    }
    // 解析出端口 modified by 16075140
    if (nameServer != null && nameServer.contains(":")) {
        String[] serverHostAndPort = nameServer.split(":");
        factory.setHost(serverHostAndPort[0]);
        factory.setPort(Integer.parseInt(serverHostAndPort[1]));
    } else {
        factory.setHost(nameServer);
    }
    factory.setAutomaticRecoveryEnabled(true);
    factory.setNetworkRecoveryInterval(5000);
    factory.setVirtualHost(vhost);
    try {
        connect = factory.newConnection();
        channel = connect.createChannel();
    } catch (IOException | TimeoutException e) {
        throw new CanalClientException("Start RabbitMQ producer error", e);
    }
    // 不存在连接 则重新连接
    if (connect == null) {
        this.connect();
    }
    Consumer consumer = new DefaultConsumer(channel) {

        @Override
        public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
            if (body != null) {
                channel.basicAck(envelope.getDeliveryTag(), process(body));
            }
        }
    };
    try {
        channel.basicConsume(queueName, false, consumer);
    } catch (IOException e) {
        throw new CanalClientException("error", e);
    }
}
Also used : ConnectionFactory(com.rabbitmq.client.ConnectionFactory) CanalMsgConsumer(com.alibaba.otter.canal.connector.core.spi.CanalMsgConsumer) Consumer(com.rabbitmq.client.Consumer) DefaultConsumer(com.rabbitmq.client.DefaultConsumer) DefaultConsumer(com.rabbitmq.client.DefaultConsumer) CanalClientException(com.alibaba.otter.canal.protocol.exception.CanalClientException) AliyunCredentialsProvider(com.alibaba.otter.canal.connector.rabbitmq.producer.AliyunCredentialsProvider) IOException(java.io.IOException) Envelope(com.rabbitmq.client.Envelope) TimeoutException(java.util.concurrent.TimeoutException)

Example 55 with ConnectionFactory

use of com.rabbitmq.client.ConnectionFactory in project zipkin by openzipkin.

the class ZipkinRabbitMQCollectorProperties method toBuilder.

public RabbitMQCollector.Builder toBuilder() throws KeyManagementException, NoSuchAlgorithmException, URISyntaxException {
    final RabbitMQCollector.Builder result = RabbitMQCollector.builder();
    ConnectionFactory connectionFactory = new ConnectionFactory();
    if (concurrency != null)
        result.concurrency(concurrency);
    if (connectionTimeout != null)
        connectionFactory.setConnectionTimeout(connectionTimeout);
    if (queue != null)
        result.queue(queue);
    if (uri != null) {
        connectionFactory.setUri(uri);
    } else {
        if (addresses != null)
            result.addresses(addresses);
        if (password != null)
            connectionFactory.setPassword(password);
        if (username != null)
            connectionFactory.setUsername(username);
        if (virtualHost != null)
            connectionFactory.setVirtualHost(virtualHost);
        if (useSsl != null && useSsl)
            connectionFactory.useSslProtocol();
    }
    result.connectionFactory(connectionFactory);
    return result;
}
Also used : RabbitMQCollector(zipkin2.collector.rabbitmq.RabbitMQCollector) ConnectionFactory(com.rabbitmq.client.ConnectionFactory)

Aggregations

ConnectionFactory (com.rabbitmq.client.ConnectionFactory)188 Connection (com.rabbitmq.client.Connection)87 Test (org.junit.Test)73 Channel (com.rabbitmq.client.Channel)63 IOException (java.io.IOException)53 TimeoutException (java.util.concurrent.TimeoutException)23 DefaultConsumer (com.rabbitmq.client.DefaultConsumer)11 Envelope (com.rabbitmq.client.Envelope)11 SSLContext (javax.net.ssl.SSLContext)11 QueueingConsumer (com.rabbitmq.client.QueueingConsumer)9 HashMap (java.util.HashMap)9 CountDownLatch (java.util.concurrent.CountDownLatch)8 AMQP (com.rabbitmq.client.AMQP)6 Consumer (com.rabbitmq.client.Consumer)6 ShutdownSignalException (com.rabbitmq.client.ShutdownSignalException)6 AMQConnection (com.rabbitmq.client.impl.AMQConnection)6 Address (com.rabbitmq.client.Address)5 NioParams (com.rabbitmq.client.impl.nio.NioParams)4 AutorecoveringConnection (com.rabbitmq.client.impl.recovery.AutorecoveringConnection)4 Path (javax.ws.rs.Path)3