Search in sources :

Example 1 with PulsarMQProducerConfig

use of com.alibaba.otter.canal.connector.pulsarmq.config.PulsarMQProducerConfig in project canal by alibaba.

the class CanalPulsarMQProducer method init.

@Override
public void init(Properties properties) {
    // 加载配置
    PulsarMQProducerConfig pulsarMQProducerConfig = new PulsarMQProducerConfig();
    this.mqProperties = pulsarMQProducerConfig;
    super.init(properties);
    loadPulsarMQProperties(properties);
    // 初始化连接客户端
    try {
        client = PulsarClient.builder().serviceUrl(pulsarMQProducerConfig.getServerUrl()).authentication(AuthenticationFactory.token(pulsarMQProducerConfig.getRoleToken())).build();
    } catch (PulsarClientException e) {
        throw new RuntimeException(e);
    }
    // 加载所有生产者 --> topic可能有正则或表名,无法确认所有topic,在使用时再加载
    int parallelPartitionSendThreadSize = mqProperties.getParallelSendThreadSize();
    sendPartitionExecutor = new ThreadPoolExecutor(parallelPartitionSendThreadSize, parallelPartitionSendThreadSize, 0, TimeUnit.SECONDS, new ArrayBlockingQueue<>(parallelPartitionSendThreadSize * 2), new NamedThreadFactory("MQ-Parallel-Sender-Partition"), new ThreadPoolExecutor.CallerRunsPolicy());
}
Also used : ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) NamedThreadFactory(com.alibaba.otter.canal.common.utils.NamedThreadFactory) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) PulsarMQProducerConfig(com.alibaba.otter.canal.connector.pulsarmq.config.PulsarMQProducerConfig)

Example 2 with PulsarMQProducerConfig

use of com.alibaba.otter.canal.connector.pulsarmq.config.PulsarMQProducerConfig in project canal by alibaba.

the class CanalPulsarMQProducer method getProducer.

/**
 * 获取指定topic的生产者,并且使用缓存
 *
 * @param topic
 * @return org.apache.pulsar.client.api.Producer<byte [ ]>
 * @date 2021/9/10 11:21
 * @author chad
 * @since 1 by chad at 2021/9/10 新增
 */
private Producer<byte[]> getProducer(String topic) {
    Producer producer = PRODUCERS.get(topic);
    if (null == producer) {
        try {
            synchronized (PRODUCERS) {
                producer = PRODUCERS.get(topic);
                if (null != producer) {
                    return producer;
                }
                // 拼接topic前缀
                PulsarMQProducerConfig pulsarMQProperties = (PulsarMQProducerConfig) this.mqProperties;
                String prefix = pulsarMQProperties.getTopicTenantPrefix();
                String fullTopic = topic;
                if (!StringUtils.isEmpty(prefix)) {
                    if (!prefix.endsWith("/")) {
                        fullTopic = "/" + fullTopic;
                    }
                    fullTopic = pulsarMQProperties.getTopicTenantPrefix() + fullTopic;
                }
                // 创建指定topic的生产者
                producer = client.newProducer().topic(fullTopic).messageRouter(new MessageRouterImpl(topic)).create();
                // 放入缓存
                PRODUCERS.put(topic, producer);
            }
        } catch (PulsarClientException e) {
            logger.error("create producer failed for topic: " + topic, e);
            throw new RuntimeException(e);
        }
    }
    return producer;
}
Also used : CanalMQProducer(com.alibaba.otter.canal.connector.core.spi.CanalMQProducer) AbstractMQProducer(com.alibaba.otter.canal.connector.core.producer.AbstractMQProducer) PulsarMQProducerConfig(com.alibaba.otter.canal.connector.pulsarmq.config.PulsarMQProducerConfig)

Example 3 with PulsarMQProducerConfig

use of com.alibaba.otter.canal.connector.pulsarmq.config.PulsarMQProducerConfig in project canal by alibaba.

the class CanalPulsarMQProducer method loadPulsarMQProperties.

/**
 * 加载配置
 *
 * @param properties
 * @return void
 * @date 2021/9/15 11:22
 * @author chad
 * @since 1 by chad at 2021/9/15 新增
 */
private void loadPulsarMQProperties(Properties properties) {
    PulsarMQProducerConfig tmpProperties = (PulsarMQProducerConfig) this.mqProperties;
    String serverUrl = PropertiesUtils.getProperty(properties, PulsarMQConstants.PULSARMQ_SERVER_URL);
    if (!StringUtils.isEmpty(serverUrl)) {
        tmpProperties.setServerUrl(serverUrl);
    }
    String roleToken = PropertiesUtils.getProperty(properties, PulsarMQConstants.PULSARMQ_ROLE_TOKEN);
    if (!StringUtils.isEmpty(roleToken)) {
        tmpProperties.setRoleToken(roleToken);
    }
    String topicTenantPrefix = PropertiesUtils.getProperty(properties, PulsarMQConstants.PULSARMQ_TOPIC_TENANT_PREFIX);
    if (!StringUtils.isEmpty(topicTenantPrefix)) {
        tmpProperties.setTopicTenantPrefix(topicTenantPrefix);
    }
    if (logger.isDebugEnabled()) {
        logger.debug("Load pulsar properties ==> {}", JSON.toJSON(this.mqProperties));
    }
}
Also used : PulsarMQProducerConfig(com.alibaba.otter.canal.connector.pulsarmq.config.PulsarMQProducerConfig)

Aggregations

PulsarMQProducerConfig (com.alibaba.otter.canal.connector.pulsarmq.config.PulsarMQProducerConfig)3 NamedThreadFactory (com.alibaba.otter.canal.common.utils.NamedThreadFactory)1 AbstractMQProducer (com.alibaba.otter.canal.connector.core.producer.AbstractMQProducer)1 CanalMQProducer (com.alibaba.otter.canal.connector.core.spi.CanalMQProducer)1 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)1 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)1