Java Code Examples for org.apache.rocketmq.client.consumer.DefaultMQPullConsumer#setNamesrvAddr()
The following examples show how to use
org.apache.rocketmq.client.consumer.DefaultMQPullConsumer#setNamesrvAddr() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MQAdminInstance.java From rocketmq-exporter with Apache License 2.0 | 6 votes |
@Bean(destroyMethod = "shutdown") private DefaultMQPullConsumer buildPullConsumer() throws Exception { String namesrvAddress = configure.getNamesrvAddr(); if (StringUtils.isBlank(namesrvAddress)) { log.error("init default pull consumer error, namesrv is null"); throw new Exception("init default pull consumer error, namesrv is null", null); } DefaultMQPullConsumer pullConsumer = new DefaultMQPullConsumer(TOOLS_CONSUMER_GROUP, null); pullConsumer.setInstanceName("consumer-" + System.currentTimeMillis()); pullConsumer.setNamesrvAddr(namesrvAddress); try { pullConsumer.start(); pullConsumer.getDefaultMQPullConsumerImpl().getPullAPIWrapper().setConnectBrokerByUser(true); } catch (MQClientException ex) { log.error(String.format("init default pull consumer error, namesrv=%s", System.getProperty(MixAll.NAMESRV_ADDR_PROPERTY)), ex); } return pullConsumer; }
Example 2
Source File: PullConsumerTest.java From rocketmq with Apache License 2.0 | 6 votes |
public static void main(String[] args) throws MQClientException { DefaultMQPullConsumer consumer = new DefaultMQPullConsumer("please_rename_unique_group_name_5"); consumer.setNamesrvAddr("127.0.0.1:9876"); consumer.start(); try { MessageQueue mq = new MessageQueue(); mq.setQueueId(0); mq.setTopic("TopicTest3"); mq.setBrokerName("vivedeMacBook-Pro.local"); long offset = 26; long beginTime = System.currentTimeMillis(); PullResult pullResult = consumer.pullBlockIfNotFound(mq, null, offset, 32); System.out.printf("%s%n", System.currentTimeMillis() - beginTime); System.out.printf("%s%n", pullResult); } catch (Exception e) { e.printStackTrace(); } consumer.shutdown(); }
Example 3
Source File: Utils.java From DDMQ with Apache License 2.0 | 5 votes |
private static DefaultMQPullConsumer getDefaultMQPullConsumer(String nameSvr, String cluster, String key) { try { DefaultMQPullConsumer consumer = new DefaultMQPullConsumer("monitor_consumer_" + key.replaceAll(":", "_").replaceAll("\\.", "_")); consumer.setInstanceName(String.valueOf(System.currentTimeMillis())); consumer.setNamesrvAddr(nameSvr); consumer.setVipChannelEnabled(false); consumer.start(); return consumer; } catch (MQClientException e) { logger.error("[DefaultMQPullConsumer] start DefaultMQPullConsumer error, nameServer:{}, cluster:{}, key:{}", nameSvr, cluster, key, e); throw new RuntimeException("[DefaultMQPullConsumer] start DefaultMQPullConsumer error, nameServer:" + nameSvr); } }
Example 4
Source File: RmqFetcher.java From DDMQ with Apache License 2.0 | 5 votes |
@Override public synchronized boolean start() { DefaultMQPullConsumer consumer = new DefaultMQPullConsumer(config.getGroup()); RocketmqConfiguration rmqConfig = config.getcProxyConfig().getRocketmqConfigs().get(config.getBrokerCluster()); String nameServers = StringUtils.join(rmqConfig.getNamesrvAddrs().iterator(), ";"); consumer.setNamesrvAddr(nameServers); //必须这样设置。一个instance只能有一个group consumer.setInstanceName(RmqCidMaker.makeCid(SECOND_LOWLEVEL_CONSUMER_INSTANCE_NUM, config.getGroup(), config.getBrokerCluster(), config.getcProxyConfig().getProxyCluster(), config.getInstance(), LOW_LEVEL_V2));//根据instancename 隔离highlevel和lowlevel的netty资源 //fix 2018.6.14 lowlevel实例过多,netty线程未共享 consumer.setClientCallbackExecutorThreads(1); consumer.setMessageQueueListener(this); consumer.setPersistConsumerOffsetInterval(rmqConfig.getPersistConsumerOffsetInterval()); consumer.setPollNameServerInterval(rmqConfig.getPollNameServerInterval()); consumer.setRegisterTopics(new HashSet<>(config.getTopicNames())); try { consumer.start(); } catch (MQClientException e) { LogUtils.MAIN_LOGGER.error(String.format("start RmqFetcher failed. group@Cluster=%s,consumerId=%s", config.getGroupBrokerCluster(), cid), e); consumer.shutdown(); return false; } this.consumer = consumer; LogUtils.logMainInfo("RmqFetcher.start, this={}", this); return true; }
Example 5
Source File: PullConsumer.java From rocketmq-read with Apache License 2.0 | 5 votes |
public static void main(String[] args) throws MQClientException { DefaultMQPullConsumer consumer = new DefaultMQPullConsumer("please_rename_unique_group_name_5"); consumer.setNamesrvAddr("127.0.0.1:9876"); consumer.start(); Set<MessageQueue> mqs = consumer.fetchSubscribeMessageQueues("face-manager-1"); for (MessageQueue mq : mqs) { System.out.printf("Consume from the queue: %s%n", mq); SINGLE_MQ: while (true) { try { PullResult pullResult = consumer.pullBlockIfNotFound(mq, null, getMessageQueueOffset(mq), 32); System.out.printf("%s%n", pullResult); putMessageQueueOffset(mq, pullResult.getNextBeginOffset()); switch (pullResult.getPullStatus()) { case FOUND: break; case NO_MATCHED_MSG: break; case NO_NEW_MSG: break SINGLE_MQ; case OFFSET_ILLEGAL: break; default: break; } } catch (Exception e) { e.printStackTrace(); } } } consumer.shutdown(); }
Example 6
Source File: Utils.java From DDMQ with Apache License 2.0 | 5 votes |
private static DefaultMQPullConsumer getDefaultMQPullConsumer(String nameSvr, String cluster, String key) { try { DefaultMQPullConsumer consumer = new DefaultMQPullConsumer("monitor_consumer_" + key.replaceAll(":", "_").replaceAll("\\.", "_")); consumer.setInstanceName(String.valueOf(System.currentTimeMillis())); consumer.setNamesrvAddr(nameSvr); consumer.setVipChannelEnabled(false); consumer.start(); return consumer; } catch (MQClientException e) { logger.error("[DefaultMQPullConsumer] start DefaultMQPullConsumer error, nameServer:{}, cluster:{}, key:{}", nameSvr, cluster, key, e); throw new RuntimeException("[DefaultMQPullConsumer] start DefaultMQPullConsumer error, nameServer:" + nameSvr); } }
Example 7
Source File: RmqFetcher.java From DDMQ with Apache License 2.0 | 5 votes |
@Override public synchronized boolean start() { DefaultMQPullConsumer consumer = new DefaultMQPullConsumer(config.getGroup()); RocketmqConfiguration rmqConfig = config.getcProxyConfig().getRocketmqConfigs().get(config.getBrokerCluster()); String nameServers = StringUtils.join(rmqConfig.getNamesrvAddrs().iterator(), ";"); consumer.setNamesrvAddr(nameServers); //必须这样设置。一个instance只能有一个group consumer.setInstanceName(RmqCidMaker.makeCid(SECOND_LOWLEVEL_CONSUMER_INSTANCE_NUM, config.getGroup(), config.getBrokerCluster(), config.getcProxyConfig().getProxyCluster(), config.getInstance(), LOW_LEVEL_V2));//根据instancename 隔离highlevel和lowlevel的netty资源 //fix 2018.6.14 lowlevel实例过多,netty线程未共享 consumer.setClientCallbackExecutorThreads(1); consumer.setMessageQueueListener(this); consumer.setPersistConsumerOffsetInterval(rmqConfig.getPersistConsumerOffsetInterval()); consumer.setPollNameServerInterval(rmqConfig.getPollNameServerInterval()); consumer.setRegisterTopics(new HashSet<>(config.getTopicNames())); try { consumer.start(); } catch (MQClientException e) { LogUtils.MAIN_LOGGER.error(String.format("start RmqFetcher failed. group@Cluster=%s,consumerId=%s", config.getGroupBrokerCluster(), cid), e); consumer.shutdown(); return false; } this.consumer = consumer; LogUtils.logMainInfo("RmqFetcher.start, this={}", this); return true; }
Example 8
Source File: PullConsumer.java From blog with BSD 2-Clause "Simplified" License | 5 votes |
public static void main(String[] args) throws MQClientException { DefaultMQPullConsumer consumer = new DefaultMQPullConsumer("please_rename_unique_group_name_5"); consumer.setNamesrvAddr("127.0.0.1:9876"); consumer.start(); Set<MessageQueue> mqs = consumer.fetchSubscribeMessageQueues("broker-a"); for (MessageQueue mq : mqs) { System.out.printf("Consume from the queue: %s%n", mq); SINGLE_MQ: while (true) { try { PullResult pullResult = consumer.pullBlockIfNotFound(mq, null, getMessageQueueOffset(mq), 32); System.out.printf("%s%n", pullResult); putMessageQueueOffset(mq, pullResult.getNextBeginOffset()); switch (pullResult.getPullStatus()) { case FOUND: break; case NO_MATCHED_MSG: break; case NO_NEW_MSG: break SINGLE_MQ; case OFFSET_ILLEGAL: break; default: break; } } catch (Exception e) { e.printStackTrace(); } } } consumer.shutdown(); }
Example 9
Source File: AclClient.java From rocketmq with Apache License 2.0 | 5 votes |
public static void pullConsumer() throws MQClientException { DefaultMQPullConsumer consumer = new DefaultMQPullConsumer("please_rename_unique_group_name_6", getAclRPCHook()); consumer.setNamesrvAddr("127.0.0.1:9876"); consumer.start(); Set<MessageQueue> mqs = consumer.fetchSubscribeMessageQueues("TopicTest"); for (MessageQueue mq : mqs) { System.out.printf("Consume from the queue: %s%n", mq); SINGLE_MQ: while (true) { try { PullResult pullResult = consumer.pullBlockIfNotFound(mq, null, getMessageQueueOffset(mq), 32); System.out.printf("%s%n", pullResult); putMessageQueueOffset(mq, pullResult.getNextBeginOffset()); printBody(pullResult); switch (pullResult.getPullStatus()) { case FOUND: break; case NO_MATCHED_MSG: break; case NO_NEW_MSG: break SINGLE_MQ; case OFFSET_ILLEGAL: break; default: break; } } catch (Exception e) { e.printStackTrace(); } } } consumer.shutdown(); }
Example 10
Source File: PullConsumer.java From rocketmq with Apache License 2.0 | 5 votes |
public static void main(String[] args) throws MQClientException { DefaultMQPullConsumer consumer = new DefaultMQPullConsumer("please_rename_unique_group_name_5"); consumer.setNamesrvAddr("127.0.0.1:9876"); consumer.start(); Set<MessageQueue> mqs = consumer.fetchSubscribeMessageQueues("broker-a"); for (MessageQueue mq : mqs) { System.out.printf("Consume from the queue: %s%n", mq); SINGLE_MQ: while (true) { try { PullResult pullResult = consumer.pullBlockIfNotFound(mq, null, getMessageQueueOffset(mq), 32); System.out.printf("%s%n", pullResult); putMessageQueueOffset(mq, pullResult.getNextBeginOffset()); switch (pullResult.getPullStatus()) { case FOUND: break; case NO_MATCHED_MSG: break; case NO_NEW_MSG: break SINGLE_MQ; case OFFSET_ILLEGAL: break; default: break; } } catch (Exception e) { e.printStackTrace(); } } } consumer.shutdown(); }
Example 11
Source File: PullConsumerWithNamespace.java From rocketmq with Apache License 2.0 | 5 votes |
public static void main(String[] args) throws Exception { DefaultMQPullConsumer pullConsumer = new DefaultMQPullConsumer("InstanceTest", "cidTest"); pullConsumer.setNamesrvAddr("127.0.0.1:9876"); pullConsumer.start(); Set<MessageQueue> mqs = pullConsumer.fetchSubscribeMessageQueues("topicTest"); for (MessageQueue mq : mqs) { System.out.printf("Consume from the topic: %s, queue: %s%n", mq.getTopic(), mq); SINGLE_MQ: while (true) { try { PullResult pullResult = pullConsumer.pullBlockIfNotFound(mq, null, getMessageQueueOffset(mq), 32); System.out.printf("%s%n", pullResult); putMessageQueueOffset(mq, pullResult.getNextBeginOffset()); switch (pullResult.getPullStatus()) { case FOUND: dealWithPullResult(pullResult); break; case NO_MATCHED_MSG: break; case NO_NEW_MSG: break SINGLE_MQ; case OFFSET_ILLEGAL: break; default: break; } } catch (Exception e) { e.printStackTrace(); } } } pullConsumer.shutdown(); }
Example 12
Source File: ConsumerFactory.java From rocketmq with Apache License 2.0 | 5 votes |
public static DefaultMQPullConsumer getRMQPullConsumer(String nsAddr, String consumerGroup) throws Exception { DefaultMQPullConsumer defaultMQPullConsumer = new DefaultMQPullConsumer(consumerGroup); defaultMQPullConsumer.setInstanceName(UUID.randomUUID().toString()); defaultMQPullConsumer.setNamesrvAddr(nsAddr); defaultMQPullConsumer.start(); return defaultMQPullConsumer; }
Example 13
Source File: SqlFilterIT.java From rocketmq with Apache License 2.0 | 4 votes |
@Test public void testFilterPullConsumer() throws Exception { int msgSize = 16; String group = initConsumerGroup(); MessageSelector selector = MessageSelector.bySql("(TAGS is not null and TAGS in ('TagA', 'TagB'))"); DefaultMQPullConsumer consumer = new DefaultMQPullConsumer(group); consumer.setNamesrvAddr(nsAddr); consumer.start(); Thread.sleep(3000); producer.send("TagA", msgSize); producer.send("TagB", msgSize); producer.send("TagC", msgSize); Assert.assertEquals("Not all sent succeeded", msgSize * 3, producer.getAllUndupMsgBody().size()); List<String> receivedMessage = new ArrayList<>(2); Set<MessageQueue> mqs = consumer.fetchSubscribeMessageQueues(topic); for (MessageQueue mq : mqs) { SINGLE_MQ: while (true) { try { PullResult pullResult = consumer.pull(mq, selector, getMessageQueueOffset(mq), 32); putMessageQueueOffset(mq, pullResult.getNextBeginOffset()); switch (pullResult.getPullStatus()) { case FOUND: List<MessageExt> msgs = pullResult.getMsgFoundList(); for (MessageExt msg : msgs) { receivedMessage.add(new String(msg.getBody())); } break; case NO_MATCHED_MSG: break; case NO_NEW_MSG: break SINGLE_MQ; case OFFSET_ILLEGAL: break; default: break; } } catch (Exception e) { e.printStackTrace(); } } } assertThat(receivedMessage.size()).isEqualTo(msgSize * 2); }