RocketMQ生产者详解

一.管理方面的API

#创建topic
void createTopic(String key, String newTopic, int queueNum, int topicSysFlag)

#查找commitlog偏移量
队列最大偏移量
long maxOffset(final MessageQueue mq)
队列最小偏移量
long minOffset(final MessageQueue mq)
根据时间戳查找队列的偏移量
long searchOffset(MessageQueue mq, long timestamp)

#查找消息
根据offsetMsgId查找消息
MessageExt viewMessage(String offsetMsgId)
根据topic和msg查找消息
MessageExt viewMessage(String topic, String msgId)
根据topic和key朝着消息
QueryResult queryMessage(String topic, String key, int maxNum, long begin,long end)


二.生产者发送相关API

启听相关
void start()
void shutdown()

获取topic下所有队列
List <MessageQueue> fetchPublishMessageQueues(String topic)

消息发送相关
#异步发送
void send(Message msg, SendCallback sendCallback, long timeout)
#同步发送
void send(Message msg,  long timeout)
#自主选择队列发送消息,需要实现MessageQueueSelector接口
SendResult send(Message msg, MessageQueueSelector selector, Object arg,long timeout)
#发送事务消息,需要实现TransactionListener接口,arg参数对应下面方法
TransactionSendResult sendMessageInTransaction(Message msg,Object arg)
#不在乎结果的发送
void sendOneway(Message msg)
#批量发送消息
SendResult send(Collection<Message> msgs, long timeout)
SendResult send(Collection<Message> msgs, MessageQueue mq, long timeout)
#消息发送到broker,需要等到消费完才返回
Message request(Message msg, long timeout)

消息轨迹
可以追踪消息生产和消费的全流程
public DefaultMOProducer(final String producerGroup
,boolean enableMsgTrace,final String customizedTraceTopic){
  this( null, producerGroup,null,enableMsgTrace,customizedTraceTopic )
  }

生产者namespace作用
新建生产者时,可以传入namespace参数,会拼接到topic上,起到隔离环境的作用
DefaultMQProducer producer = new 
            DefaultMQProducer("namespace","please_rename_unique_group_name");

生产者发送消息简单实例

public class Producer {
    public static void main(String[] args) throws Exception{
        DefaultMQProducer producer = new 
            DefaultMQProducer("please_rename_unique_group_name");
        producer.setNamesrvAddr("127.0.0.1:9876");
        producer.start();
        //发送单条消息
        Message msg = new Message("TOPIC_TEST", "hello rocketmq".getBytes());
        SendResult sendResult = null;
        sendResult = producer.send(msg);
        // 输出结果
        System.out.printf("%s%n", sendResult);
        // 发送带 Key 的消息
        msg = n
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值