Java Code Examples for org.apache.rocketmq.client.consumer.DefaultMQPushConsumer#setNamesrvAddr()
The following examples show how to use
org.apache.rocketmq.client.consumer.DefaultMQPushConsumer#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: 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 2
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("please_rename_unique_group_name_1"); consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET); consumer.setMessageModel(MessageModel.BROADCASTING); // consumer.subscribe("TopicTest", "TagA || TagC || TagD"); consumer.subscribe("TopicTest", "*"); consumer.setNamesrvAddr("127.0.0.1:9876"); // TODO add by yunai 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 3
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 4
Source File: PushConsumer.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("TopicTest7", "*"); consumer.setConsumeThreadMin(1); consumer.setConsumeThreadMax(1); consumer.registerMessageListener(new MessageListenerConcurrently() { public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) { System.out.printf("Time [" + new Date().toString() + "]," +Thread.currentThread().getName() + "Receive New Messages :" + msgs + "%n"); return ConsumeConcurrentlyStatus.CONSUME_SUCCESS; } }); consumer.start(); }
Example 5
Source File: RMQNormalConsumer.java From rocketmq_trans_message 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 6
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 7
Source File: BroadcastConsumer2.java From blog with MIT License | 5 votes |
public static void main(String[] args) throws MQClientException { /** TODO: 创建消息消费者对象 */ DefaultMQPushConsumer broadcast_consumer = new DefaultMQPushConsumer("Broadcast_Consumer"); /** TODO: 连接 namerserver */ broadcast_consumer.setNamesrvAddr("127.0.0.1:9876"); /** * TODO: 设置 Consumer 类型 * * <p>默认 CLUSTERING(集群模式,一个 Consumer Group 中的 Consumer 实例平均分摊消费消息); * * <p>BROADCASTING(广播模式,一个消息被多个 Consumer 消费) */ broadcast_consumer.setMessageModel(MessageModel.BROADCASTING); /** TODO: 订阅消息主题, 第二个参数表示 指定 订阅的 tag,* 表示全部 */ broadcast_consumer.subscribe("MyTopic", "*"); /** TODO: 注册消息监听 */ broadcast_consumer.registerMessageListener( new MessageListenerConcurrently() { @Override public ConsumeConcurrentlyStatus consumeMessage( List<MessageExt> msgs, ConsumeConcurrentlyContext context) { // TODO: 输出获取的消息 msgs.forEach(o -> System.out.println(new String(o.getBody()))); // TODO: 返回消费结果 return ConsumeConcurrentlyStatus.CONSUME_SUCCESS; } }); /** TODO: 启动 namerserver 连接 */ broadcast_consumer.start(); System.out.println("BroadcastConsumer2 已启动==========="); }
Example 8
Source File: RocketMQPushConsumerFactory.java From rocketmq-spring-boot-starter with Apache License 2.0 | 5 votes |
private DefaultMQPushConsumer createDefaultMQPushConsumer(RocketMQConsumerListener rocketMQConsumerListener) { RocketMQConsumerConfig config = rocketMQConsumerListener.getConsumerConfig(); DefaultMQPushConsumer defaultMQPushConsumer = new DefaultMQPushConsumer(); defaultMQPushConsumer.setNamesrvAddr(nameSrvAddr); defaultMQPushConsumer.setConsumerGroup(config.getConsumerGroup()); Map<String, Class<?>> tags = config.getTags(); StringBuilder tagBuilder = new StringBuilder(); List<String> tmpTags = new ArrayList<>(tags.keySet()); for (int i = 0; i < tmpTags.size(); i++) { if (tmpTags.contains("*") && tmpTags.size() > 1) { throw new IllegalArgumentException("订阅的tag不合法"); } tagBuilder.append(tmpTags.get(i)); if (tmpTags.size() > i + 1) { tagBuilder.append("||"); } } try { defaultMQPushConsumer.subscribe(config.getTopic(), tagBuilder.toString()); defaultMQPushConsumer.subscribe(config.getTopic(), "*"); } catch (MQClientException e) { throw new IllegalArgumentException("订阅语法错误", e); } defaultMQPushConsumer.setMessageModel(config.getMessageModel()); if (config.getConsumeThreadMax() == 0) { defaultMQPushConsumer.setConsumeThreadMax(this.consumeThreadMax); } else { defaultMQPushConsumer.setConsumeThreadMax(config.getConsumeThreadMax()); } if (config.getConsumeThreadMin() == 0) { defaultMQPushConsumer.setConsumeThreadMin(this.consumeThreadMin); } else { defaultMQPushConsumer.setConsumeThreadMin(config.getConsumeThreadMin()); } return defaultMQPushConsumer; }
Example 9
Source File: AbstractTestCase.java From rocketmq-all-4.1.0-incubating with Apache License 2.0 | 5 votes |
protected int consumeMessages(int count,final String key,int timeout) throws MQClientException, InterruptedException { final AtomicInteger cc = new AtomicInteger(0); final CountDownLatch countDownLatch = new CountDownLatch(count); DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("hello"); consumer.setNamesrvAddr(nameServer); consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET); consumer.subscribe(topic, "*"); consumer.registerMessageListener(new MessageListenerConcurrently() { @Override public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) { for (MessageExt msg : msgs) { String body = new String(msg.getBody()); if(key==null||body.contains(key)){ countDownLatch.countDown(); cc.incrementAndGet(); continue; } } return ConsumeConcurrentlyStatus.CONSUME_SUCCESS; } }); consumer.start(); countDownLatch.await(timeout, TimeUnit.SECONDS); consumer.shutdown(); return cc.get(); }
Example 10
Source File: OrderConsumer.java From blog with MIT License | 5 votes |
public static void main(String[] args) throws MQClientException { DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("Order_Consumer"); consumer.setNamesrvAddr("127.0.0.1:9876"); consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET); consumer.subscribe("OrderTopic", "*"); /** TODO: 设置最小消费线程数 */ consumer.setConsumeThreadMin(1); /** TODO: 设置最大消费线程数 */ consumer.setConsumeThreadMax(1); consumer.registerMessageListener( // 顺序消费,一个 Queue 对应一个线程 new MessageListenerOrderly() { @Override public ConsumeOrderlyStatus consumeMessage( List<MessageExt> msgs, ConsumeOrderlyContext context) { System.out.println( Thread.currentThread().getName() + " message: " + new String(msgs.get(0).getBody()) + " QueueID: " + msgs.get(0).getQueueId()); return ConsumeOrderlyStatus.SUCCESS; } }); consumer.start(); System.out.println("Consumer 已启动"); }
Example 11
Source File: TransactionConsumer.java From java-tutorial with MIT License | 5 votes |
public static void main(String[] args) throws MQClientException { DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("transaction_Consumer"); consumer.setNamesrvAddr(NAMESRVADDR); /** * 设置Consumer第一次启动是从队列头部开始消费还是队列尾部开始消费 * 如果非第一次启动,那么按照上次消费的位置继续消费 */ consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET); consumer.subscribe("TopicTransactionTest", "*"); consumer.registerMessageListener(new MessageListenerConcurrently() { @Override public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext concurrentlyContext) { for (MessageExt msg : list) { System.out.println(msg + ",内容:" + new String(msg.getBody())); } try { TimeUnit.SECONDS.sleep(5L); } catch (InterruptedException e) { e.printStackTrace(); //重试 return ConsumeConcurrentlyStatus.RECONSUME_LATER; } //成功 return ConsumeConcurrentlyStatus.CONSUME_SUCCESS; } }); consumer.start(); System.out.println("consumer Started."); }
Example 12
Source File: RocketMQEventConfig.java From enode with MIT License | 5 votes |
@Bean(initMethod = "start", destroyMethod = "shutdown") public DefaultMQPushConsumer exceptionConsumer(RocketMQPublishableExceptionListener publishableExceptionListener) throws MQClientException { DefaultMQPushConsumer defaultMQPushConsumer = new DefaultMQPushConsumer(); defaultMQPushConsumer.setConsumerGroup(QueueProperties.DEFAULT_CONSUMER_GROUP2); defaultMQPushConsumer.setNamesrvAddr(QueueProperties.NAMESRVADDR); defaultMQPushConsumer.subscribe(QueueProperties.EXCEPTION_TOPIC, "*"); defaultMQPushConsumer.setMessageListener(publishableExceptionListener); return defaultMQPushConsumer; }
Example 13
Source File: RocketMQEventConfig.java From enode with MIT License | 5 votes |
@Bean(initMethod = "start", destroyMethod = "shutdown") public DefaultMQPushConsumer applicationConsumer(RocketMQApplicationMessageListener applicationMessageListener) throws MQClientException { DefaultMQPushConsumer defaultMQPushConsumer = new DefaultMQPushConsumer(); defaultMQPushConsumer.setConsumerGroup(QueueProperties.DEFAULT_CONSUMER_GROUP1); defaultMQPushConsumer.setNamesrvAddr(QueueProperties.NAMESRVADDR); defaultMQPushConsumer.subscribe(QueueProperties.APPLICATION_TOPIC, "*"); defaultMQPushConsumer.setMessageListener(applicationMessageListener); return defaultMQPushConsumer; }
Example 14
Source File: RocketMQEventConfig.java From enode with MIT License | 5 votes |
@Bean(initMethod = "start", destroyMethod = "shutdown") public DefaultMQPushConsumer eventConsumer(RocketMQDomainEventListener domainEventListener) throws MQClientException { DefaultMQPushConsumer defaultMQPushConsumer = new DefaultMQPushConsumer(); defaultMQPushConsumer.setConsumerGroup(QueueProperties.DEFAULT_CONSUMER_GROUP); defaultMQPushConsumer.setNamesrvAddr(QueueProperties.NAMESRVADDR); defaultMQPushConsumer.subscribe(QueueProperties.EVENT_TOPIC, "*"); defaultMQPushConsumer.setMessageListener(domainEventListener); return defaultMQPushConsumer; }
Example 15
Source File: ClusterConsumer.java From blog with MIT License | 5 votes |
public static void main(String[] args) throws MQClientException { /** TODO: 创建消息消费者对象 */ DefaultMQPushConsumer cluster_consumer = new DefaultMQPushConsumer("Cluster_Consumer"); /** TODO: 连接 namerserver */ cluster_consumer.setNamesrvAddr("127.0.0.1:9876"); /** TODO: 订阅消息主题, 第二个参数表示 指定 订阅的 tag,* 表示全部 */ cluster_consumer.subscribe("MyTopic", "*"); /** TODO: 一次传入 MessageListener 的 msgs 消息数,默认为 1 条 */ // cluster_consumer.setConsumeMessageBatchMaxSize(10); /** TODO: 注册消息监听 */ cluster_consumer.registerMessageListener( new MessageListenerConcurrently() { @Override public ConsumeConcurrentlyStatus consumeMessage( List<MessageExt> msgs, ConsumeConcurrentlyContext context) { // TODO: 输出获取的消息 System.out.println(new String(msgs.get(0).getBody())); // System.out.println(msgs.size()); // msgs.forEach(o -> System.out.println(new String(o.getBody()))); // TODO: 返回消费结果 return ConsumeConcurrentlyStatus.CONSUME_SUCCESS; } }); /** TODO: 启动 namerserver 连接 */ cluster_consumer.start(); System.out.println("Consumer 已启动==========="); }
Example 16
Source File: RocketmqConsumer.java From myth with Apache License 2.0 | 4 votes |
@Bean public DefaultMQPushConsumer pushConsumer() throws MQClientException { /** * 一个应用创建一个Consumer,由应用来维护此对象,可以设置为全局对象或者单例<br> * 注意:ConsumerGroupName需要由应用来保证唯一 */ DefaultMQPushConsumer consumer = new DefaultMQPushConsumer(env.getProperty("spring.rocketmq.consumerGroupName")); consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET); consumer.setNamesrvAddr(env.getProperty("spring.rocketmq.namesrvAddr")); consumer.setConsumeMessageBatchMaxSize(2); //RECONSUME_LATER的重试次数,RocketMQ默认是16次 consumer.setMaxReconsumeTimes(mythConfig.getRetryMax()); /** * 订阅指定topic下tags */ String topic = env.getProperty("spring.rocketmq.topic"); consumer.subscribe(topic, TAGS); consumer.registerMessageListener((List<MessageExt> msgList, ConsumeConcurrentlyContext context) -> { MessageExt msg = msgList.get(0); try { // 默认msgList里只有一条消息,可以通过设置consumeMessageBatchMaxSize参数来批量接收消息 final byte[] message = msg.getBody(); final Boolean aBoolean = mythMqReceiveService.processMessage(message); } catch (Exception e) { e.printStackTrace(); //重复消费 return ConsumeConcurrentlyStatus.RECONSUME_LATER; } //如果没有return success,consumer会重复消费此信息,直到success。 return ConsumeConcurrentlyStatus.CONSUME_SUCCESS; }); /** * Consumer对象在使用之前必须要调用start初始化,初始化一次即可<br> */ consumer.start(); return consumer; }
Example 17
Source File: Consumer2.java From rocketmq-all-4.1.0-incubating with Apache License 2.0 | 4 votes |
public static void main(String[] args) throws InterruptedException, MQClientException { /* * Instantiate with specified consumer group name. */ /** * Consumer Group,非常重要的概念,后续会慢慢补充 */ DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("please_rename_unique_group_name_4"); //指定NameServer地址,多个地址以 ; 隔开 consumer.setNamesrvAddr("127.0.0.1:9876"); /* * Specify name server addresses. * <p/> * * Alternatively, you may specify name server addresses via exporting environmental variable: NAMESRV_ADDR * <pre> * {@code * consumer.setNamesrvAddr("name-server1-ip:9876;name-server2-ip:9876"); * } * </pre> */ /* * Specify where to start in case the specified consumer group is a brand new one. */ /** * 设置Consumer第一次启动是从队列头部开始消费还是队列尾部开始消费 * 如果非第一次启动,那么按照上次消费的位置继续消费 */ consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET); /* * Subscribe one more more topics to consume. */ consumer.subscribe("TopicTest", "*"); /* * Register callback to execute on arrival of messages fetched from brokers. */ consumer.registerMessageListener(new MessageListenerConcurrently() { @Override public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) { try { MessageExt msg = msgs.get(0); //通过把时间打印出来看看重试的时间效果 重试应该时间10s 30s 1m 2m 3m 4m 5m 6m 7m 8m 9m 10m 20m 30m 1h 2h SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS"); String msgbody = new String(msg.getBody(), "utf-8"); System.out.println(simpleDateFormat.format(new Date())+msgbody + " Receive New Messages: " + msgs); Thread.sleep(200000); //不模拟异常,模拟非常耗时看看效果如何??,观察消息重试情况 观察该字段的值的变化reconsumeTimes } catch (Exception e) { e.printStackTrace(); return ConsumeConcurrentlyStatus.RECONSUME_LATER; } return ConsumeConcurrentlyStatus.CONSUME_SUCCESS; } }); /* * Launch the consumer instance. */ consumer.start(); System.out.printf("Consumer Started.%n"); }
Example 18
Source File: RocketmqConsumer.java From myth with Apache License 2.0 | 4 votes |
/** * Push consumer default mq push consumer. * * @return the default mq push consumer * @throws MQClientException the mq client exception */ @Bean public DefaultMQPushConsumer pushConsumer() throws MQClientException { /** * 一个应用创建一个Consumer,由应用来维护此对象,可以设置为全局对象或者单例<br> * 注意:ConsumerGroupName需要由应用来保证唯一 */ DefaultMQPushConsumer consumer = new DefaultMQPushConsumer(env.getProperty("spring.rocketmq.consumerGroupName")); consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET); consumer.setNamesrvAddr(env.getProperty("spring.rocketmq.namesrvAddr")); consumer.setInstanceName(env.getProperty("spring.rocketmq.instanceName")); //设置批量消费,以提升消费吞吐量,默认是1 consumer.setConsumeMessageBatchMaxSize(3); consumer.subscribe(QUEUE, QUEUE); consumer.registerMessageListener((List<MessageExt> msgList, ConsumeConcurrentlyContext context) -> { MessageExt msg = msgList.get(0); try { // 默认msgList里只有一条消息,可以通过设置consumeMessageBatchMaxSize参数来批量接收消息 final byte[] message = msg.getBody(); LogUtil.debug(LOGGER,()->"springcloud inventory-serivce rocketmq 接收到myth框架发出的信息===="); final Boolean success = mythMqReceiveService.processMessage(message); } catch (Exception e) { e.printStackTrace(); return ConsumeConcurrentlyStatus.RECONSUME_LATER; } //如果没有return success,consumer会重复消费此信息,直到success。 return ConsumeConcurrentlyStatus.CONSUME_SUCCESS; }); consumer.start(); return consumer; }
Example 19
Source File: RocketmqConsumer.java From myth with Apache License 2.0 | 4 votes |
/** * Push consumer default mq push consumer. * * @return the default mq push consumer * @throws MQClientException the mq client exception */ @Bean public DefaultMQPushConsumer pushConsumer() throws MQClientException { /** * 一个应用创建一个Consumer,由应用来维护此对象,可以设置为全局对象或者单例<br> * 注意:ConsumerGroupName需要由应用来保证唯一 */ DefaultMQPushConsumer consumer = new DefaultMQPushConsumer(env.getProperty("spring.rocketmq.consumerGroupName")); consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET); consumer.setNamesrvAddr(env.getProperty("spring.rocketmq.namesrvAddr")); consumer.setInstanceName(env.getProperty("spring.rocketmq.instanceName")); //设置批量消费,以提升消费吞吐量,默认是1 consumer.setConsumeMessageBatchMaxSize(1); /** * 订阅指定topic下tags */ consumer.subscribe(TOPIC, TOPIC); consumer.registerMessageListener((List<MessageExt> msgList, ConsumeConcurrentlyContext context) -> { MessageExt msg = msgList.get(0); try { // 默认msgList里只有一条消息,可以通过设置consumeMessageBatchMaxSize参数来批量接收消息 final byte[] message = msg.getBody(); final Boolean success = mythMqReceiveService.processMessage(message); if (success) { //如果没有return success,consumer会重复消费此信息,直到success。 return ConsumeConcurrentlyStatus.CONSUME_SUCCESS; } } catch (Exception e) { e.printStackTrace(); //重复消费3次 return ConsumeConcurrentlyStatus.RECONSUME_LATER; } return ConsumeConcurrentlyStatus.CONSUME_SUCCESS; }); consumer.start(); return consumer; }
Example 20
Source File: Consumer.java From rocketmq with Apache License 2.0 | 4 votes |
public static void main(String[] args) throws InterruptedException, MQClientException { /* * Instantiate with specified consumer group name. */ DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("please_rename_unique_group_name_4"); consumer.setConsumeThreadMin(1); consumer.setConsumeThreadMax(1); // consumer.setConsumeMessageBatchMaxSize(2); consumer.setNamesrvAddr("127.0.0.1:9876"); // TODO add by yunai /* * Specify name server addresses. * <p/> * * Alternatively, you may specify name server addresses via exporting environmental variable: NAMESRV_ADDR * <pre> * {@code * consumer.setNamesrvAddr("name-server1-ip:9876;name-server2-ip:9876"); * } * </pre> */ /* * Specify where to start in case the specified consumer group is a brand new one. */ // consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET); /* * Subscribe one more more topics to consume. */ // consumer.subscribe("TopicRead3", "*"); // consumer.subscribe("TopicTest", "*"); consumer.subscribe("TopicTest_mis", "*"); /* * Register callback to execute on arrival of messages fetched from brokers. */ 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.RECONSUME_LATER; // return ConsumeConcurrentlyStatus.RECONSUME_LATER; } }); /* * Launch the consumer instance. */ consumer.start(); System.out.printf("Consumer Started.%n"); }