Search in sources :

Example 1 with AliyunCredentialsProvider

use of com.alibaba.otter.canal.connector.rabbitmq.producer.AliyunCredentialsProvider 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)

Aggregations

CanalMsgConsumer (com.alibaba.otter.canal.connector.core.spi.CanalMsgConsumer)1 AliyunCredentialsProvider (com.alibaba.otter.canal.connector.rabbitmq.producer.AliyunCredentialsProvider)1 CanalClientException (com.alibaba.otter.canal.protocol.exception.CanalClientException)1 ConnectionFactory (com.rabbitmq.client.ConnectionFactory)1 Consumer (com.rabbitmq.client.Consumer)1 DefaultConsumer (com.rabbitmq.client.DefaultConsumer)1 Envelope (com.rabbitmq.client.Envelope)1 IOException (java.io.IOException)1 TimeoutException (java.util.concurrent.TimeoutException)1