Search in sources :

Example 1 with RabbitMQProducerConfig

use of com.alibaba.otter.canal.connector.rabbitmq.config.RabbitMQProducerConfig in project canal by alibaba.

the class CanalRabbitMQProducer method loadRabbitMQProperties.

private void loadRabbitMQProperties(Properties properties) {
    RabbitMQProducerConfig rabbitMQProperties = (RabbitMQProducerConfig) this.mqProperties;
    // 兼容下<=1.1.4的mq配置
    doMoreCompatibleConvert("canal.mq.servers", "rabbitmq.host", properties);
    String host = PropertiesUtils.getProperty(properties, RabbitMQConstants.RABBITMQ_HOST);
    if (!StringUtils.isEmpty(host)) {
        rabbitMQProperties.setHost(host);
    }
    String vhost = PropertiesUtils.getProperty(properties, RabbitMQConstants.RABBITMQ_VIRTUAL_HOST);
    if (!StringUtils.isEmpty(vhost)) {
        rabbitMQProperties.setVirtualHost(vhost);
    }
    String exchange = PropertiesUtils.getProperty(properties, RabbitMQConstants.RABBITMQ_EXCHANGE);
    if (!StringUtils.isEmpty(exchange)) {
        rabbitMQProperties.setExchange(exchange);
    }
    String username = PropertiesUtils.getProperty(properties, RabbitMQConstants.RABBITMQ_USERNAME);
    if (!StringUtils.isEmpty(username)) {
        rabbitMQProperties.setUsername(username);
    }
    String password = PropertiesUtils.getProperty(properties, RabbitMQConstants.RABBITMQ_PASSWORD);
    if (!StringUtils.isEmpty(password)) {
        rabbitMQProperties.setPassword(password);
    }
}
Also used : RabbitMQProducerConfig(com.alibaba.otter.canal.connector.rabbitmq.config.RabbitMQProducerConfig)

Example 2 with RabbitMQProducerConfig

use of com.alibaba.otter.canal.connector.rabbitmq.config.RabbitMQProducerConfig in project canal by alibaba.

the class CanalRabbitMQProducer method init.

@Override
public void init(Properties properties) {
    RabbitMQProducerConfig rabbitMQProperties = new RabbitMQProducerConfig();
    this.mqProperties = rabbitMQProperties;
    super.init(properties);
    loadRabbitMQProperties(properties);
    ConnectionFactory factory = new ConnectionFactory();
    String servers = rabbitMQProperties.getHost();
    if (servers.contains(":")) {
        String[] serverHostAndPort = servers.split(":");
        factory.setHost(serverHostAndPort[0]);
        factory.setPort(Integer.parseInt(serverHostAndPort[1]));
    } else {
        factory.setHost(servers);
    }
    if (mqProperties.getAliyunAccessKey().length() > 0 && mqProperties.getAliyunSecretKey().length() > 0 && mqProperties.getAliyunUid() > 0) {
        factory.setCredentialsProvider(new AliyunCredentialsProvider(mqProperties.getAliyunAccessKey(), mqProperties.getAliyunSecretKey(), mqProperties.getAliyunUid()));
    } else {
        factory.setUsername(rabbitMQProperties.getUsername());
        factory.setPassword(rabbitMQProperties.getPassword());
    }
    factory.setVirtualHost(rabbitMQProperties.getVirtualHost());
    try {
        connect = factory.newConnection();
        channel = connect.createChannel();
    // channel.exchangeDeclare(mqProperties.getExchange(), "topic");
    } catch (IOException | TimeoutException ex) {
        throw new CanalException("Start RabbitMQ producer error", ex);
    }
}
Also used : RabbitMQProducerConfig(com.alibaba.otter.canal.connector.rabbitmq.config.RabbitMQProducerConfig) ConnectionFactory(com.rabbitmq.client.ConnectionFactory) IOException(java.io.IOException) TimeoutException(java.util.concurrent.TimeoutException) CanalException(com.alibaba.otter.canal.common.CanalException)

Example 3 with RabbitMQProducerConfig

use of com.alibaba.otter.canal.connector.rabbitmq.config.RabbitMQProducerConfig in project canal by alibaba.

the class CanalRabbitMQProducer method sendMessage.

private void sendMessage(String queueName, byte[] message) {
    // tips: 目前逻辑中暂不处理对exchange处理,请在Console后台绑定 才可使用routekey
    try {
        RabbitMQProducerConfig rabbitMQProperties = (RabbitMQProducerConfig) this.mqProperties;
        channel.basicPublish(rabbitMQProperties.getExchange(), queueName, null, message);
    } catch (Throwable e) {
        throw new RuntimeException(e);
    }
}
Also used : RabbitMQProducerConfig(com.alibaba.otter.canal.connector.rabbitmq.config.RabbitMQProducerConfig)

Aggregations

RabbitMQProducerConfig (com.alibaba.otter.canal.connector.rabbitmq.config.RabbitMQProducerConfig)3 CanalException (com.alibaba.otter.canal.common.CanalException)1 ConnectionFactory (com.rabbitmq.client.ConnectionFactory)1 IOException (java.io.IOException)1 TimeoutException (java.util.concurrent.TimeoutException)1