RocketMq 之 java rocket client

 

maven依赖:

<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-client</artifactId>
<version>4.4.0</version>
</dependency>

 

先用命令建一个topic:

sh mqadmin updateTopic -b 192.168.71.170:10911 -n 192.168.71.170:9876 -t TopicTest

查看:

sh mqadmin topicList -n 192.168.71.170:9876

输出:

[root@localhost bin]# sh mqadmin topicList -n 192.168.71.170:9876
RocketMQLog:WARN No appenders could be found for logger (io.netty.util.internal.PlatformDependent0).
RocketMQLog:WARN Please initialize the logger system properly.
RMQ_SYS_TRANS_HALF_TOPIC
DefaultCluster_REPLY_TOPIC
BenchmarkTest
OFFSET_MOVED_EVENT
TopicTest
TBW102
localhost.localdomain
SELF_TEST_TOPIC
DefaultCluster

 

Producer.java:

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;

public class Producer {
    public static void main(String[] args) throws Exception {
        DefaultMQProducer producer = new DefaultMQProducer("ProducerGroup");
        producer.setNamesrvAddr("192.168.71.170:9876");
        //producer.set
        producer.start();
        for(int i= 0; i <100; i++){
            Message message = new Message("TopicTest", "TagA", ("hello RocketMQ" + i).getBytes("UTF-8"));
            SendResult send = producer.send(message);
            System.out.println(send);
        }
        producer.shutdown();
    }
}

 主要三个参数要指定,ProducerGroup(组), namesrvaddr, 和topicname。

 

输出结果:

SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB161055B9A0000, offsetMsgId=C0A847AA00002A9F0000000000000000, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=6], queueOffset=0]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB161055BE50001, offsetMsgId=C0A847AA00002A9F00000000000000C8, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=7], queueOffset=0]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB161055C010002, offsetMsgId=C0A847AA00002A9F0000000000000190, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=0], queueOffset=0]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB161055C1E0003, offsetMsgId=C0A847AA00002A9F0000000000000258, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=1], queueOffset=0]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB161055C3F0004, offsetMsgId=C0A847AA00002A9F0000000000000320, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=2], queueOffset=0]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB161055C600005, offsetMsgId=C0A847AA00002A9F00000000000003E8, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=3], queueOffset=0]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB161055C7D0006, offsetMsgId=C0A847AA00002A9F00000000000004B0, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=4], queueOffset=0]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB161055C9A0007, offsetMsgId=C0A847AA00002A9F0000000000000578, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=5], queueOffset=0]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB161055CBA0008, offsetMsgId=C0A847AA00002A9F0000000000000640, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=6], queueOffset=1]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB161055CD70009, offsetMsgId=C0A847AA00002A9F0000000000000708, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=7], queueOffset=1]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB161055CFD000A, offsetMsgId=C0A847AA00002A9F00000000000007D0, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=0], queueOffset=1]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB161055D25000B, offsetMsgId=C0A847AA00002A9F0000000000000899, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=1], queueOffset=1]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB161055D41000C, offsetMsgId=C0A847AA00002A9F0000000000000962, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=2], queueOffset=1]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB161055D5C000D, offsetMsgId=C0A847AA00002A9F0000000000000A2B, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=3], queueOffset=1]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB161055D81000E, offsetMsgId=C0A847AA00002A9F0000000000000AF4, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=4], queueOffset=1]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB161055DA1000F, offsetMsgId=C0A847AA00002A9F0000000000000BBD, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=5], queueOffset=1]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB161055DC20010, offsetMsgId=C0A847AA00002A9F0000000000000C86, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=6], queueOffset=2]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB161055DDE0011, offsetMsgId=C0A847AA00002A9F0000000000000D4F, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=7], queueOffset=2]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB161055DFB0012, offsetMsgId=C0A847AA00002A9F0000000000000E18, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=0], queueOffset=2]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB161055E160013, offsetMsgId=C0A847AA00002A9F0000000000000EE1, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=1], queueOffset=2]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB161055E340014, offsetMsgId=C0A847AA00002A9F0000000000000FAA, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=2], queueOffset=2]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB161055E4F0015, offsetMsgId=C0A847AA00002A9F0000000000001073, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=3], queueOffset=2]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB161055E710016, offsetMsgId=C0A847AA00002A9F000000000000113C, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=4], queueOffset=2]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB161055E9F0017, offsetMsgId=C0A847AA00002A9F0000000000001205, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=5], queueOffset=2]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB161055EC30018, offsetMsgId=C0A847AA00002A9F00000000000012CE, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=6], queueOffset=3]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB161055EDF0019, offsetMsgId=C0A847AA00002A9F0000000000001397, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=7], queueOffset=3]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB161055EFB001A, offsetMsgId=C0A847AA00002A9F0000000000001460, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=0], queueOffset=3]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB161055F16001B, offsetMsgId=C0A847AA00002A9F0000000000001529, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=1], queueOffset=3]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB161055F31001C, offsetMsgId=C0A847AA00002A9F00000000000015F2, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=2], queueOffset=3]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB161055F4C001D, offsetMsgId=C0A847AA00002A9F00000000000016BB, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=3], queueOffset=3]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB161055F77001E, offsetMsgId=C0A847AA00002A9F0000000000001784, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=4], queueOffset=3]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB161055F9A001F, offsetMsgId=C0A847AA00002A9F000000000000184D, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=5], queueOffset=3]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB161055FB90020, offsetMsgId=C0A847AA00002A9F0000000000001916, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=6], queueOffset=4]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB161055FE60021, offsetMsgId=C0A847AA00002A9F00000000000019DF, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=7], queueOffset=4]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB1610560050022, offsetMsgId=C0A847AA00002A9F0000000000001AA8, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=0], queueOffset=4]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB1610560200023, offsetMsgId=C0A847AA00002A9F0000000000001B71, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=1], queueOffset=4]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB1610560400024, offsetMsgId=C0A847AA00002A9F0000000000001C3A, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=2], queueOffset=4]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB16105605C0025, offsetMsgId=C0A847AA00002A9F0000000000001D03, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=3], queueOffset=4]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB16105607A0026, offsetMsgId=C0A847AA00002A9F0000000000001DCC, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=4], queueOffset=4]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB1610560950027, offsetMsgId=C0A847AA00002A9F0000000000001E95, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=5], queueOffset=4]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB1610560B40028, offsetMsgId=C0A847AA00002A9F0000000000001F5E, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=6], queueOffset=5]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB1610560D10029, offsetMsgId=C0A847AA00002A9F0000000000002027, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=7], queueOffset=5]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB1610560ED002A, offsetMsgId=C0A847AA00002A9F00000000000020F0, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=0], queueOffset=5]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB16105610D002B, offsetMsgId=C0A847AA00002A9F00000000000021B9, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=1], queueOffset=5]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB161056126002C, offsetMsgId=C0A847AA00002A9F0000000000002282, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=2], queueOffset=5]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB161056141002D, offsetMsgId=C0A847AA00002A9F000000000000234B, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=3], queueOffset=5]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB16105615E002E, offsetMsgId=C0A847AA00002A9F0000000000002414, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=4], queueOffset=5]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB16105617E002F, offsetMsgId=C0A847AA00002A9F00000000000024DD, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=5], queueOffset=5]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB16105619C0030, offsetMsgId=C0A847AA00002A9F00000000000025A6, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=6], queueOffset=6]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB1610561BA0031, offsetMsgId=C0A847AA00002A9F000000000000266F, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=7], queueOffset=6]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB1610561DB0032, offsetMsgId=C0A847AA00002A9F0000000000002738, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=0], queueOffset=6]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB1610561FF0033, offsetMsgId=C0A847AA00002A9F0000000000002801, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=1], queueOffset=6]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB16105621B0034, offsetMsgId=C0A847AA00002A9F00000000000028CA, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=2], queueOffset=6]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB1610562350035, offsetMsgId=C0A847AA00002A9F0000000000002993, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=3], queueOffset=6]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB1610562580036, offsetMsgId=C0A847AA00002A9F0000000000002A5C, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=4], queueOffset=6]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB1610562740037, offsetMsgId=C0A847AA00002A9F0000000000002B25, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=5], queueOffset=6]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB1610562910038, offsetMsgId=C0A847AA00002A9F0000000000002BEE, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=6], queueOffset=7]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB1610562B50039, offsetMsgId=C0A847AA00002A9F0000000000002CB7, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=7], queueOffset=7]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB1610562E0003A, offsetMsgId=C0A847AA00002A9F0000000000002D80, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=0], queueOffset=7]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB161056307003B, offsetMsgId=C0A847AA00002A9F0000000000002E49, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=1], queueOffset=7]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB16105632C003C, offsetMsgId=C0A847AA00002A9F0000000000002F12, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=2], queueOffset=7]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB161056349003D, offsetMsgId=C0A847AA00002A9F0000000000002FDB, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=3], queueOffset=7]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB16105636C003E, offsetMsgId=C0A847AA00002A9F00000000000030A4, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=4], queueOffset=7]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB161056395003F, offsetMsgId=C0A847AA00002A9F000000000000316D, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=5], queueOffset=7]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB1610563B10040, offsetMsgId=C0A847AA00002A9F0000000000003236, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=6], queueOffset=8]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB1610563D10041, offsetMsgId=C0A847AA00002A9F00000000000032FF, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=7], queueOffset=8]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB1610564020042, offsetMsgId=C0A847AA00002A9F00000000000033C8, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=0], queueOffset=8]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB16105641F0043, offsetMsgId=C0A847AA00002A9F0000000000003491, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=1], queueOffset=8]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB16105643B0044, offsetMsgId=C0A847AA00002A9F000000000000355A, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=2], queueOffset=8]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB1610564580045, offsetMsgId=C0A847AA00002A9F0000000000003623, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=3], queueOffset=8]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB1610564730046, offsetMsgId=C0A847AA00002A9F00000000000036EC, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=4], queueOffset=8]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB16105648E0047, offsetMsgId=C0A847AA00002A9F00000000000037B5, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=5], queueOffset=8]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB1610564AC0048, offsetMsgId=C0A847AA00002A9F000000000000387E, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=6], queueOffset=9]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB1610564C90049, offsetMsgId=C0A847AA00002A9F0000000000003947, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=7], queueOffset=9]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB1610564E2004A, offsetMsgId=C0A847AA00002A9F0000000000003A10, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=0], queueOffset=9]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB1610564FF004B, offsetMsgId=C0A847AA00002A9F0000000000003AD9, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=1], queueOffset=9]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB16105651A004C, offsetMsgId=C0A847AA00002A9F0000000000003BA2, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=2], queueOffset=9]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB161056540004D, offsetMsgId=C0A847AA00002A9F0000000000003C6B, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=3], queueOffset=9]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB161056581004E, offsetMsgId=C0A847AA00002A9F0000000000003D34, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=4], queueOffset=9]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB16105659C004F, offsetMsgId=C0A847AA00002A9F0000000000003DFD, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=5], queueOffset=9]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB1610565B80050, offsetMsgId=C0A847AA00002A9F0000000000003EC6, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=6], queueOffset=10]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB1610565F20051, offsetMsgId=C0A847AA00002A9F0000000000003F8F, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=7], queueOffset=10]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB16105660C0052, offsetMsgId=C0A847AA00002A9F0000000000004058, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=0], queueOffset=10]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB16105662B0053, offsetMsgId=C0A847AA00002A9F0000000000004121, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=1], queueOffset=10]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB1610566450054, offsetMsgId=C0A847AA00002A9F00000000000041EA, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=2], queueOffset=10]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB1610566610055, offsetMsgId=C0A847AA00002A9F00000000000042B3, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=3], queueOffset=10]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB1610566850056, offsetMsgId=C0A847AA00002A9F000000000000437C, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=4], queueOffset=10]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB1610566A30057, offsetMsgId=C0A847AA00002A9F0000000000004445, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=5], queueOffset=10]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB1610566BF0058, offsetMsgId=C0A847AA00002A9F000000000000450E, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=6], queueOffset=11]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB1610566D90059, offsetMsgId=C0A847AA00002A9F00000000000045D7, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=7], queueOffset=11]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB16105670C005A, offsetMsgId=C0A847AA00002A9F00000000000046A0, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=0], queueOffset=11]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB161056727005B, offsetMsgId=C0A847AA00002A9F0000000000004769, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=1], queueOffset=11]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB161056774005C, offsetMsgId=C0A847AA00002A9F0000000000004832, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=2], queueOffset=11]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB161056797005D, offsetMsgId=C0A847AA00002A9F00000000000048FB, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=3], queueOffset=11]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB1610567B3005E, offsetMsgId=C0A847AA00002A9F00000000000049C4, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=4], queueOffset=11]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB1610567D5005F, offsetMsgId=C0A847AA00002A9F0000000000004A8D, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=5], queueOffset=11]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB1610567F30060, offsetMsgId=C0A847AA00002A9F0000000000004B56, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=6], queueOffset=12]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB1610568110061, offsetMsgId=C0A847AA00002A9F0000000000004C1F, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=7], queueOffset=12]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB1610568330062, offsetMsgId=C0A847AA00002A9F0000000000004CE8, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=0], queueOffset=12]
SendResult [sendStatus=SEND_OK, msgId=02000133622C78308DB16105685F0063, offsetMsgId=C0A847AA00002A9F0000000000004DB1, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=1], queueOffset=12]

 

Consumer.java

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.consumer.ConsumeFromWhere;
import org.apache.rocketmq.common.message.MessageExt;

public class Consumer {
    public static void main(String[] args) throws MQClientException {
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("ConsumerGroup");
        consumer.setNamesrvAddr("192.168.71.170:9876");
        consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
        consumer.subscribe("TopicTest", "*");
        consumer.registerMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
                for (MessageExt ext : list) {
                    try {
                        System.out.println(new Date()+new String(ext.getBody(), "UTF-8"));
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                }
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });
        consumer.start();
        System.out.println("consumer start");
    }
}

 

输出:

consumer start
Wed Feb 19 20:46:54 CST 2020hello RocketMQ6
Wed Feb 19 20:46:54 CST 2020hello RocketMQ14
Wed Feb 19 20:46:54 CST 2020hello RocketMQ94
Wed Feb 19 20:46:54 CST 2020hello RocketMQ30
Wed Feb 19 20:46:54 CST 2020hello RocketMQ54
Wed Feb 19 20:46:54 CST 2020hello RocketMQ22
Wed Feb 19 20:46:54 CST 2020hello RocketMQ78
Wed Feb 19 20:46:54 CST 2020hello RocketMQ70
Wed Feb 19 20:46:54 CST 2020hello RocketMQ62
Wed Feb 19 20:46:54 CST 2020hello RocketMQ38
Wed Feb 19 20:46:54 CST 2020hello RocketMQ46
Wed Feb 19 20:46:54 CST 2020hello RocketMQ86
Wed Feb 19 20:46:54 CST 2020hello RocketMQ11
Wed Feb 19 20:46:54 CST 2020hello RocketMQ19
Wed Feb 19 20:46:54 CST 2020hello RocketMQ28
Wed Feb 19 20:46:54 CST 2020hello RocketMQ44
Wed Feb 19 20:46:54 CST 2020hello RocketMQ3
Wed Feb 19 20:46:54 CST 2020hello RocketMQ52
Wed Feb 19 20:46:54 CST 2020hello RocketMQ36
Wed Feb 19 20:46:54 CST 2020hello RocketMQ92
Wed Feb 19 20:46:54 CST 2020hello RocketMQ27
Wed Feb 19 20:46:54 CST 2020hello RocketMQ20
Wed Feb 19 20:46:54 CST 2020hello RocketMQ51
Wed Feb 19 20:46:54 CST 2020hello RocketMQ59
Wed Feb 19 20:46:54 CST 2020hello RocketMQ67
Wed Feb 19 20:46:54 CST 2020hello RocketMQ75
Wed Feb 19 20:46:54 CST 2020hello RocketMQ83
Wed Feb 19 20:46:54 CST 2020hello RocketMQ99
Wed Feb 19 20:46:54 CST 2020hello RocketMQ12
Wed Feb 19 20:46:54 CST 2020hello RocketMQ4
Wed Feb 19 20:46:54 CST 2020hello RocketMQ76
Wed Feb 19 20:46:54 CST 2020hello RocketMQ91
Wed Feb 19 20:46:54 CST 2020hello RocketMQ43
Wed Feb 19 20:46:54 CST 2020hello RocketMQ35
Wed Feb 19 20:46:54 CST 2020hello RocketMQ84
Wed Feb 19 20:46:54 CST 2020hello RocketMQ68
Wed Feb 19 20:46:54 CST 2020hello RocketMQ60
Wed Feb 19 20:46:54 CST 2020hello RocketMQ5
Wed Feb 19 20:46:54 CST 2020hello RocketMQ7
Wed Feb 19 20:46:54 CST 2020hello RocketMQ15
Wed Feb 19 20:46:54 CST 2020hello RocketMQ13
Wed Feb 19 20:46:54 CST 2020hello RocketMQ29
Wed Feb 19 20:46:54 CST 2020hello RocketMQ21
Wed Feb 19 20:46:54 CST 2020hello RocketMQ31
Wed Feb 19 20:46:54 CST 2020hello RocketMQ37
Wed Feb 19 20:46:54 CST 2020hello RocketMQ23
Wed Feb 19 20:46:54 CST 2020hello RocketMQ93
Wed Feb 19 20:46:54 CST 2020hello RocketMQ18
Wed Feb 19 20:46:54 CST 2020hello RocketMQ34
Wed Feb 19 20:46:54 CST 2020hello RocketMQ77
Wed Feb 19 20:46:54 CST 2020hello RocketMQ85
Wed Feb 19 20:46:54 CST 2020hello RocketMQ55
Wed Feb 19 20:46:54 CST 2020hello RocketMQ1
Wed Feb 19 20:46:54 CST 2020hello RocketMQ61
Wed Feb 19 20:46:54 CST 2020hello RocketMQ53
Wed Feb 19 20:46:54 CST 2020hello RocketMQ69
Wed Feb 19 20:46:54 CST 2020hello RocketMQ47
Wed Feb 19 20:46:54 CST 2020hello RocketMQ45
Wed Feb 19 20:46:54 CST 2020hello RocketMQ39
Wed Feb 19 20:46:54 CST 2020hello RocketMQ17
Wed Feb 19 20:46:54 CST 2020hello RocketMQ9
Wed Feb 19 20:46:54 CST 2020hello RocketMQ95
Wed Feb 19 20:46:54 CST 2020hello RocketMQ87
Wed Feb 19 20:46:54 CST 2020hello RocketMQ79
Wed Feb 19 20:46:54 CST 2020hello RocketMQ71
Wed Feb 19 20:46:54 CST 2020hello RocketMQ63
Wed Feb 19 20:46:54 CST 2020hello RocketMQ90
Wed Feb 19 20:46:54 CST 2020hello RocketMQ82
Wed Feb 19 20:46:54 CST 2020hello RocketMQ98
Wed Feb 19 20:46:54 CST 2020hello RocketMQ66
Wed Feb 19 20:46:54 CST 2020hello RocketMQ74
Wed Feb 19 20:46:54 CST 2020hello RocketMQ50
Wed Feb 19 20:46:54 CST 2020hello RocketMQ58
Wed Feb 19 20:46:54 CST 2020hello RocketMQ42
Wed Feb 19 20:46:54 CST 2020hello RocketMQ26
Wed Feb 19 20:46:54 CST 2020hello RocketMQ10
Wed Feb 19 20:46:54 CST 2020hello RocketMQ2
Wed Feb 19 20:46:54 CST 2020hello RocketMQ56
Wed Feb 19 20:46:54 CST 2020hello RocketMQ48
Wed Feb 19 20:46:54 CST 2020hello RocketMQ40
Wed Feb 19 20:46:54 CST 2020hello RocketMQ32
Wed Feb 19 20:46:54 CST 2020hello RocketMQ24
Wed Feb 19 20:46:54 CST 2020hello RocketMQ16
Wed Feb 19 20:46:54 CST 2020hello RocketMQ8
Wed Feb 19 20:46:54 CST 2020hello RocketMQ0
Wed Feb 19 20:46:54 CST 2020hello RocketMQ97
Wed Feb 19 20:46:54 CST 2020hello RocketMQ89
Wed Feb 19 20:46:54 CST 2020hello RocketMQ81
Wed Feb 19 20:46:54 CST 2020hello RocketMQ73
Wed Feb 19 20:46:54 CST 2020hello RocketMQ65
Wed Feb 19 20:46:54 CST 2020hello RocketMQ57
Wed Feb 19 20:46:54 CST 2020hello RocketMQ49
Wed Feb 19 20:46:54 CST 2020hello RocketMQ41
Wed Feb 19 20:46:54 CST 2020hello RocketMQ33
Wed Feb 19 20:46:54 CST 2020hello RocketMQ25
Wed Feb 19 20:46:54 CST 2020hello RocketMQ96
Wed Feb 19 20:46:54 CST 2020hello RocketMQ88
Wed Feb 19 20:46:54 CST 2020hello RocketMQ80
Wed Feb 19 20:46:54 CST 2020hello RocketMQ72
Wed Feb 19 20:46:54 CST 2020hello RocketMQ64

 

但是发现不是有序的。

 

posted @ 2020-02-19 20:48  护花使者  Views(1078)  Comments(0Edit  收藏  举报