/** * 生成消费者 * * @throws Exception */ private void makeConsumer() throws Exception { if (null == mqConsumer) { mqConsumer = new DefaultMQPushConsumer(mqGroupName); mqConsumer.setNamesrvAddr(mqNameServers); mqConsumer.setInstanceName(mqInstanceName); mqConsumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_LAST_OFFSET); mqConsumer.registerMessageListener(mqListener); mqConsumer.start(); logger.info( "Consumer-> GroupName:{} InstanceName:{} <{}> is ok.", mqGroupName, mqInstanceName, mqNameServers); } }
public static void main(String[] args) { DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("consumer"); consumer.setNamesrvAddr("10.0.1.187:9876:9876"); try { // 订阅PushTopic下Tag为push的消息 consumer.subscribe("test", "test1"); // 程序第一次启动从消息队列头取数据 consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET); consumer.registerMessageListener( new MessageListenerConcurrently() { public ConsumeConcurrentlyStatus consumeMessage( List<MessageExt> list, ConsumeConcurrentlyContext Context) { Message msg = list.get(0); System.out.println(msg.toString()); return ConsumeConcurrentlyStatus.CONSUME_SUCCESS; } }); consumer.start(); } catch (Exception e) { e.printStackTrace(); } }
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.println(Thread.currentThread().getName() + " Receive New Messages: " + msgs); return ConsumeConcurrentlyStatus.CONSUME_SUCCESS; } }); consumer.start(); System.out.println("Consumer Started."); }