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();
}
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);
}
}
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 ...");
});
}
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);
}
}
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;
}
Aggregations