org.apache.rocketmq.common.message.MessageExt Java Examples

The following examples show how to use org.apache.rocketmq.common.message.MessageExt. 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: Consumer.java    From rocketmq_trans_message with Apache License 2.0 6 votes vote down vote up
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 #2
Source File: ClientRemotingProcessor.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 6 votes vote down vote up
private RemotingCommand consumeMessageDirectly(ChannelHandlerContext ctx, RemotingCommand request) throws RemotingCommandException {
    final RemotingCommand response = RemotingCommand.createResponseCommand(null);
    final ConsumeMessageDirectlyResultRequestHeader requestHeader =
        (ConsumeMessageDirectlyResultRequestHeader) request
            .decodeCommandCustomHeader(ConsumeMessageDirectlyResultRequestHeader.class);

    final MessageExt msg = MessageDecoder.decode(ByteBuffer.wrap(request.getBody()));

    ConsumeMessageDirectlyResult result =
        this.mqClientFactory.consumeMessageDirectly(msg, requestHeader.getConsumerGroup(), requestHeader.getBrokerName());

    if (null != result) {
        response.setCode(ResponseCode.SUCCESS);
        response.setBody(result.encode());
    } else {
        response.setCode(ResponseCode.SYSTEM_ERROR);
        response.setRemark(String.format("The Consumer Group <%s> not exist in this consumer", requestHeader.getConsumerGroup()));
    }

    return response;
}
 
Example #3
Source File: MQClientAPIImpl.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
public MessageExt viewMessage(final String addr, final long phyoffset, final long timeoutMillis)
    throws RemotingException, MQBrokerException, InterruptedException {
    ViewMessageRequestHeader requestHeader = new ViewMessageRequestHeader();
    requestHeader.setOffset(phyoffset);
    RemotingCommand request = RemotingCommand.createRequestCommand(RequestCode.VIEW_MESSAGE_BY_ID, requestHeader);

    RemotingCommand response = this.remotingClient.invokeSync(MixAll.brokerVIPChannel(this.clientConfig.isVipChannelEnabled(), addr),
        request, timeoutMillis);
    assert response != null;
    switch (response.getCode()) {
        case ResponseCode.SUCCESS: {
            ByteBuffer byteBuffer = ByteBuffer.wrap(response.getBody());
            MessageExt messageExt = MessageDecoder.clientDecode(byteBuffer, true);
            //If namespace not null , reset Topic without namespace.
            if (StringUtils.isNotEmpty(this.clientConfig.getNamespace())) {
                messageExt.setTopic(NamespaceUtil.withoutNamespace(messageExt.getTopic(), this.clientConfig.getNamespace()));
            }
            return messageExt;
        }
        default:
            break;
    }

    throw new MQBrokerException(response.getCode(), response.getRemark());
}
 
Example #4
Source File: MQClientAPIImpl.java    From DDMQ with Apache License 2.0 6 votes vote down vote up
public MessageExt viewMessage(final String addr, final long phyoffset, final long timeoutMillis)
    throws RemotingException, MQBrokerException, InterruptedException {
    ViewMessageRequestHeader requestHeader = new ViewMessageRequestHeader();
    requestHeader.setOffset(phyoffset);
    RemotingCommand request = RemotingCommand.createRequestCommand(RequestCode.VIEW_MESSAGE_BY_ID, requestHeader);

    RemotingCommand response = this.remotingClient.invokeSync(MixAll.brokerVIPChannel(this.clientConfig.isVipChannelEnabled(), addr),
        request, timeoutMillis);
    assert response != null;
    switch (response.getCode()) {
        case ResponseCode.SUCCESS: {
            ByteBuffer byteBuffer = ByteBuffer.wrap(response.getBody());
            MessageExt messageExt = MessageDecoder.clientDecode(byteBuffer, true);
            return messageExt;
        }
        default:
            break;
    }

    throw new MQBrokerException(response.getCode(), response.getRemark());
}
 
Example #5
Source File: DefaultMQPushConsumerTest.java    From rocketmq_trans_message with Apache License 2.0 6 votes vote down vote up
@Test
public void testPullMessage_Success() throws InterruptedException, RemotingException, MQBrokerException {
    final CountDownLatch countDownLatch = new CountDownLatch(1);
    final MessageExt[] messageExts = new MessageExt[1];
    pushConsumer.getDefaultMQPushConsumerImpl().setConsumeMessageService(new ConsumeMessageConcurrentlyService(pushConsumer.getDefaultMQPushConsumerImpl(), new MessageListenerConcurrently() {
        @Override public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs,
            ConsumeConcurrentlyContext context) {
            messageExts[0] = msgs.get(0);
            countDownLatch.countDown();
            return null;
        }
    }));

    PullMessageService pullMessageService = mQClientFactory.getPullMessageService();
    pullMessageService.executePullRequestImmediately(createPullRequest());
    countDownLatch.await();
    assertThat(messageExts[0].getTopic()).isEqualTo(topic);
    assertThat(messageExts[0].getBody()).isEqualTo(new byte[] {'a'});
}
 
Example #6
Source File: SqlFilterConsumer.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {

        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("please_rename_unique_group_name");

        // Don't forget to set enablePropertyFilter=true in broker
        consumer.subscribe("SqlFilterTest",
            MessageSelector.bySql("(TAGS is not null and TAGS in ('TagA', 'TagB'))" +
                "and (a is not null and a between 0 and 3)"));

        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 #7
Source File: AbstractMQConsumer.java    From rocketmq-spring-boot-starter with Apache License 2.0 6 votes vote down vote up
protected Map<String, Object> parseExtParam(MessageExt message) {
    Map<String, Object> extMap = new HashMap<>();

    // parse message property
    extMap.put(MessageExtConst.PROPERTY_TOPIC, message.getTopic());
    extMap.putAll(message.getProperties());

    // parse messageExt property
    extMap.put(MessageExtConst.PROPERTY_EXT_BORN_HOST, message.getBornHost());
    extMap.put(MessageExtConst.PROPERTY_EXT_BORN_TIMESTAMP, message.getBornTimestamp());
    extMap.put(MessageExtConst.PROPERTY_EXT_COMMIT_LOG_OFFSET, message.getCommitLogOffset());
    extMap.put(MessageExtConst.PROPERTY_EXT_MSG_ID, message.getMsgId());
    extMap.put(MessageExtConst.PROPERTY_EXT_PREPARED_TRANSACTION_OFFSET, message.getPreparedTransactionOffset());
    extMap.put(MessageExtConst.PROPERTY_EXT_QUEUE_ID, message.getQueueId());
    extMap.put(MessageExtConst.PROPERTY_EXT_QUEUE_OFFSET, message.getQueueOffset());
    extMap.put(MessageExtConst.PROPERTY_EXT_RECONSUME_TIMES, message.getReconsumeTimes());
    extMap.put(MessageExtConst.PROPERTY_EXT_STORE_HOST, message.getStoreHost());
    extMap.put(MessageExtConst.PROPERTY_EXT_STORE_SIZE, message.getStoreSize());
    extMap.put(MessageExtConst.PROPERTY_EXT_STORE_TIMESTAMP, message.getStoreTimestamp());
    extMap.put(MessageExtConst.PROPERTY_EXT_SYS_FLAG, message.getSysFlag());
    extMap.put(MessageExtConst.PROPERTY_EXT_BODY_CRC, message.getBodyCRC());

    return extMap;
}
 
Example #8
Source File: PushConsumerImplTest.java    From DDMQ with Apache License 2.0 6 votes vote down vote up
@Test
public void testConsumeMessage() {
    final byte[] testBody = new byte[] {'a', 'b'};

    MessageExt consumedMsg = new MessageExt();
    consumedMsg.setMsgId("NewMsgId");
    consumedMsg.setBody(testBody);
    consumedMsg.putUserProperty(NonStandardKeys.MESSAGE_DESTINATION, "TOPIC");
    consumedMsg.setTopic("HELLO_QUEUE");
    consumer.attachQueue("HELLO_QUEUE", new MessageListener() {
        @Override
        public void onMessage(final Message message, final ReceivedMessageContext context) {
            assertThat(message.headers().getString(MessageHeader.MESSAGE_ID)).isEqualTo("NewMsgId");
            assertThat(((BytesMessage) message).getBody()).isEqualTo(testBody);
            context.ack();
        }
    });
    ((MessageListenerConcurrently) rocketmqPushConsumer
        .getMessageListener()).consumeMessage(Collections.singletonList(consumedMsg), null);
}
 
Example #9
Source File: DeFiBusClientRemotingProcessor.java    From DeFiBus with Apache License 2.0 6 votes vote down vote up
private void processResponse(MessageExt msg) {
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("receive reply message :{}", msg);
    }
    final String uniqueId = msg.getUserProperty(DeFiBusConstant.PROPERTY_RR_REQUEST_ID);

    RRResponseFuture rrResponseFuture = ResponseTable.getRrResponseFurtureConcurrentHashMap().get(uniqueId);
    if (rrResponseFuture != null && !rrResponseFuture.release()) {
        if (rrResponseFuture.getRrCallback() != null) {
            rrResponseFuture.getRrCallback().onSuccess(msg);
            ResponseTable.getRrResponseFurtureConcurrentHashMap().remove(uniqueId);
        } else {
            rrResponseFuture.putResponse(msg);
        }
    } else {
        LOGGER.warn("receive reply message {} , but requester has gone away", msg.toString());
    }
}
 
Example #10
Source File: QueryMsgByIdExceptionIT.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 6 votes vote down vote up
@Test
public void testQueryMsgByErrorMsgId() {
    producer.clearMsg();
    int msgSize = 20;
    String errorMsgId = "errorMsgId";
    producer.send(msgSize);
    Assert.assertEquals("Not all are sent", msgSize, producer.getAllUndupMsgBody().size());

    MessageExt queryMsg = null;
    try {
        queryMsg = producer.getProducer().viewMessage(errorMsgId);
    } catch (Exception e) {
    }

    assertThat(queryMsg).isNull();
}
 
Example #11
Source File: TagFilterConsumer.java    From blog with MIT License 6 votes vote down vote up
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 #12
Source File: ProcessQueue.java    From rocketmq-4.3.0 with Apache License 2.0 6 votes vote down vote up
public long commit() {
    try {
        this.lockTreeMap.writeLock().lockInterruptibly();
        try {
            Long offset = this.consumingMsgOrderlyTreeMap.lastKey();
            msgCount.addAndGet(0 - this.consumingMsgOrderlyTreeMap.size());
            for (MessageExt msg : this.consumingMsgOrderlyTreeMap.values()) {
                msgSize.addAndGet(0 - msg.getBody().length);
            }
            this.consumingMsgOrderlyTreeMap.clear();
            if (offset != null) {
                return offset + 1;
            }
        } finally {
            this.lockTreeMap.writeLock().unlock();
        }
    } catch (InterruptedException e) {
        log.error("commit exception", e);
    }

    return -1;
}
 
Example #13
Source File: TestProducer.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws MQClientException, InterruptedException {
    DefaultMQProducer producer = new DefaultMQProducer("ProducerGroupName");
    producer.start();

    for (int i = 0; i < 1; i++)
        try {
            {
                Message msg = new Message("TopicTest1",
                    "TagA",
                    "key113",
                    "Hello world".getBytes(RemotingHelper.DEFAULT_CHARSET));
                SendResult sendResult = producer.send(msg);
                System.out.printf("%s%n", sendResult);

                QueryResult queryMessage =
                    producer.queryMessage("TopicTest1", "key113", 10, 0, System.currentTimeMillis());
                for (MessageExt m : queryMessage.getMessageList()) {
                    System.out.printf("%s%n", m);
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    producer.shutdown();
}
 
Example #14
Source File: PushConsumer.java    From blog with BSD 2-Clause "Simplified" License 6 votes vote down vote up
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 #15
Source File: ConsumeMessageOrderlyService.java    From rocketmq-read with Apache License 2.0 5 votes vote down vote up
/**
 * 提交消费请求
 * @param msgs 消息
 * @param processQueue 处理队列
 * @param messageQueue 消息队列
 * @param dispathToConsume 是否分发消费
 */
@Override
public void submitConsumeRequest(
    final List<MessageExt> msgs,
    final ProcessQueue processQueue,
    final MessageQueue messageQueue,
    final boolean dispathToConsume) {
    if (dispathToConsume) {
        ConsumeRequest consumeRequest = new ConsumeRequest(processQueue, messageQueue);
        this.consumeExecutor.submit(consumeRequest);
    }
}
 
Example #16
Source File: ConsumeMessageConcurrentlyService.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
@Override
public void submitConsumeRequestLater(
        final List<MessageExt> msgs,
        final ProcessQueue processQueue,
        final MessageQueue messageQueue,
        final boolean dispathToConsume,
        final long timeMillis) {
    this.scheduledExecutorService.schedule(new Runnable() {
        @Override
        public void run() {
            ConsumeMessageConcurrentlyService.this.submitConsumeRequest(msgs, processQueue, messageQueue, true);
        }
    }, timeMillis, TimeUnit.MILLISECONDS);
}
 
Example #17
Source File: EndTransactionProcessor.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
private MessageExtBrokerInner endMessageTransaction(MessageExt msgExt) {
    MessageExtBrokerInner msgInner = new MessageExtBrokerInner();
    msgInner.setBody(msgExt.getBody());
    msgInner.setFlag(msgExt.getFlag());
    MessageAccessor.setProperties(msgInner, msgExt.getProperties());

    TopicFilterType topicFilterType =
        (msgInner.getSysFlag() & MessageSysFlag.MULTI_TAGS_FLAG) == MessageSysFlag.MULTI_TAGS_FLAG ? TopicFilterType.MULTI_TAG
            : TopicFilterType.SINGLE_TAG;
    long tagsCodeValue = MessageExtBrokerInner.tagsString2tagsCode(topicFilterType, msgInner.getTags());
    msgInner.setTagsCode(tagsCodeValue);
    msgInner.setPropertiesString(MessageDecoder.messageProperties2String(msgExt.getProperties()));

    msgInner.setSysFlag(msgExt.getSysFlag());
    msgInner.setBornTimestamp(msgExt.getBornTimestamp());
    msgInner.setBornHost(msgExt.getBornHost());
    msgInner.setStoreHost(msgExt.getStoreHost());
    msgInner.setReconsumeTimes(msgExt.getReconsumeTimes());

    msgInner.setWaitStoreMsgOK(false);
    MessageAccessor.clearProperty(msgInner, MessageConst.PROPERTY_DELAY_TIME_LEVEL);

    msgInner.setTopic(msgExt.getTopic());
    msgInner.setQueueId(msgExt.getQueueId());

    return msgInner;
}
 
Example #18
Source File: QueryMsgByKeySubCommand.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
private void queryByKey(final DefaultMQAdminExt admin, final String topic, final String key)
    throws MQClientException, InterruptedException {
    admin.start();

    QueryResult queryResult = admin.queryMessage(topic, key, 64, 0, Long.MAX_VALUE);
    System.out.printf("%-50s %4s %40s%n",
        "#Message ID",
        "#QID",
        "#Offset");
    for (MessageExt msg : queryResult.getMessageList()) {
        System.out.printf("%-50s %4d %40d%n", msg.getMsgId(), msg.getQueueId(), msg.getQueueOffset());
    }
}
 
Example #19
Source File: ClientRemotingProcessor.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
public RemotingCommand checkTransactionState(ChannelHandlerContext ctx,
    RemotingCommand request) throws RemotingCommandException {
    final CheckTransactionStateRequestHeader requestHeader =
        (CheckTransactionStateRequestHeader) request.decodeCommandCustomHeader(CheckTransactionStateRequestHeader.class);
    final ByteBuffer byteBuffer = ByteBuffer.wrap(request.getBody());
    final MessageExt messageExt = MessageDecoder.decode(byteBuffer);
    if (messageExt != null) {
        if (StringUtils.isNotEmpty(this.mqClientFactory.getClientConfig().getNamespace())) {
            messageExt.setTopic(NamespaceUtil
                .withoutNamespace(messageExt.getTopic(), this.mqClientFactory.getClientConfig().getNamespace()));
        }
        String transactionId = messageExt.getProperty(MessageConst.PROPERTY_UNIQ_CLIENT_MESSAGE_ID_KEYIDX);
        if (null != transactionId && !"".equals(transactionId)) {
            messageExt.setTransactionId(transactionId);
        }
        final String group = messageExt.getProperty(MessageConst.PROPERTY_PRODUCER_GROUP);
        if (group != null) {
            MQProducerInner producer = this.mqClientFactory.selectProducer(group);
            if (producer != null) {
                final String addr = RemotingHelper.parseChannelRemoteAddr(ctx.channel());
                producer.checkTransactionState(addr, messageExt, requestHeader);
            } else {
                log.debug("checkTransactionState, pick producer by group[{}] failed", group);
            }
        } else {
            log.warn("checkTransactionState, pick producer group failed");
        }
    } else {
        log.warn("checkTransactionState, decode message failed");
    }

    return null;
}
 
Example #20
Source File: DefaultMessageStore.java    From rocketmq-read with Apache License 2.0 5 votes vote down vote up
/**
 * 根据offset和大小查询消息
 * @param commitLogOffset ;
 * @param size ;
 * @return ;
 */
public MessageExt lookMessageByOffset(long commitLogOffset, int size) {
    SelectMappedBufferResult sbr = this.commitLog.getMessage(commitLogOffset, size);
    if (null != sbr) {
        try {
            return MessageDecoder.decode(sbr.getByteBuffer(), true, false);
        } finally {
            sbr.release();
        }
    }

    return null;
}
 
Example #21
Source File: DefaultMQPullConsumer.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
@Override
public MessageExt viewMessage(String topic,
    String uniqKey) throws RemotingException, MQBrokerException, InterruptedException, MQClientException {
    try {
        MessageDecoder.decodeMessageId(uniqKey);
        return this.viewMessage(uniqKey);
    } catch (Exception e) {
        // Ignore
    }
    return this.defaultMQPullConsumerImpl.queryMessageByUniqKey(topic, uniqKey);
}
 
Example #22
Source File: DefaultMQPushConsumer.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 5 votes vote down vote up
@Override
public MessageExt viewMessage(String topic, String msgId) throws RemotingException, MQBrokerException, InterruptedException, MQClientException {
    try {
        MessageDecoder.decodeMessageId(msgId);
        return this.viewMessage(msgId);
    } catch (Exception e) {
        // Ignore
    }
    return this.defaultMQPushConsumerImpl.queryMessageByUniqKey(topic, msgId);
}
 
Example #23
Source File: AclClient.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
private static void printBody(List<MessageExt> msg) {
    if (msg == null || msg.size() == 0)
        return;
    for (MessageExt m : msg) {
        if (m != null) {
            System.out.printf("msgId : %s  body : %s  \n\r", m.getMsgId(), new String(m.getBody()));
        }
    }
}
 
Example #24
Source File: DefaultMQAdminExtImpl.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 5 votes vote down vote up
@Override
public ConsumeMessageDirectlyResult consumeMessageDirectly(String consumerGroup, String clientId, String msgId)
    throws RemotingException, MQClientException, InterruptedException, MQBrokerException {
    MessageExt msg = this.viewMessage(msgId);

    return this.mqClientInstance.getMQClientAPIImpl().consumeMessageDirectly(RemotingUtil.socketAddress2String(msg.getStoreHost()),
        consumerGroup, clientId, msgId, timeoutMillis * 3);
}
 
Example #25
Source File: SqlFilterConsumer.java    From blog with MIT License 5 votes vote down vote up
public static void main(String[] args) throws MQClientException {
  /** TODO: 创建消息消费者 */
  DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("Sql_Filter_Consumer");
  /** TODO: 设置 nameserver 地址 */
  consumer.setNamesrvAddr("127.0.0.1:9876");

  /**
   * TODO: 订阅主题, MessageSelect.bySql() 指定消息中的属性过滤,MessageSelector.byTag() 指定消息中的 TAG
   *
   * <p>SQL 过滤需要在 broker 配置文件中设置 enablePropertyFilter = true
   */
  consumer.subscribe(
      "SqlFilterTest", MessageSelector.bySql("(TAGS in ('TagA')) and (index > 3)"));
  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 #26
Source File: CommitLog.java    From rocketmq-4.3.0 with Apache License 2.0 5 votes vote down vote up
public void handleHA(AppendMessageResult result, PutMessageResult putMessageResult, MessageExt messageExt) {
//        如果master同步刷新
        if (BrokerRole.SYNC_MASTER == this.defaultMessageStore.getMessageStoreConfig().getBrokerRole()) {
            HAService service = this.defaultMessageStore.getHaService();
            if (messageExt.isWaitStoreMsgOK()) {
                // Determine whether to wait
                if (service.isSlaveOK(result.getWroteOffset() + result.getWroteBytes())) {
                    GroupCommitRequest request = new GroupCommitRequest(result.getWroteOffset() + result.getWroteBytes());
                    service.putRequest(request);
                    service.getWaitNotifyObject().wakeupAll();
//                    countDownLatch.await 同步等待刷新,除非等待超时
                    boolean flushOK =
                        request.waitForFlush(this.defaultMessageStore.getMessageStoreConfig().getSyncFlushTimeout());
                    if (!flushOK) {
                        log.error("do sync transfer other node, wait return, but failed, topic: " + messageExt.getTopic() + " tags: "
                            + messageExt.getTags() + " client address: " + messageExt.getBornHostNameString());
                        putMessageResult.setPutMessageStatus(PutMessageStatus.FLUSH_SLAVE_TIMEOUT);
                    }
                }
                // Slave problem
                else {
                    // Tell the producer, slave not available
                    putMessageResult.setPutMessageStatus(PutMessageStatus.SLAVE_NOT_AVAILABLE);
                }
            }
        }

    }
 
Example #27
Source File: Consumer.java    From rocketmq-4.3.0 with Apache License 2.0 5 votes vote down vote up
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("%s Receive New Messages: %s %n", Thread.currentThread().getName(), msgs);
            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 #28
Source File: DefaultMQPushConsumer.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
/**
 * This method will be removed in a certain version after April 5, 2020, so please do not use this method.
 */
@Deprecated
@Override
public MessageExt viewMessage(String topic,
    String msgId) throws RemotingException, MQBrokerException, InterruptedException, MQClientException {
    try {
        MessageDecoder.decodeMessageId(msgId);
        return this.viewMessage(msgId);
    } catch (Exception e) {
        // Ignore
    }
    return this.defaultMQPushConsumerImpl.queryMessageByUniqKey(withNamespace(topic), msgId);
}
 
Example #29
Source File: MessageFilterImpl.java    From rocketmq-read with Apache License 2.0 5 votes vote down vote up
@Override
public boolean match(MessageExt msg, FilterContext context) {
    String property = msg.getProperty("SequenceId");
    if (property != null) {
        int id = Integer.parseInt(property);
        if (((id % 10) == 0) &&
            (id > 100)) {
            return true;
        }
    }

    return false;
}
 
Example #30
Source File: DefaultMQPullConsumerTest.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
@Test
public void testPullMessage_NotFound() throws Exception {
    doAnswer(new Answer() {
        @Override
        public Object answer(InvocationOnMock mock) throws Throwable {
            PullMessageRequestHeader requestHeader = mock.getArgument(1);
            return createPullResult(requestHeader, PullStatus.NO_NEW_MSG, new ArrayList<MessageExt>());
        }
    }).when(mQClientAPIImpl).pullMessage(anyString(), any(PullMessageRequestHeader.class), anyLong(), any(CommunicationMode.class), nullable(PullCallback.class));

    MessageQueue messageQueue = new MessageQueue(topic, brokerName, 0);
    PullResult pullResult = pullConsumer.pull(messageQueue, "*", 1024, 3);
    assertThat(pullResult.getPullStatus()).isEqualTo(PullStatus.NO_NEW_MSG);
}