/**
  * 生成消费者
  *
  * @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);
   }
 }
示例#2
0
 public void destroy() {
   if (consumer != null) {
     consumer.shutdown();
     logger.info(
         "consumer shutdown! topic={0},subExpression={1},group={2}",
         topic.getTopic(), subExpression, group);
   }
 }
示例#3
0
  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.");
  }
示例#4
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();
   }
 }
示例#5
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);
  }
 /**
  * 订阅
  *
  * @param _topic 主题
  * @param _subExpression 条件
  */
 public void subscribe(String _topic, String _subExpression) throws Exception {
   if (active & enableRecv) {
     if (null == _subExpression || _subExpression.length() == 0) _subExpression = "*";
     mqConsumer.subscribe(_topic, _subExpression);
   }
 }
 /** 停止工作 */
 private void stop() {
   if (enableSend) mqConsumer.shutdown();
   if (enableRecv) mqProducer.shutdown();
 }