org.apache.rocketmq.client.consumer.DefaultMQPushConsumer Java Examples
The following examples show how to use
org.apache.rocketmq.client.consumer.DefaultMQPushConsumer.
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: PushConsumer2.java From blog with BSD 2-Clause "Simplified" License | 6 votes |
public static void main(String[] args) throws MQClientException { DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("ConsumerGroupName"); consumer.setNamesrvAddr("192.168.237.128:9876"); consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET); consumer.subscribe("TopicTest1234", "*"); consumer.registerMessageListener(new MessageListenerOrderly() { @Override public ConsumeOrderlyStatus consumeMessage(List<MessageExt> msgs, ConsumeOrderlyContext context) { System.out.printf("Time [" + new Date().toString() + "]," +Thread.currentThread().getName() + "Receive New Messages :" + msgs + "%n"); return ConsumeOrderlyStatus.SUCCESS; } }); consumer.start(); }
Example #2
Source File: Consumer.java From DDMQ with Apache License 2.0 | 6 votes |
public static void main(String[] args) throws InterruptedException, MQClientException, IOException { DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("ConsumerGroupNamecc4"); ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); File classFile = new File(classLoader.getResource("MessageFilterImpl.java").getFile()); String filterCode = MixAll.file2String(classFile); consumer.subscribe("TopicTest", "org.apache.rocketmq.example.filter.MessageFilterImpl", filterCode); consumer.registerMessageListener(new MessageListenerConcurrently() { @Override public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) { System.out.printf("%s Receive New Messages: %s %n", Thread.currentThread().getName(), msgs); return ConsumeConcurrentlyStatus.CONSUME_SUCCESS; } }); consumer.start(); System.out.printf("Consumer Started.%n"); }
Example #3
Source File: Consumer.java From rocketmq-read with Apache License 2.0 | 6 votes |
public static void main(String[] args) throws InterruptedException, MQClientException, IOException { DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("ConsumerGroupNamecc4"); ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); File classFile = new File(classLoader.getResource("MessageFilterImpl.java").getFile()); String filterCode = MixAll.file2String(classFile); consumer.subscribe("TopicTest", "org.apache.rocketmq.example.filter.MessageFilterImpl", filterCode); consumer.registerMessageListener(new MessageListenerConcurrently() { @Override public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) { System.out.printf("%s Receive New Messages: %s %n", Thread.currentThread().getName(), msgs); return ConsumeConcurrentlyStatus.CONSUME_SUCCESS; } }); consumer.start(); System.out.printf("Consumer Started.%n"); }
Example #4
Source File: PushConsumer.java From rocketmq-read with Apache License 2.0 | 6 votes |
public static void main(String[] args) throws InterruptedException, MQClientException { DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("please_rename_unique_group_name_1"); consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET); consumer.setMessageModel(MessageModel.BROADCASTING); consumer.subscribe("TopicTest", "TagA || TagC || TagD"); consumer.registerMessageListener(new MessageListenerConcurrently() { @Override public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) { System.out.printf("%s Receive New Messages: %s %n", Thread.currentThread().getName(), msgs); return ConsumeConcurrentlyStatus.CONSUME_SUCCESS; } }); consumer.start(); System.out.printf("Broadcast Consumer Started.%n"); }
Example #5
Source File: RocketMQBenchmarkDriver.java From openmessaging-benchmark with Apache License 2.0 | 6 votes |
@Override public CompletableFuture<BenchmarkConsumer> createConsumer(final String topic, final String subscriptionName, final ConsumerCallback consumerCallback) { DefaultMQPushConsumer rmqConsumer = new DefaultMQPushConsumer(subscriptionName); rmqConsumer.setNamesrvAddr(this.rmqClientConfig.namesrvAddr); rmqConsumer.setInstanceName("ConsumerInstance" + getRandomString()); if(null != this.rmqClientConfig.vipChannelEnabled){ rmqConsumer.setVipChannelEnabled(this.rmqClientConfig.vipChannelEnabled); } try { rmqConsumer.subscribe(topic, "*"); rmqConsumer.registerMessageListener((MessageListenerConcurrently) (msgs, context) -> { for (MessageExt message : msgs) { consumerCallback.messageReceived(message.getBody(), message.getBornTimestamp()); } return ConsumeConcurrentlyStatus.CONSUME_SUCCESS; }); rmqConsumer.start(); } catch (MQClientException e) { log.error("Failed to create consumer instance.", e); } return CompletableFuture.completedFuture(new RocketMQBenchmarkConsumer(rmqConsumer)); }
Example #6
Source File: DefaultMQPushConsumerImplTest.java From rocketmq-read with Apache License 2.0 | 6 votes |
@Test public void checkConfigTest() throws MQClientException { //test type thrown.expect(MQClientException.class); //test message thrown.expectMessage("consumeThreadMin (10) is larger than consumeThreadMax (9)"); DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("test_consumer_group"); consumer.setConsumeThreadMin(10); consumer.setConsumeThreadMax(9); consumer.registerMessageListener(new MessageListenerConcurrently() { public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) { System.out.println(" Receive New Messages: " + msgs); return ConsumeConcurrentlyStatus.CONSUME_SUCCESS; } }); DefaultMQPushConsumerImpl defaultMQPushConsumerImpl = new DefaultMQPushConsumerImpl(consumer, null); defaultMQPushConsumerImpl.start(); }
Example #7
Source File: TagFilterConsumer.java From blog with MIT License | 6 votes |
public static void main(String[] args) throws MQClientException { /** TODO: 创建消息消费者 */ DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("Tag_Filter_Consumer"); /** TODO: 设置 nameserver 地址 */ consumer.setNamesrvAddr("127.0.0.1:9876"); /** TODO: 订阅主题, 指定 Tag 过滤 */ consumer.subscribe("TagFilterTest", "TagA || TagB"); consumer.registerMessageListener( new MessageListenerConcurrently() { @Override public ConsumeConcurrentlyStatus consumeMessage( List<MessageExt> msgs, ConsumeConcurrentlyContext context) { msgs.forEach(o -> System.out.println(new String(o.getBody()))); return ConsumeConcurrentlyStatus.CONSUME_SUCCESS; } }); /** TODO: 连接 nameserver */ consumer.start(); System.out.println("Consumer 已开启============"); }
Example #8
Source File: PushConsumer.java From rocketmq-all-4.1.0-incubating with Apache License 2.0 | 6 votes |
public static void main(String[] args) throws InterruptedException, MQClientException { DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("CID_JODIE_1"); consumer.subscribe("Jodie_topic_1023", "*"); consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET); //wrong time format 2017_0422_221800 consumer.setConsumeTimestamp("20170422221800"); consumer.registerMessageListener(new MessageListenerConcurrently() { /** */ @Override public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) { System.out.printf(Thread.currentThread().getName() + " Receive New Messages: " + msgs + "%n"); return ConsumeConcurrentlyStatus.CONSUME_SUCCESS; } }); consumer.start(); System.out.printf("Consumer Started.%n"); }
Example #9
Source File: Consumer.java From rocketmq-all-4.1.0-incubating with Apache License 2.0 | 6 votes |
public static void main(String[] args) throws InterruptedException, MQClientException, IOException { DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("ConsumerGroupNamecc4"); ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); File classFile = new File(classLoader.getResource("MessageFilterImpl.java").getFile()); String filterCode = MixAll.file2String(classFile); consumer.subscribe("TopicTest", "org.apache.rocketmq.example.filter.MessageFilterImpl", filterCode); consumer.registerMessageListener(new MessageListenerConcurrently() { @Override public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) { System.out.printf(Thread.currentThread().getName() + " Receive New Messages: " + msgs + "%n"); return ConsumeConcurrentlyStatus.CONSUME_SUCCESS; } }); consumer.start(); System.out.printf("Consumer Started.%n"); }
Example #10
Source File: PushConsumer.java From DDMQ with Apache License 2.0 | 6 votes |
public static void main(String[] args) throws InterruptedException, MQClientException { DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("CID_JODIE_1"); consumer.subscribe("Jodie_topic_1023", "*"); consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET); //wrong time format 2017_0422_221800 consumer.setConsumeTimestamp("20170422221800"); consumer.registerMessageListener(new MessageListenerConcurrently() { @Override public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) { System.out.printf("%s Receive New Messages: %s %n", Thread.currentThread().getName(), msgs); return ConsumeConcurrentlyStatus.CONSUME_SUCCESS; } }); consumer.start(); System.out.printf("Consumer Started.%n"); }
Example #11
Source File: NotifySendConsumer.java From order-charge-notify with Apache License 2.0 | 6 votes |
@PostConstruct public void init() { defaultMQPushConsumer = new DefaultMQPushConsumer(MessageProtocolConst.ORDER_RESULT_NOTIFY_TOPIC.getConsumerGroup()); defaultMQPushConsumer.setNamesrvAddr(nameSrvAddr); // 从头开始消费 defaultMQPushConsumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET); // 消费模式:集群模式 defaultMQPushConsumer.setMessageModel(MessageModel.CLUSTERING); // 注册监听器 defaultMQPushConsumer.registerMessageListener(messageListener); // 订阅所有消息 try { defaultMQPushConsumer.subscribe(MessageProtocolConst.ORDER_RESULT_NOTIFY_TOPIC.getTopic(), "*"); defaultMQPushConsumer.start(); } catch (MQClientException e) { LOGGER.error("[订单结果通知消息消费者]--NotifySendConsumer加载异常!e={}", LogExceptionWapper.getStackTrace(e)); throw new RuntimeException("[订单结果通知消息消费者]--NotifySendConsumer加载异常!", e); } LOGGER.info("[订单结果通知消息消费者]--NotifySendConsumer加载完成!"); }
Example #12
Source File: BroadcastConsumer.java From javatech with Creative Commons Attribution Share Alike 4.0 International | 6 votes |
public static void main(String[] args) throws Exception { DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("example_group_name"); consumer.setNamesrvAddr(RocketConfig.HOST); consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET); // set to broadcast mode consumer.setMessageModel(MessageModel.BROADCASTING); consumer.subscribe("TopicTest", "TagA || TagC || TagD"); consumer.registerMessageListener(new MessageListenerConcurrently() { @Override public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) { System.out.printf(Thread.currentThread().getName() + " Receive New Messages: " + msgs + "%n"); return ConsumeConcurrentlyStatus.CONSUME_SUCCESS; } }); consumer.start(); System.out.printf("Broadcast Consumer Started.%n"); }
Example #13
Source File: BroadcastConsumer.java From spring-boot-starter-samples with Apache License 2.0 | 6 votes |
public static void main(String[] args) throws Exception { DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("example_group_name"); consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET); // set to broadcast mode consumer.setMessageModel(MessageModel.BROADCASTING); consumer.subscribe("TopicTest", "TagA || TagC || TagD"); consumer.registerMessageListener(new MessageListenerConcurrently() { @Override public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) { System.out.printf(Thread.currentThread().getName() + " Receive New Messages: " + msgs + "%n"); return ConsumeConcurrentlyStatus.CONSUME_SUCCESS; } }); consumer.start(); System.out.printf("Broadcast Consumer Started.%n"); }
Example #14
Source File: PushConsumer.java From rocketmq-all-4.1.0-incubating with Apache License 2.0 | 6 votes |
public static void main(String[] args) throws InterruptedException, MQClientException { DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("please_rename_unique_group_name_1"); consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET); consumer.setMessageModel(MessageModel.BROADCASTING); consumer.subscribe("TopicTest", "TagA || TagC || TagD"); consumer.registerMessageListener(new MessageListenerConcurrently() { @Override public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) { System.out.printf(Thread.currentThread().getName() + " Receive New Messages: " + msgs + "%n"); return ConsumeConcurrentlyStatus.CONSUME_SUCCESS; } }); consumer.start(); System.out.printf("Broadcast Consumer Started.%n"); }
Example #15
Source File: DefaultMQPushConsumerImplTest.java From rocketmq-4.3.0 with Apache License 2.0 | 6 votes |
@Test public void checkConfigTest() throws MQClientException { //test type thrown.expect(MQClientException.class); //test message thrown.expectMessage("consumeThreadMin (10) is larger than consumeThreadMax (9)"); DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("test_consumer_group"); consumer.setConsumeThreadMin(10); consumer.setConsumeThreadMax(9); consumer.registerMessageListener(new MessageListenerConcurrently() { public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) { System.out.println(" Receive New Messages: " + msgs); return ConsumeConcurrentlyStatus.CONSUME_SUCCESS; } }); DefaultMQPushConsumerImpl defaultMQPushConsumerImpl = new DefaultMQPushConsumerImpl(consumer, null); defaultMQPushConsumerImpl.start(); }
Example #16
Source File: PushConsumer.java From rocketmq_trans_message with Apache License 2.0 | 6 votes |
public static void main(String[] args) throws InterruptedException, MQClientException { DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("CID_JODIE_1"); consumer.subscribe("Jodie_topic_1023", "*"); consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET); consumer.setNamesrvAddr("testserver001:9876;testserver004:9876"); consumer.registerMessageListener(new MessageListenerConcurrently() { /** */ @Override public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) { System.out.printf(Thread.currentThread().getName() + " Receive New Messages: " + msgs + "%n"); return ConsumeConcurrentlyStatus.CONSUME_SUCCESS; } }); consumer.start(); System.out.printf("Consumer Started.%n"); }
Example #17
Source File: PushConsumer.java From rocketmq with Apache License 2.0 | 6 votes |
public static void main(String[] args) throws InterruptedException, MQClientException { DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("CID_JODIE_1"); consumer.subscribe("Jodie_topic_1023", "*"); consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET); consumer.registerMessageListener(new MessageListenerConcurrently() { /** */ @Override public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) { System.out.printf(Thread.currentThread().getName() + " Receive New Messages: " + msgs + "%n"); return ConsumeConcurrentlyStatus.CONSUME_SUCCESS; } }); consumer.start(); System.out.printf("Consumer Started.%n"); }
Example #18
Source File: TagFilterConsumer.java From rocketmq with Apache License 2.0 | 6 votes |
public static void main(String[] args) throws InterruptedException, MQClientException, IOException { DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("please_rename_unique_group_name"); consumer.subscribe("TagFilterTest", "TagA || TagC"); consumer.registerMessageListener(new MessageListenerConcurrently() { @Override public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) { System.out.printf("%s Receive New Messages: %s %n", Thread.currentThread().getName(), msgs); return ConsumeConcurrentlyStatus.CONSUME_SUCCESS; } }); consumer.start(); System.out.printf("Consumer Started.%n"); }
Example #19
Source File: PushConsumerImpl.java From DDMQ with Apache License 2.0 | 5 votes |
public PushConsumerImpl(final KeyValue properties) { this.rocketmqPushConsumer = new DefaultMQPushConsumer(); this.properties = properties; this.clientConfig = BeanUtils.populate(properties, ClientConfig.class); String accessPoints = clientConfig.getOmsAccessPoints(); if (accessPoints == null || accessPoints.isEmpty()) { throw new OMSRuntimeException("-1", "OMS AccessPoints is null or empty."); } this.rocketmqPushConsumer.setNamesrvAddr(accessPoints.replace(',', ';')); String consumerGroup = clientConfig.getRmqConsumerGroup(); if (null == consumerGroup || consumerGroup.isEmpty()) { throw new OMSRuntimeException("-1", "Consumer Group is necessary for RocketMQ, please set it."); } this.rocketmqPushConsumer.setConsumerGroup(consumerGroup); this.rocketmqPushConsumer.setMaxReconsumeTimes(clientConfig.getRmqMaxRedeliveryTimes()); this.rocketmqPushConsumer.setConsumeTimeout(clientConfig.getRmqMessageConsumeTimeout()); this.rocketmqPushConsumer.setConsumeThreadMax(clientConfig.getRmqMaxConsumeThreadNums()); this.rocketmqPushConsumer.setConsumeThreadMin(clientConfig.getRmqMinConsumeThreadNums()); String consumerId = OMSUtil.buildInstanceName(); this.rocketmqPushConsumer.setInstanceName(consumerId); properties.put(PropertyKeys.CONSUMER_ID, consumerId); this.rocketmqPushConsumer.registerMessageListener(new MessageListenerImpl()); }
Example #20
Source File: RocketMQMessageListenerContainer.java From rocketmq-spring-boot-starter with Apache License 2.0 | 5 votes |
private void startAllListener() { pushConsumerMap.forEach((topic, consumer) -> { try { consumer.start(); runningMap.put(topic, consumer); } catch (MQClientException e) { logger.error(e.getErrorMessage()); Map.Entry<DefaultMQPushConsumer, String> errEntry = new AbstractMap.SimpleEntry<>(consumer, e.getErrorMessage()); startErrMap.put(topic, errEntry); } }); }
Example #21
Source File: RocketMQPushConsumerFactory.java From rocketmq-spring-boot-starter with Apache License 2.0 | 5 votes |
@Override public void afterPropertiesSet() { pushConsumers = new ArrayList<>(); pushConsumerMap = new HashMap<>(16); if (listenerFactory == null) { listenerFactory = new SimpleListenerFactory(); listenerFactory.setApplicationContext(this.applicationContext); listenerFactory.afterPropertiesSet(); } listenerFactory.getAllListeners().forEach((topic, consumerListener) -> { DefaultMQPushConsumer pushConsumer = createDefaultMQPushConsumer(consumerListener); pushConsumers.add(pushConsumer); pushConsumerMap.put(topic, pushConsumer); }); }
Example #22
Source File: SecKillChargeOrderConsumer.java From seckill-rocketmq with Apache License 2.0 | 5 votes |
@PostConstruct public void init() { defaultMQPushConsumer = new DefaultMQPushConsumer( MessageProtocolConst.SECKILL_CHARGE_ORDER_TOPIC.getConsumerGroup(), new AclClientRPCHook(new SessionCredentials(aclAccessKey, aclAccessSecret)), // 平均分配队列算法,hash new AllocateMessageQueueAveragely()); defaultMQPushConsumer.setNamesrvAddr(namesrvConfig.nameSrvAddr()); // 从头开始消费 defaultMQPushConsumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET); // 消费模式:集群模式 // 集群:同一条消息 只会被一个消费者节点消费到 // 广播:同一条消息 每个消费者都会消费到 defaultMQPushConsumer.setMessageModel(MessageModel.CLUSTERING); // 注册监听器 defaultMQPushConsumer.registerMessageListener(messageListener); // 设置每次拉取的消息量,默认为1 defaultMQPushConsumer.setConsumeMessageBatchMaxSize(1); // 订阅所有消息 try { defaultMQPushConsumer.subscribe(MessageProtocolConst.SECKILL_CHARGE_ORDER_TOPIC.getTopic(), "*"); // 启动消费者 defaultMQPushConsumer.start(); } catch (MQClientException e) { LOGGER.error("[秒杀下单消费者]--SecKillChargeOrderConsumer加载异常!e={}", LogExceptionWapper.getStackTrace(e)); throw new RuntimeException("[秒杀下单消费者]--SecKillChargeOrderConsumer加载异常!", e); } LOGGER.info("[秒杀下单消费者]--SecKillChargeOrderConsumer加载完成!"); }
Example #23
Source File: RMQNormalConsumer.java From rocketmq with Apache License 2.0 | 5 votes |
public void create() { consumer = new DefaultMQPushConsumer(consumerGroup); consumer.setInstanceName(RandomUtil.getStringByUUID()); consumer.setNamesrvAddr(nsAddr); try { consumer.subscribe(topic, subExpression); } catch (MQClientException e) { logger.error("consumer subscribe failed!"); e.printStackTrace(); } consumer.setMessageListener(listner); }
Example #24
Source File: AliyunMqConfiguration.java From paascloud-master with Apache License 2.0 | 5 votes |
/** * Default mq push consumer default mq push consumer. * * @return the default mq push consumer * * @throws MQClientException the mq client exception */ @Bean public DefaultMQPushConsumer defaultMQPushConsumer() throws MQClientException { DefaultMQPushConsumer consumer = new DefaultMQPushConsumer(paascloudProperties.getAliyun().getRocketMq().getConsumerGroup()); consumer.setNamesrvAddr(paascloudProperties.getAliyun().getRocketMq().getNamesrvAddr()); consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_LAST_OFFSET); String[] strArray = AliyunMqTopicConstants.ConsumerTopics.UAC.split(GlobalConstant.Symbol.COMMA); for (String aStrArray : strArray) { String[] topicArray = aStrArray.split(GlobalConstant.Symbol.AT); String topic = topicArray[0]; String tags = topicArray[1]; if (PublicUtil.isEmpty(tags)) { tags = "*"; } consumer.subscribe(topic, tags); log.info("RocketMq UacPushConsumer topic = {}, tags={}", topic, tags); } consumer.registerMessageListener(uacPushMessageListener); consumer.setConsumeThreadMax(2); consumer.setConsumeThreadMin(2); taskExecutor.execute(() -> { try { Thread.sleep(5000); consumer.start(); log.info("RocketMq UacPushConsumer OK."); } catch (InterruptedException | MQClientException e) { log.error("RocketMq OpcPushConsumer, 出现异常={}", e.getMessage(), e); } }); return consumer; }
Example #25
Source File: RocketMQMessageListenerContainer.java From rocketmq-spring-boot-starter with Apache License 2.0 | 5 votes |
@Override public OperationResult suspendConsumer(String topic) { return analyzeResult(topic, OperatinType.SUSPEND, () -> { DefaultMQPushConsumer consumer = runningMap.get(topic); consumer.suspend(); runningMap.remove(topic); removedMap.put(topic, consumer); }); }
Example #26
Source File: Consumer.java From rocketmq-read with Apache License 2.0 | 5 votes |
public static void main(String[] args) throws MQClientException { DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("please_rename_unique_group_name_3"); consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET); consumer.subscribe("TopicTest", "TagA || TagC || TagD"); consumer.registerMessageListener(new MessageListenerOrderly() { AtomicLong consumeTimes = new AtomicLong(0); @Override public ConsumeOrderlyStatus consumeMessage(List<MessageExt> msgs, ConsumeOrderlyContext context) { context.setAutoCommit(false); System.out.printf("%s Receive New Messages: %s %n", Thread.currentThread().getName(), msgs); this.consumeTimes.incrementAndGet(); if ((this.consumeTimes.get() % 2) == 0) { return ConsumeOrderlyStatus.SUCCESS; } else if ((this.consumeTimes.get() % 3) == 0) { return ConsumeOrderlyStatus.ROLLBACK; } else if ((this.consumeTimes.get() % 4) == 0) { return ConsumeOrderlyStatus.COMMIT; } else if ((this.consumeTimes.get() % 5) == 0) { context.setSuspendCurrentQueueTimeMillis(3000); return ConsumeOrderlyStatus.SUSPEND_CURRENT_QUEUE_A_MOMENT; } return ConsumeOrderlyStatus.SUCCESS; } }); consumer.start(); System.out.printf("Consumer Started.%n"); }
Example #27
Source File: RMQNormalConsumer.java From rocketmq-all-4.1.0-incubating with Apache License 2.0 | 5 votes |
public void create() { consumer = new DefaultMQPushConsumer(consumerGroup); consumer.setInstanceName(RandomUtil.getStringByUUID()); consumer.setNamesrvAddr(nsAddr); try { consumer.subscribe(topic, subExpression); } catch (MQClientException e) { logger.error("consumer subscribe failed!"); e.printStackTrace(); } consumer.setMessageListener(listner); }
Example #28
Source File: PushConsumerWithNamespace.java From rocketmq with Apache License 2.0 | 5 votes |
public static void main(String[] args) throws Exception { DefaultMQPushConsumer defaultMQPushConsumer = new DefaultMQPushConsumer("InstanceTest", "cidTest"); defaultMQPushConsumer.setNamesrvAddr("127.0.0.1:9876"); defaultMQPushConsumer.subscribe("topicTest", "*"); defaultMQPushConsumer.registerMessageListener((MessageListenerConcurrently)(msgs, context) -> { msgs.stream().forEach((msg) -> { System.out.printf("Msg topic is:%s, MsgId is:%s, reconsumeTimes is:%s%n", msg.getTopic() , msg.getMsgId(), msg.getReconsumeTimes()); }); return ConsumeConcurrentlyStatus.RECONSUME_LATER; }); defaultMQPushConsumer.start(); }
Example #29
Source File: PushConsumerImpl.java From rocketmq-4.3.0 with Apache License 2.0 | 5 votes |
public PushConsumerImpl(final KeyValue properties) { this.rocketmqPushConsumer = new DefaultMQPushConsumer(); this.properties = properties; this.clientConfig = BeanUtils.populate(properties, ClientConfig.class); if ("true".equalsIgnoreCase(System.getenv("OMS_RMQ_DIRECT_NAME_SRV"))) { String accessPoints = clientConfig.getAccessPoints(); if (accessPoints == null || accessPoints.isEmpty()) { throw new OMSRuntimeException("-1", "OMS AccessPoints is null or empty."); } this.rocketmqPushConsumer.setNamesrvAddr(accessPoints.replace(',', ';')); } String consumerGroup = clientConfig.getConsumerId(); if (null == consumerGroup || consumerGroup.isEmpty()) { throw new OMSRuntimeException("-1", "Consumer Group is necessary for RocketMQ, please set it."); } this.rocketmqPushConsumer.setConsumerGroup(consumerGroup); this.rocketmqPushConsumer.setMaxReconsumeTimes(clientConfig.getRmqMaxRedeliveryTimes()); this.rocketmqPushConsumer.setConsumeTimeout(clientConfig.getRmqMessageConsumeTimeout()); this.rocketmqPushConsumer.setConsumeThreadMax(clientConfig.getRmqMaxConsumeThreadNums()); this.rocketmqPushConsumer.setConsumeThreadMin(clientConfig.getRmqMinConsumeThreadNums()); String consumerId = OMSUtil.buildInstanceName(); this.rocketmqPushConsumer.setInstanceName(consumerId); properties.put(OMSBuiltinKeys.CONSUMER_ID, consumerId); this.rocketmqPushConsumer.setLanguage(LanguageCode.OMS); this.rocketmqPushConsumer.registerMessageListener(new MessageListenerImpl()); }
Example #30
Source File: Consumer.java From rocketmq with Apache License 2.0 | 5 votes |
public static void main(String[] args) throws MQClientException { DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("please_rename_unique_group_name_3"); consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET); consumer.subscribe("TopicTest", "TagA || TagC || TagD"); consumer.setNamesrvAddr("127.0.0.1:9876"); // TODO add by yunai consumer.registerMessageListener(new MessageListenerOrderly() { AtomicLong consumeTimes = new AtomicLong(0); @Override public ConsumeOrderlyStatus consumeMessage(List<MessageExt> msgs, ConsumeOrderlyContext context) { context.setAutoCommit(false); System.out.printf(Thread.currentThread().getName() + " Receive New Messages: " + msgs + "%n"); this.consumeTimes.incrementAndGet(); if ((this.consumeTimes.get() % 2) == 0) { return ConsumeOrderlyStatus.SUCCESS; } else if ((this.consumeTimes.get() % 3) == 0) { return ConsumeOrderlyStatus.ROLLBACK; } else if ((this.consumeTimes.get() % 4) == 0) { return ConsumeOrderlyStatus.COMMIT; } else if ((this.consumeTimes.get() % 5) == 0) { context.setSuspendCurrentQueueTimeMillis(3000); return ConsumeOrderlyStatus.SUSPEND_CURRENT_QUEUE_A_MOMENT; } return ConsumeOrderlyStatus.SUCCESS; } }); consumer.start(); System.out.printf("Consumer Started.%n"); }