Java Code Examples for org.apache.rocketmq.client.consumer.DefaultMQPushConsumer#subscribe()
The following examples show how to use
org.apache.rocketmq.client.consumer.DefaultMQPushConsumer#subscribe() .
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-4.3.0 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"); consumer.shutdown(); }
Example 2
Source File: Consumer.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("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 3
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 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("CID_JODIE_1"); consumer.setNamesrvAddr("127.0.0.1:9876"); consumer.subscribe("face-manager-1", "*"); 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 5
Source File: OnewayProducer.java From java-tutorial with MIT License | 6 votes |
public static void main(String[] args) throws Exception { DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("ONEWAY_CONSUMER"); consumer.setNamesrvAddr(SyncProducer.NAMESRVADDR); consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET); //设置consumer所订阅的Topic和Tag,*代表全部的Tag consumer.subscribe("Oneway_Topic_Test", "*"); consumer.registerMessageListener((MessageListenerConcurrently) (list, consumeOrderlyContext) -> { MessageExt messge = list.get(0); try { String topic = messge.getTopic(); String tags = messge.getTags(); String msgBody = new String(messge.getBody(), "utf-8"); System.out.println("收到消息:topic:" + topic + ",tags:" + tags + ",msg:" + messge); System.out.println("MsgBody:" + msgBody); System.out.println("------------------------------------------------------------------------------"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return ConsumeConcurrentlyStatus.CONSUME_SUCCESS; }); consumer.start(); System.out.println("consumer start"); }
Example 6
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("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 7
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("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 8
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 9
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 10
Source File: RockMqTestCase.java From jstarcraft-core with Apache License 2.0 | 5 votes |
@Test public void testTopic() throws Exception { int size = 5; DefaultMQPushConsumer[] consumers = new DefaultMQPushConsumer[size]; for (int index = 0; index < size; index++) { DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("topic"); consumer.setInstanceName("topicConsumer" + index); consumer.setNamesrvAddr("localhost:9876"); consumer.setMessageModel(MessageModel.BROADCASTING); consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET); consumer.setConsumeMessageBatchMaxSize(1000); consumer.subscribe("topic-message", "*"); consumer.registerMessageListener(listener); consumer.start(); consumers[index] = consumer; } DefaultMQProducer producer = new DefaultMQProducer("topic"); producer.setNamesrvAddr("localhost:9876"); producer.start(); for (int index = 0; index < 10; index++) { Message message = new Message("topic-message", "tag", ("RocketMQ 广播模式 " + index).getBytes(StringUtility.CHARSET)); producer.send(message); } semaphore.acquire(50); producer.shutdown(); for (int index = 0; index < size; index++) { consumers[index].shutdown(); } }
Example 11
Source File: RockMqTestCase.java From jstarcraft-core with Apache License 2.0 | 5 votes |
@Test public void testQueue() throws Exception { int size = 5; DefaultMQPushConsumer[] consumers = new DefaultMQPushConsumer[size]; for (int index = 0; index < size; index++) { DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("queue"); consumer.setInstanceName("queueConsumer" + index); consumer.setNamesrvAddr("localhost:9876"); consumer.setMessageModel(MessageModel.CLUSTERING); consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET); consumer.setConsumeMessageBatchMaxSize(1000); consumer.subscribe("queue-message", "*"); consumer.registerMessageListener(listener); consumer.start(); consumers[index] = consumer; } DefaultMQProducer producer = new DefaultMQProducer("queue"); producer.setNamesrvAddr("localhost:9876"); producer.start(); for (int index = 0; index < 10; index++) { Message message = new Message("queue-message", "tag", ("RocketMQ 集群模式 " + index).getBytes(StringUtility.CHARSET)); producer.send(message); } semaphore.acquire(10); producer.shutdown(); for (int index = 0; index < size; index++) { consumers[index].shutdown(); } }
Example 12
Source File: Consumer.java From rocketmq_trans_message 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(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"); }
Example 13
Source File: Consumer.java From rocketmq with Apache License 2.0 | 5 votes |
public static void main(String[] args) throws InterruptedException, MQClientException { CommandLine commandLine = buildCommandline(args); if (commandLine != null) { String group = commandLine.getOptionValue('g'); String topic = commandLine.getOptionValue('t'); String subscription = commandLine.getOptionValue('s'); final String returnFailedHalf = commandLine.getOptionValue('f'); DefaultMQPushConsumer consumer = new DefaultMQPushConsumer(group); consumer.setInstanceName(Long.toString(System.currentTimeMillis())); consumer.subscribe(topic, subscription); consumer.registerMessageListener(new MessageListenerConcurrently() { AtomicLong consumeTimes = new AtomicLong(0); @Override public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) { long currentTimes = this.consumeTimes.incrementAndGet(); System.out.printf("%-8d %s%n", currentTimes, msgs); if (Boolean.parseBoolean(returnFailedHalf)) { if ((currentTimes % 2) == 0) { return ConsumeConcurrentlyStatus.RECONSUME_LATER; } } return ConsumeConcurrentlyStatus.CONSUME_SUCCESS; } }); consumer.start(); System.out.printf("Consumer Started.%n"); } }
Example 14
Source File: RMQNormalConsumer.java From DDMQ with Apache License 2.0 | 5 votes |
public void create(boolean useTLS) { 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(listener); consumer.setUseTLS(useTLS); }
Example 15
Source File: RMQNormalConsumer.java From rocketmq with Apache License 2.0 | 5 votes |
public void create(boolean useTLS) { 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(listener); consumer.setUseTLS(useTLS); }
Example 16
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 17
Source File: Consumer.java From rocketmq-read with Apache License 2.0 | 5 votes |
public static void main(String[] args) throws InterruptedException, MQClientException { CommandLine commandLine = buildCommandline(args); if (commandLine != null) { String group = commandLine.getOptionValue('g'); String topic = commandLine.getOptionValue('t'); String subscription = commandLine.getOptionValue('s'); final String returnFailedHalf = commandLine.getOptionValue('f'); DefaultMQPushConsumer consumer = new DefaultMQPushConsumer(group); consumer.setInstanceName(Long.toString(System.currentTimeMillis())); consumer.subscribe(topic, subscription); consumer.registerMessageListener(new MessageListenerConcurrently() { AtomicLong consumeTimes = new AtomicLong(0); @Override public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) { long currentTimes = this.consumeTimes.incrementAndGet(); System.out.printf("%-8d %s%n", currentTimes, msgs); if (Boolean.parseBoolean(returnFailedHalf)) { if ((currentTimes % 2) == 0) { return ConsumeConcurrentlyStatus.RECONSUME_LATER; } } return ConsumeConcurrentlyStatus.CONSUME_SUCCESS; } }); consumer.start(); System.out.printf("Consumer Started.%n"); } }
Example 18
Source File: Consumer.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) { System.out.printf(Thread.currentThread().getName() + " Receive New Messages: " + msgs + "%n"); return ConsumeConcurrentlyStatus.CONSUME_SUCCESS; } }); /* * Launch the consumer instance. */ consumer.start(); System.out.printf("Consumer Started.%n"); }
Example 19
Source File: Consumer1.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); // int i=1/0; //模拟异常情况,观察消息重试情况 观察该字段的值的变化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 20
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; }