Search in sources :

Example 1 with AclClientRPCHook

use of org.apache.rocketmq.acl.common.AclClientRPCHook in project canal by alibaba.

the class CanalRocketMQConsumer method connect.

@Override
public void connect() {
    RPCHook rpcHook = null;
    if (null != accessKey && accessKey.length() > 0 && null != secretKey && secretKey.length() > 0) {
        SessionCredentials sessionCredentials = new SessionCredentials();
        sessionCredentials.setAccessKey(accessKey);
        sessionCredentials.setSecretKey(secretKey);
        rpcHook = new AclClientRPCHook(sessionCredentials);
    }
    rocketMQConsumer = new DefaultMQPushConsumer(groupName, rpcHook, new AllocateMessageQueueAveragely(), enableMessageTrace, customizedTraceTopic);
    rocketMQConsumer.setVipChannelEnabled(false);
    if (CLOUD_ACCESS_CHANNEL.equals(this.accessChannel)) {
        rocketMQConsumer.setAccessChannel(AccessChannel.CLOUD);
    }
    if (!StringUtils.isEmpty(this.namespace)) {
        rocketMQConsumer.setNamespace(this.namespace);
    }
    if (!StringUtils.isBlank(nameServer)) {
        rocketMQConsumer.setNamesrvAddr(nameServer);
    }
    if (batchSize != -1) {
        rocketMQConsumer.setConsumeMessageBatchMaxSize(batchSize);
    }
    try {
        if (rocketMQConsumer == null) {
            this.connect();
        }
        rocketMQConsumer.subscribe(this.topic, this.filter);
        rocketMQConsumer.registerMessageListener((MessageListenerOrderly) (messageExts, context) -> {
            context.setAutoCommit(true);
            boolean isSuccess = process(messageExts);
            if (isSuccess) {
                return ConsumeOrderlyStatus.SUCCESS;
            } else {
                return ConsumeOrderlyStatus.SUSPEND_CURRENT_QUEUE_A_MOMENT;
            }
        });
        rocketMQConsumer.start();
    } catch (MQClientException ex) {
        logger.error("Start RocketMQ consumer error", ex);
    }
}
Also used : StringUtils(org.apache.commons.lang.StringUtils) DefaultMQPushConsumer(org.apache.rocketmq.client.consumer.DefaultMQPushConsumer) RPCHook(org.apache.rocketmq.remoting.RPCHook) AclClientRPCHook(org.apache.rocketmq.acl.common.AclClientRPCHook) LoggerFactory(org.slf4j.LoggerFactory) MessageExt(org.apache.rocketmq.common.message.MessageExt) MessageUtil(com.alibaba.otter.canal.connector.core.util.MessageUtil) CanalMessageSerializerUtil(com.alibaba.otter.canal.connector.core.util.CanalMessageSerializerUtil) ArrayList(java.util.ArrayList) MQClientException(org.apache.rocketmq.client.exception.MQClientException) CanalConstants(com.alibaba.otter.canal.connector.core.config.CanalConstants) RocketMQConstants(com.alibaba.otter.canal.connector.rocketmq.config.RocketMQConstants) CanalMsgConsumer(com.alibaba.otter.canal.connector.core.spi.CanalMsgConsumer) Properties(java.util.Properties) Logger(org.slf4j.Logger) Message(com.alibaba.otter.canal.protocol.Message) CanalClientException(com.alibaba.otter.canal.protocol.exception.CanalClientException) BlockingQueue(java.util.concurrent.BlockingQueue) SPI(com.alibaba.otter.canal.connector.core.spi.SPI) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) AllocateMessageQueueAveragely(org.apache.rocketmq.client.consumer.rebalance.AllocateMessageQueueAveragely) TimeUnit(java.util.concurrent.TimeUnit) MessageListenerOrderly(org.apache.rocketmq.client.consumer.listener.MessageListenerOrderly) List(java.util.List) SessionCredentials(org.apache.rocketmq.acl.common.SessionCredentials) JSON(com.alibaba.fastjson.JSON) CommonMessage(com.alibaba.otter.canal.connector.core.consumer.CommonMessage) AccessChannel(org.apache.rocketmq.client.AccessChannel) ConsumeOrderlyStatus(org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyStatus) SessionCredentials(org.apache.rocketmq.acl.common.SessionCredentials) AllocateMessageQueueAveragely(org.apache.rocketmq.client.consumer.rebalance.AllocateMessageQueueAveragely) DefaultMQPushConsumer(org.apache.rocketmq.client.consumer.DefaultMQPushConsumer) RPCHook(org.apache.rocketmq.remoting.RPCHook) AclClientRPCHook(org.apache.rocketmq.acl.common.AclClientRPCHook) AclClientRPCHook(org.apache.rocketmq.acl.common.AclClientRPCHook) MQClientException(org.apache.rocketmq.client.exception.MQClientException)

Example 2 with AclClientRPCHook

use of org.apache.rocketmq.acl.common.AclClientRPCHook 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)

Example 3 with AclClientRPCHook

use of org.apache.rocketmq.acl.common.AclClientRPCHook in project canal by alibaba.

the class RocketMQCanalConnector method connect.

public void connect() throws CanalClientException {
    RPCHook rpcHook = null;
    if (null != accessKey && accessKey.length() > 0 && null != secretKey && secretKey.length() > 0) {
        SessionCredentials sessionCredentials = new SessionCredentials();
        sessionCredentials.setAccessKey(accessKey);
        sessionCredentials.setSecretKey(secretKey);
        rpcHook = new AclClientRPCHook(sessionCredentials);
    }
    rocketMQConsumer = new DefaultMQPushConsumer(groupName, rpcHook, new AllocateMessageQueueAveragely(), enableMessageTrace, customizedTraceTopic);
    rocketMQConsumer.setVipChannelEnabled(false);
    if (CLOUD_ACCESS_CHANNEL.equals(this.accessChannel)) {
        rocketMQConsumer.setAccessChannel(AccessChannel.CLOUD);
    }
    if (!StringUtils.isEmpty(this.namespace)) {
        rocketMQConsumer.setNamespace(this.namespace);
    }
    if (!StringUtils.isBlank(nameServer)) {
        rocketMQConsumer.setNamesrvAddr(nameServer);
    }
    if (batchSize != -1) {
        rocketMQConsumer.setConsumeMessageBatchMaxSize(batchSize);
    }
}
Also used : SessionCredentials(org.apache.rocketmq.acl.common.SessionCredentials) AllocateMessageQueueAveragely(org.apache.rocketmq.client.consumer.rebalance.AllocateMessageQueueAveragely) DefaultMQPushConsumer(org.apache.rocketmq.client.consumer.DefaultMQPushConsumer) RPCHook(org.apache.rocketmq.remoting.RPCHook) AclClientRPCHook(org.apache.rocketmq.acl.common.AclClientRPCHook) AclClientRPCHook(org.apache.rocketmq.acl.common.AclClientRPCHook)

Aggregations

AclClientRPCHook (org.apache.rocketmq.acl.common.AclClientRPCHook)3 SessionCredentials (org.apache.rocketmq.acl.common.SessionCredentials)3 RPCHook (org.apache.rocketmq.remoting.RPCHook)3 DefaultMQPushConsumer (org.apache.rocketmq.client.consumer.DefaultMQPushConsumer)2 AllocateMessageQueueAveragely (org.apache.rocketmq.client.consumer.rebalance.AllocateMessageQueueAveragely)2 MQClientException (org.apache.rocketmq.client.exception.MQClientException)2 JSON (com.alibaba.fastjson.JSON)1 CanalException (com.alibaba.otter.canal.common.CanalException)1 NamedThreadFactory (com.alibaba.otter.canal.common.utils.NamedThreadFactory)1 CanalConstants (com.alibaba.otter.canal.connector.core.config.CanalConstants)1 CommonMessage (com.alibaba.otter.canal.connector.core.consumer.CommonMessage)1 CanalMsgConsumer (com.alibaba.otter.canal.connector.core.spi.CanalMsgConsumer)1 SPI (com.alibaba.otter.canal.connector.core.spi.SPI)1 CanalMessageSerializerUtil (com.alibaba.otter.canal.connector.core.util.CanalMessageSerializerUtil)1 MessageUtil (com.alibaba.otter.canal.connector.core.util.MessageUtil)1 RocketMQConstants (com.alibaba.otter.canal.connector.rocketmq.config.RocketMQConstants)1 RocketMQProducerConfig (com.alibaba.otter.canal.connector.rocketmq.config.RocketMQProducerConfig)1 Message (com.alibaba.otter.canal.protocol.Message)1 CanalClientException (com.alibaba.otter.canal.protocol.exception.CanalClientException)1 ArrayList (java.util.ArrayList)1