/**
  * 生成消费者
  *
  * @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);
   }
 }
Esempio n. 2
0
 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();
   }
 }
Esempio n. 3
0
  public void init() throws MQClientException {
    nameServer = PropertyFileUtil.get("rocketmq.namesrv.domain");

    if (StringUtils.isBlank(nameServer)) {

      logger.warn("【MQ init】property rocketmq.namesrv.domain not found");

      return;
    }

    if ("localTest".equals(nameServer)) {

      logger.warn("【MQ init】localTest");

      return;
    }

    if (StringUtils.isBlank(System.getProperty("rocketmq.namesrv.domain"))) {
      System.setProperty("rocketmq.namesrv.domain", nameServer);
    }
    topicType = getTopic();
    topic = RocketMqUtils.getTopic(topicType);

    if (StringUtils.isBlank(group)) {
      group = "S_" + topic.getTopic() + "_" + topic.getTags();
    }
    consumer = new DefaultMQPushConsumer(group);
    consumer.setNamesrvAddr(nameServer);
    consumer.setMessageModel(getMessageModel());
    consumer.setConsumeThreadMin(minConsumeThread);
    consumer.setConsumeThreadMax(maxConsumeThread);
    // 可以不设置 设置后可以起多个 消费端
    try {
      consumer.setInstanceName("DEFAULT_CONSUMER-" + InetAddress.getLocalHost().getHostName());
    } catch (UnknownHostException e) {
      logger.error("getHostName error", e);
    }
    // 设置订阅的topic 设置订阅过滤表达式
    if (StringUtils.isBlank(subExpression)) {
      subExpression = topic.getTags();
      consumer.subscribe(topic.getTopic(), subExpression);
    } else {
      consumer.subscribe(topic.getTopic(), subExpression);
    }
    try {
      consumer.registerMessageListener(this);
      consumer.start();
    } catch (MQClientException e) {
      logger.error(
          "consumer start error!topic={},subExpression={},group={}",
          topic.getTopic(),
          subExpression,
          group,
          e);
    }
    logger.info(
        "consumer start! topic={},subExpression={},group={}",
        topic.getTopic(),
        subExpression,
        group);
  }