Search in sources :

Example 1 with RocketMQProducerConfig

use of com.alibaba.otter.canal.connector.rocketmq.config.RocketMQProducerConfig in project canal by alibaba.

the class CanalRocketMQProducer method loadRocketMQProperties.

private void loadRocketMQProperties(Properties properties) {
    RocketMQProducerConfig rocketMQProperties = (RocketMQProducerConfig) this.mqProperties;
    // 兼容下<=1.1.4的mq配置
    doMoreCompatibleConvert("canal.mq.servers", "rocketmq.namesrv.addr", properties);
    doMoreCompatibleConvert("canal.mq.producerGroup", "rocketmq.producer.group", properties);
    doMoreCompatibleConvert("canal.mq.namespace", "rocketmq.namespace", properties);
    doMoreCompatibleConvert("canal.mq.retries", "rocketmq.retry.times.when.send.failed", properties);
    String producerGroup = PropertiesUtils.getProperty(properties, RocketMQConstants.ROCKETMQ_PRODUCER_GROUP);
    if (!StringUtils.isEmpty(producerGroup)) {
        rocketMQProperties.setProducerGroup(producerGroup);
    }
    String enableMessageTrace = PropertiesUtils.getProperty(properties, RocketMQConstants.ROCKETMQ_ENABLE_MESSAGE_TRACE);
    if (!StringUtils.isEmpty(enableMessageTrace)) {
        rocketMQProperties.setEnableMessageTrace(Boolean.parseBoolean(enableMessageTrace));
    }
    String customizedTraceTopic = PropertiesUtils.getProperty(properties, RocketMQConstants.ROCKETMQ_CUSTOMIZED_TRACE_TOPIC);
    if (!StringUtils.isEmpty(customizedTraceTopic)) {
        rocketMQProperties.setCustomizedTraceTopic(customizedTraceTopic);
    }
    String namespace = PropertiesUtils.getProperty(properties, RocketMQConstants.ROCKETMQ_NAMESPACE);
    if (!StringUtils.isEmpty(namespace)) {
        rocketMQProperties.setNamespace(namespace);
    }
    String namesrvAddr = PropertiesUtils.getProperty(properties, RocketMQConstants.ROCKETMQ_NAMESRV_ADDR);
    if (!StringUtils.isEmpty(namesrvAddr)) {
        rocketMQProperties.setNamesrvAddr(namesrvAddr);
    }
    String retry = PropertiesUtils.getProperty(properties, RocketMQConstants.ROCKETMQ_RETRY_TIMES_WHEN_SEND_FAILED);
    if (!StringUtils.isEmpty(retry)) {
        rocketMQProperties.setRetryTimesWhenSendFailed(Integer.parseInt(retry));
    }
    String vipChannelEnabled = PropertiesUtils.getProperty(properties, RocketMQConstants.ROCKETMQ_VIP_CHANNEL_ENABLED);
    if (!StringUtils.isEmpty(vipChannelEnabled)) {
        rocketMQProperties.setVipChannelEnabled(Boolean.parseBoolean(vipChannelEnabled));
    }
    String tag = PropertiesUtils.getProperty(properties, RocketMQConstants.ROCKETMQ_TAG);
    if (!StringUtils.isEmpty(tag)) {
        rocketMQProperties.setTag(tag);
    }
}
Also used : RocketMQProducerConfig(com.alibaba.otter.canal.connector.rocketmq.config.RocketMQProducerConfig)

Example 2 with RocketMQProducerConfig

use of com.alibaba.otter.canal.connector.rocketmq.config.RocketMQProducerConfig in project canal by alibaba.

the class CanalRocketMQProducer method send.

public void send(final MQDestination destination, String topicName, com.alibaba.otter.canal.protocol.Message message) {
    // 获取当前topic的分区数
    Integer partitionNum = MQMessageUtils.parseDynamicTopicPartition(topicName, destination.getDynamicTopicPartitionNum());
    // 获取topic的队列数为分区数
    if (partitionNum == null) {
        partitionNum = getTopicDynamicQueuesSize(destination.getEnableDynamicQueuePartition(), topicName);
    }
    if (partitionNum == null) {
        partitionNum = destination.getPartitionsNum();
    }
    if (!mqProperties.isFlatMessage()) {
        if (destination.getPartitionHash() != null && !destination.getPartitionHash().isEmpty()) {
            // 并发构造
            MQMessageUtils.EntryRowData[] datas = MQMessageUtils.buildMessageData(message, buildExecutor);
            // 串行分区
            com.alibaba.otter.canal.protocol.Message[] messages = MQMessageUtils.messagePartition(datas, message.getId(), partitionNum, destination.getPartitionHash(), mqProperties.isDatabaseHash());
            int length = messages.length;
            ExecutorTemplate template = new ExecutorTemplate(sendPartitionExecutor);
            for (int i = 0; i < length; i++) {
                com.alibaba.otter.canal.protocol.Message dataPartition = messages[i];
                if (dataPartition != null) {
                    final int index = i;
                    template.submit(() -> {
                        Message data = new Message(topicName, ((RocketMQProducerConfig) this.mqProperties).getTag(), CanalMessageSerializerUtil.serializer(dataPartition, mqProperties.isFilterTransactionEntry()));
                        sendMessage(data, index);
                    });
                }
            }
            // 等所有分片发送完毕
            template.waitForResult();
        } else {
            final int partition = destination.getPartition() != null ? destination.getPartition() : 0;
            Message data = new Message(topicName, ((RocketMQProducerConfig) this.mqProperties).getTag(), CanalMessageSerializerUtil.serializer(message, mqProperties.isFilterTransactionEntry()));
            sendMessage(data, partition);
        }
    } else {
        // 并发构造
        MQMessageUtils.EntryRowData[] datas = MQMessageUtils.buildMessageData(message, buildExecutor);
        // 串行分区
        List<FlatMessage> flatMessages = MQMessageUtils.messageConverter(datas, message.getId());
        // 初始化分区合并队列
        if (destination.getPartitionHash() != null && !destination.getPartitionHash().isEmpty()) {
            List<List<FlatMessage>> partitionFlatMessages = new ArrayList<>();
            for (int i = 0; i < partitionNum; i++) {
                partitionFlatMessages.add(new ArrayList<>());
            }
            for (FlatMessage flatMessage : flatMessages) {
                FlatMessage[] partitionFlatMessage = MQMessageUtils.messagePartition(flatMessage, partitionNum, destination.getPartitionHash(), mqProperties.isDatabaseHash());
                int length = partitionFlatMessage.length;
                for (int i = 0; i < length; i++) {
                    // 增加null判断,issue #3267
                    if (partitionFlatMessage[i] != null) {
                        partitionFlatMessages.get(i).add(partitionFlatMessage[i]);
                    }
                }
            }
            ExecutorTemplate template = new ExecutorTemplate(sendPartitionExecutor);
            for (int i = 0; i < partitionFlatMessages.size(); i++) {
                final List<FlatMessage> flatMessagePart = partitionFlatMessages.get(i);
                if (flatMessagePart != null && flatMessagePart.size() > 0) {
                    final int index = i;
                    template.submit(() -> {
                        List<Message> messages = flatMessagePart.stream().map(flatMessage -> new Message(topicName, ((RocketMQProducerConfig) this.mqProperties).getTag(), JSON.toJSONBytes(flatMessage, SerializerFeature.WriteMapNullValue))).collect(Collectors.toList());
                        // 批量发送
                        sendMessage(messages, index);
                    });
                }
            }
            // 批量等所有分区的结果
            template.waitForResult();
        } else {
            final int partition = destination.getPartition() != null ? destination.getPartition() : 0;
            List<Message> messages = flatMessages.stream().map(flatMessage -> new Message(topicName, ((RocketMQProducerConfig) this.mqProperties).getTag(), JSON.toJSONBytes(flatMessage, SerializerFeature.WriteMapNullValue))).collect(Collectors.toList());
            // 批量发送
            sendMessage(messages, partition);
        }
    }
}
Also used : Callback(com.alibaba.otter.canal.connector.core.util.Callback) StringUtils(org.apache.commons.lang.StringUtils) Message(org.apache.rocketmq.common.message.Message) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) SerializerFeature(com.alibaba.fastjson.serializer.SerializerFeature) RPCHook(org.apache.rocketmq.remoting.RPCHook) AclClientRPCHook(org.apache.rocketmq.acl.common.AclClientRPCHook) MessageQueue(org.apache.rocketmq.common.message.MessageQueue) LoggerFactory(org.slf4j.LoggerFactory) CanalMessageSerializerUtil(com.alibaba.otter.canal.connector.core.util.CanalMessageSerializerUtil) PropertiesUtils(com.alibaba.otter.canal.common.utils.PropertiesUtils) ArrayList(java.util.ArrayList) RocketMQConstants(com.alibaba.otter.canal.connector.rocketmq.config.RocketMQConstants) MQClientException(org.apache.rocketmq.client.exception.MQClientException) MQMessageUtils(com.alibaba.otter.canal.connector.core.producer.MQMessageUtils) FlatMessage(com.alibaba.otter.canal.protocol.FlatMessage) Map(java.util.Map) DefaultMQProducerImpl(org.apache.rocketmq.client.impl.producer.DefaultMQProducerImpl) SendResult(org.apache.rocketmq.client.producer.SendResult) ExecutorTemplate(com.alibaba.otter.canal.common.utils.ExecutorTemplate) DefaultMQProducer(org.apache.rocketmq.client.producer.DefaultMQProducer) RocketMQProducerConfig(com.alibaba.otter.canal.connector.rocketmq.config.RocketMQProducerConfig) MQDestination(com.alibaba.otter.canal.connector.core.producer.MQDestination) Logger(org.slf4j.Logger) Properties(java.util.Properties) NamedThreadFactory(com.alibaba.otter.canal.common.utils.NamedThreadFactory) SPI(com.alibaba.otter.canal.connector.core.spi.SPI) Collectors(java.util.stream.Collectors) CanalException(com.alibaba.otter.canal.common.CanalException) TimeUnit(java.util.concurrent.TimeUnit) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) List(java.util.List) JSON(com.alibaba.fastjson.JSON) SessionCredentials(org.apache.rocketmq.acl.common.SessionCredentials) CanalMQProducer(com.alibaba.otter.canal.connector.core.spi.CanalMQProducer) TopicPublishInfo(org.apache.rocketmq.client.impl.producer.TopicPublishInfo) AbstractMQProducer(com.alibaba.otter.canal.connector.core.producer.AbstractMQProducer) AccessChannel(org.apache.rocketmq.client.AccessChannel) ExecutorTemplate(com.alibaba.otter.canal.common.utils.ExecutorTemplate) Message(org.apache.rocketmq.common.message.Message) FlatMessage(com.alibaba.otter.canal.protocol.FlatMessage) ArrayList(java.util.ArrayList) FlatMessage(com.alibaba.otter.canal.protocol.FlatMessage) ArrayList(java.util.ArrayList) List(java.util.List)

Example 3 with RocketMQProducerConfig

use of com.alibaba.otter.canal.connector.rocketmq.config.RocketMQProducerConfig in project canal by alibaba.

the class CanalRocketMQProducer method init.

@Override
public void init(Properties properties) {
    RocketMQProducerConfig rocketMQProperties = new RocketMQProducerConfig();
    this.mqProperties = rocketMQProperties;
    super.init(properties);
    loadRocketMQProperties(properties);
    RPCHook rpcHook = null;
    if (mqProperties.getAliyunAccessKey().length() > 0 && mqProperties.getAliyunSecretKey().length() > 0) {
        SessionCredentials sessionCredentials = new SessionCredentials();
        sessionCredentials.setAccessKey(mqProperties.getAliyunAccessKey());
        sessionCredentials.setSecretKey(mqProperties.getAliyunSecretKey());
        rpcHook = new AclClientRPCHook(sessionCredentials);
    }
    defaultMQProducer = new DefaultMQProducer(rocketMQProperties.getProducerGroup(), rpcHook, rocketMQProperties.isEnableMessageTrace(), rocketMQProperties.getCustomizedTraceTopic());
    if (CLOUD_ACCESS_CHANNEL.equals(rocketMQProperties.getAccessChannel())) {
        defaultMQProducer.setAccessChannel(AccessChannel.CLOUD);
    }
    if (!StringUtils.isEmpty(rocketMQProperties.getNamespace())) {
        defaultMQProducer.setNamespace(rocketMQProperties.getNamespace());
    }
    defaultMQProducer.setNamesrvAddr(rocketMQProperties.getNamesrvAddr());
    defaultMQProducer.setRetryTimesWhenSendFailed(rocketMQProperties.getRetryTimesWhenSendFailed());
    defaultMQProducer.setVipChannelEnabled(rocketMQProperties.isVipChannelEnabled());
    logger.info("##Start RocketMQ producer##");
    try {
        defaultMQProducer.start();
    } catch (MQClientException ex) {
        throw new CanalException("Start RocketMQ producer error", ex);
    }
    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 : SessionCredentials(org.apache.rocketmq.acl.common.SessionCredentials) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) NamedThreadFactory(com.alibaba.otter.canal.common.utils.NamedThreadFactory) RocketMQProducerConfig(com.alibaba.otter.canal.connector.rocketmq.config.RocketMQProducerConfig) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) RPCHook(org.apache.rocketmq.remoting.RPCHook) AclClientRPCHook(org.apache.rocketmq.acl.common.AclClientRPCHook) DefaultMQProducer(org.apache.rocketmq.client.producer.DefaultMQProducer) AclClientRPCHook(org.apache.rocketmq.acl.common.AclClientRPCHook) MQClientException(org.apache.rocketmq.client.exception.MQClientException) CanalException(com.alibaba.otter.canal.common.CanalException)

Aggregations

RocketMQProducerConfig (com.alibaba.otter.canal.connector.rocketmq.config.RocketMQProducerConfig)3 CanalException (com.alibaba.otter.canal.common.CanalException)2 NamedThreadFactory (com.alibaba.otter.canal.common.utils.NamedThreadFactory)2 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)2 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)2 AclClientRPCHook (org.apache.rocketmq.acl.common.AclClientRPCHook)2 SessionCredentials (org.apache.rocketmq.acl.common.SessionCredentials)2 MQClientException (org.apache.rocketmq.client.exception.MQClientException)2 DefaultMQProducer (org.apache.rocketmq.client.producer.DefaultMQProducer)2 RPCHook (org.apache.rocketmq.remoting.RPCHook)2 JSON (com.alibaba.fastjson.JSON)1 SerializerFeature (com.alibaba.fastjson.serializer.SerializerFeature)1 ExecutorTemplate (com.alibaba.otter.canal.common.utils.ExecutorTemplate)1 PropertiesUtils (com.alibaba.otter.canal.common.utils.PropertiesUtils)1 AbstractMQProducer (com.alibaba.otter.canal.connector.core.producer.AbstractMQProducer)1 MQDestination (com.alibaba.otter.canal.connector.core.producer.MQDestination)1 MQMessageUtils (com.alibaba.otter.canal.connector.core.producer.MQMessageUtils)1 CanalMQProducer (com.alibaba.otter.canal.connector.core.spi.CanalMQProducer)1 SPI (com.alibaba.otter.canal.connector.core.spi.SPI)1 Callback (com.alibaba.otter.canal.connector.core.util.Callback)1