生产者:
package com.lai.rocketmq.provider;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.MQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
@Component
@Slf4j
@Data
public class Producer {
@Value("${mq.groupName}")
private String groupName;
@Value("${mq.nameServer}")
private String nameServer;
@Value("${mq.toppic1}")
private String topic1;
private MQProducer mqProducer;
//初始化配置生产者
@PostConstruct
public void init() throws Exception {
DefaultMQProducer producer = new DefaultMQProducer(groupName);
//多个nameserver则以 ; 隔开
producer.setNamesrvAddr(nameServer);
producer.start();
}
//测试
@Autowired
private Producer producer;
public void testProvider() throws Exception {
//设置主题,内容等
Message message = new Message(topic1,"".getBytes());
message.setBody("".getBytes());
producer.getMqProducer().send(message);
producer.getMqProducer().send(message, new SendCallback() {
@Override
public void onSuccess(SendResult sendResult) {
//成功回调
}
@Override
public void onException(Throwable throwable) {
//异常回调
}
});
producer.getMqProducer().sendOneway(message);
}
}
消费者
package com.lai.rocketmq.consumer;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.*;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.common.message.MessageQueue;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.List;
@Component
@Slf4j
public class Consumer {
@Value("${mq.groupName}")
private String groupName;
@Value("${mq.nameServer}")
private String nameServer;
@Value("${mq.toppic1}")
private String topic1;
@PostConstruct
public void consumer() throws MQClientException {
//消费者的组名
DefaultMQPushConsumer consumer = new DefaultMQPushConsumer(groupName);
//指定NameServer地址,多个地址以 ; 隔开
consumer.setNamesrvAddr(nameServer);
//订阅topic和tag
consumer.subscribe("topic1","*");
//无序性的消费消息
consumer.registerMessageListener(new MessageListenerConcurrently() {
@Override
public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
//TODO 消费消息
for (MessageExt messageExt:list) {
messageExt.getMsgId();
messageExt.getQueueId();
messageExt.getBody();
messageExt.getBrokerName();
messageExt.getBornHostString();
log.info("消费消息{}",messageExt.getMsgId());
}
MessageQueue messageQueue = consumeConcurrentlyContext.getMessageQueue();
int queueId = messageQueue.getQueueId();
return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
}
});
//有顺序消费消息
/**consumer.registerMessageListener(new MessageListenerOrderly() {
@Override
public ConsumeOrderlyStatus consumeMessage(List<MessageExt> list, ConsumeOrderlyContext consumeOrderlyContext) {
return null;
}
});**/
//开启消费者
consumer.start();
}
}