Java Code Examples for com.alibaba.rocketmq.client.QueryResult#getMessageList()

The following examples show how to use com.alibaba.rocketmq.client.QueryResult#getMessageList() . 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: TestProducer.java    From reading-and-annotate-rocketmq-3.4.6 with GNU General Public License v3.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",// topic
                    "TagA",// tag
                    "key113",// key
                    ("Hello MetaQ").getBytes());// body
                SendResult sendResult = producer.send(msg);
                System.out.println(sendResult);

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

        }
        catch (Exception e) {
            e.printStackTrace();
        }
    producer.shutdown();
}
 
Example 2
Source File: QueryMsgByKeySubCommand.java    From reading-and-annotate-rocketmq-3.4.6 with GNU General Public License v3.0 5 votes vote down vote up
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 3
Source File: MQAdminImpl.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
public MessageExt queryMessageByUniqKey(String topic, String uniqKey)  throws InterruptedException, MQClientException  {

        QueryResult qr = this.queryMessage(topic, uniqKey, 32,
                MessageClientIDSetter.getNearlyTimeFromID(uniqKey).getTime() - 1000, Long.MAX_VALUE, true);
        if (qr != null && qr.getMessageList() != null && qr.getMessageList().size() > 0) {
            return qr.getMessageList().get(0);
        }
        else {
            return null;
        }
    }
 
Example 4
Source File: TestProducer.java    From rocketmq with Apache License 2.0 5 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",// topic
                            "TagA",// tag
                            "key113",// key
                            ("Hello MetaQ").getBytes(RemotingHelper.DEFAULT_CHARSET));// body
                    SendResult sendResult = producer.send(msg);
                    System.out.println(sendResult);

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

            } catch (Exception e) {
                e.printStackTrace();
            }

        producer.shutdown();
    }
 
Example 5
Source File: QueryMsgByKeySubCommand.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
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 6
Source File: QueryMsgByKeySubCommand.java    From RocketMQ-Master-analyze with Apache License 2.0 5 votes vote down vote up
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 7
Source File: MessageService.java    From rocketmq with Apache License 2.0 4 votes vote down vote up
@CmdTrace(cmdClazz = QueryMsgByKeySubCommand.class)
public Table queryMsgByKey(String topicName, String msgKey, String fallbackHours) throws Throwable {
    Throwable t = null;
    DefaultMQAdminExt defaultMQAdminExt = getDefaultMQAdminExt();
    try {
        defaultMQAdminExt.start();
        long h = 0;
        if (StringUtils.isNotBlank(fallbackHours)) {
            h = Long.parseLong(fallbackHours);
        }
        long end = System.currentTimeMillis() - (h * 60 * 60 * 1000);
        long begin = end - (6 * 60 * 60 * 1000);
        QueryResult queryResult = defaultMQAdminExt.queryMessage(topicName, msgKey, 32, begin, end);

        // System.out.printf("%-50s %-4s  %s\n",//
        // "#Message ID",//
        // "#QID",//
        // "#Offset");
        String[] thead = new String[] { "#Message ID", "#QID", "#Offset" };
        int row = queryResult.getMessageList().size();
        Table table = new Table(thead, row);

        for (MessageExt msg : queryResult.getMessageList()) {
            String[] data =
                    new String[] { msg.getMsgId(), String.valueOf(msg.getQueueId()),
                                  String.valueOf(msg.getQueueOffset()) };
            table.insertTR(data);
            // System.out.printf("%-50s %-4d %d\n", msg.getMsgId(),
            // msg.getQueueId(), msg.getQueueOffset());
        }
        return table;
    }
    catch (Throwable e) {
        logger.error(e.getMessage(), e);
        t = e;
    }
    finally {
        shutdownDefaultMQAdminExt(defaultMQAdminExt);
    }
    throw t;
}
 
Example 8
Source File: TestProducer.java    From RocketMQ-Master-analyze with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws MQClientException, InterruptedException {
    /**
     * 一个应用创建一个Producer,由应用来维护此对象,可以设置为全局对象或者单例<br>
     * 注意:ProducerGroupName需要由应用来保证唯一<br>
     * ProducerGroup这个概念发送普通的消息时,作用不大,但是发送分布式事务消息时,比较关键,
     * 因为服务器会回查这个Group下的任意一个Producer
     */
    DefaultMQProducer producer = new DefaultMQProducer("ProducerGroupName");
    /**
     * Producer对象在使用之前必须要调用start初始化,初始化一次即可<br>
     * 注意:切记不可以在每次发送消息时,都调用start方法
     */
    producer.start();

    /**
     * 下面这段代码表明一个Producer对象可以发送多个topic,多个tag的消息。
     * 注意:send方法是同步调用,只要不抛异常就标识成功。但是发送成功也可会有多种状态,<br>
     * 例如消息写入Master成功,但是Slave不成功,这种情况消息属于成功,但是对于个别应用如果对消息可靠性要求极高,<br>
     * 需要对这种情况做处理。另外,消息可能会存在发送失败的情况,失败重试由应用来处理。
     */
    for (int i = 0; i < 1; i++)
        try {
            {
                Message msg = new Message("TopicTest1", // topic
                    "TagA", // tag
                    "key113", // key
                    ("Hello MetaQ").getBytes());// body
                SendResult sendResult = producer.send(msg);
                System.out.println(sendResult);

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

        }
        catch (Exception e) {
            e.printStackTrace();
        }

    /**
     * 应用退出时,要调用shutdown来清理资源,关闭网络连接,从MetaQ服务器上注销自己
     * 注意:我们建议应用在JBOSS、Tomcat等容器的退出钩子里调用shutdown方法
     */
    producer.shutdown();
}