Search in sources :

Example 6 with AllocateMessageQueueAveragely

use of org.apache.rocketmq.client.consumer.rebalance.AllocateMessageQueueAveragely 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)

Example 7 with AllocateMessageQueueAveragely

use of org.apache.rocketmq.client.consumer.rebalance.AllocateMessageQueueAveragely in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class AllocateMQSubCommand method execute.

@Override
public void execute(CommandLine commandLine, Options options, RPCHook rpcHook) throws SubCommandException {
    DefaultMQAdminExt adminExt = new DefaultMQAdminExt(rpcHook);
    adminExt.setInstanceName(Long.toString(System.currentTimeMillis()));
    try {
        adminExt.start();
        String topic = commandLine.getOptionValue('t').trim();
        String ips = commandLine.getOptionValue('i').trim();
        final String[] split = ips.split(",");
        final List<String> ipList = new LinkedList<String>();
        for (String ip : split) {
            ipList.add(ip);
        }
        final TopicRouteData topicRouteData = adminExt.examineTopicRouteInfo(topic);
        final Set<MessageQueue> mqs = MQClientInstance.topicRouteData2TopicSubscribeInfo(topic, topicRouteData);
        final AllocateMessageQueueAveragely averagely = new AllocateMessageQueueAveragely();
        RebalanceResult rr = new RebalanceResult();
        for (String i : ipList) {
            final List<MessageQueue> mqResult = averagely.allocate("aa", i, new ArrayList<MessageQueue>(mqs), ipList);
            rr.getResult().put(i, mqResult);
        }
        final String json = RemotingSerializable.toJson(rr, false);
        System.out.printf("%s%n", json);
    } catch (Exception e) {
        throw new SubCommandException(this.getClass().getSimpleName() + " command failed", e);
    } finally {
        adminExt.shutdown();
    }
}
Also used : AllocateMessageQueueAveragely(org.apache.rocketmq.client.consumer.rebalance.AllocateMessageQueueAveragely) SubCommandException(org.apache.rocketmq.tools.command.SubCommandException) LinkedList(java.util.LinkedList) SubCommandException(org.apache.rocketmq.tools.command.SubCommandException) TopicRouteData(org.apache.rocketmq.common.protocol.route.TopicRouteData) MessageQueue(org.apache.rocketmq.common.message.MessageQueue) DefaultMQAdminExt(org.apache.rocketmq.tools.admin.DefaultMQAdminExt)

Aggregations

AllocateMessageQueueAveragely (org.apache.rocketmq.client.consumer.rebalance.AllocateMessageQueueAveragely)7 MessageQueue (org.apache.rocketmq.common.message.MessageQueue)5 HashSet (java.util.HashSet)3 Test (org.junit.Test)3 LinkedList (java.util.LinkedList)2 AclClientRPCHook (org.apache.rocketmq.acl.common.AclClientRPCHook)2 SessionCredentials (org.apache.rocketmq.acl.common.SessionCredentials)2 DefaultMQPushConsumer (org.apache.rocketmq.client.consumer.DefaultMQPushConsumer)2 TopicRouteData (org.apache.rocketmq.common.protocol.route.TopicRouteData)2 RPCHook (org.apache.rocketmq.remoting.RPCHook)2 DefaultMQAdminExt (org.apache.rocketmq.tools.admin.DefaultMQAdminExt)2 SubCommandException (org.apache.rocketmq.tools.command.SubCommandException)2 JSON (com.alibaba.fastjson.JSON)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