Search in sources :

Example 6 with ExecutorTemplate

use of com.alibaba.otter.canal.common.utils.ExecutorTemplate in project canal by alibaba.

the class CanalRabbitMQProducer method send.

@Override
public void send(final MQDestination destination, Message message, Callback callback) {
    ExecutorTemplate template = new ExecutorTemplate(sendExecutor);
    try {
        if (!StringUtils.isEmpty(destination.getDynamicTopic())) {
            // 动态topic
            Map<String, Message> messageMap = MQMessageUtils.messageTopics(message, destination.getTopic(), destination.getDynamicTopic());
            for (Map.Entry<String, com.alibaba.otter.canal.protocol.Message> entry : messageMap.entrySet()) {
                final String topicName = entry.getKey().replace('.', '_');
                final com.alibaba.otter.canal.protocol.Message messageSub = entry.getValue();
                template.submit(() -> send(destination, topicName, messageSub));
            }
            template.waitForResult();
        } else {
            send(destination, destination.getTopic(), message);
        }
        callback.commit();
    } catch (Throwable e) {
        logger.error(e.getMessage(), e);
        callback.rollback();
    } finally {
        template.clear();
    }
}
Also used : ExecutorTemplate(com.alibaba.otter.canal.common.utils.ExecutorTemplate) FlatMessage(com.alibaba.otter.canal.protocol.FlatMessage) Message(com.alibaba.otter.canal.protocol.Message) Message(com.alibaba.otter.canal.protocol.Message) Map(java.util.Map)

Example 7 with ExecutorTemplate

use of com.alibaba.otter.canal.common.utils.ExecutorTemplate in project canal by alibaba.

the class CanalPulsarMQProducer method send.

/**
 * 发送消息,处理的任务:
 * <p>
 * 1. 动态 Topic,根据schema.table或schema来匹配topic配置,将改变发送到指定的一个或多个具体的Topic<br/>
 * 2. 使用线程池发送多个消息,单个消息不使用线程池
 * </p>
 *
 * @param destination 消息目标信息
 * @param message     消息
 * @param callback    消息发送结果回调
 * @return void
 * @date 2021/9/2 22:01
 * @author chad
 * @since 1.0.0 by chad at 2021/9/2: 新增
 */
@Override
public void send(MQDestination destination, com.alibaba.otter.canal.protocol.Message message, Callback callback) {
    ExecutorTemplate template = new ExecutorTemplate(sendExecutor);
    try {
        if (!StringUtils.isEmpty(destination.getDynamicTopic())) {
            // 动态topic
            Map<String, com.alibaba.otter.canal.protocol.Message> messageMap = MQMessageUtils.messageTopics(message, destination.getTopic(), destination.getDynamicTopic());
            for (Map.Entry<String, com.alibaba.otter.canal.protocol.Message> entry : messageMap.entrySet()) {
                String topicName = entry.getKey().replace('.', '_');
                com.alibaba.otter.canal.protocol.Message messageSub = entry.getValue();
                template.submit(() -> {
                    try {
                        send(destination, topicName, messageSub);
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                });
            }
            template.waitForResult();
        } else {
            send(destination, destination.getTopic(), message);
        }
        callback.commit();
    } catch (Throwable e) {
        logger.error(e.getMessage(), e);
        callback.rollback();
    } finally {
        template.clear();
    }
}
Also used : ExecutorTemplate(com.alibaba.otter.canal.common.utils.ExecutorTemplate) FlatMessage(com.alibaba.otter.canal.protocol.FlatMessage)

Aggregations

ExecutorTemplate (com.alibaba.otter.canal.common.utils.ExecutorTemplate)7 FlatMessage (com.alibaba.otter.canal.protocol.FlatMessage)6 Map (java.util.Map)4 CanalException (com.alibaba.otter.canal.common.CanalException)2 MQMessageUtils (com.alibaba.otter.canal.connector.core.producer.MQMessageUtils)2 CanalEntry (com.alibaba.otter.canal.protocol.CanalEntry)2 Message (com.alibaba.otter.canal.protocol.Message)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 MQClientException (org.apache.rocketmq.client.exception.MQClientException)2 Message (org.apache.rocketmq.common.message.Message)2 JSON (com.alibaba.fastjson.JSON)1 SerializerFeature (com.alibaba.fastjson.serializer.SerializerFeature)1 NamedThreadFactory (com.alibaba.otter.canal.common.utils.NamedThreadFactory)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 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