1、application.properties或者其他配置文件
# Apache RocketMQ
# 发送同一类消息的设置为同一个group,保证唯一,默认不需要设置,rocketmq会使用ip@pid(pid代表jvm名字)作为唯一标示
rocketmq.producer.groupName=ordersProducerGroup
#mq的nameserver地址
rocketmq.producer.namesrvAddr=192.168.11.23:9876
#如果需要同一个jvm中不同的producer往不同的mq集群发送消息,需要设置不同的instanceName
rocketmq.producer.instanceName=vehicleProducer
#topic名称
rocketmq.producer.topic=ordersTopic
#根据实际情况设置消息的tag
rocketmq.producer.tag=ordersTag
#消息最大长度 1024*128
rocketmq.producer.maxMessageSize=131072
#发送消息超时时间
rocketmq.producer.sendMsgTimeout=10000
#消费端
rocketmq.consumer.namesrvAddr=192.168.11.23:9876
rocketmq.consumer.groupName=ordersConsumerGroup
rocketmq.consumer.topic=ordersTopic
rocketmq.consumer.tag=ordersTag
rocketmq.consumer.consumeThreadMin=20
rocketmq.consumer.consumeThreadMax=64
二、rocketMQ配置类
1)、消息提供者配置
RocketMQProducerConfiguration
package com.infun.orders.rocketmq.configuration;
import com.infun.orders.rocketmq.exception.RocketMQException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Bean;
/**
* 生产者配置
*
* @author 黄培桂
* @create 2018-08-22 12:05
**/
@SpringBootConfiguration
public class RocketMQProducerConfiguration {
public static final Logger LOGGER = LoggerFactory.getLogger(RocketMQProducerConfiguration.class);
@Value("${rocketmq.producer.groupName}")
private String groupName;
@Value("${rocketmq.producer.namesrvAddr}")
private String namesrvAddr;
@Value("${rocketmq.producer.instanceName}")
private String instanceName;
@Value("${rocketmq.producer.maxMessageSize}")
private int maxMessageSize ; //4M
@Value("${rocketmq.producer.sendMsgTimeout}")
private int sendMsgTimeout ;
@Bean
public DefaultMQProducer getRocketMQProducer() throws RocketMQException {
if (StringUtils.isBlank(this.groupName)) {
throw new RocketMQException("groupName is blank");
}
if (StringUtils.isBlank(this.namesrvAddr)) {
throw new RocketMQException("nameServerAddr is blank");
}
if (StringUtils.isBlank(this.instanceName)){
throw new RocketMQException("instanceName is blank");
}
DefaultMQProducer producer;
producer = new DefaultMQProducer(this.groupName);
producer.setNamesrvAddr(this.namesrvAddr);
producer.setInstanceName(instanceName);
producer.setMaxMessageSize(this.maxMessageSize);
producer.setSendMsgTimeout(this.sendMsgTimeout);
try {
producer.start();
LOGGER.info(String.format("producer is start ! groupName:[%s],namesrvAddr:[%s]"
, this.groupName, this.namesrvAddr));
} catch (MQClientException e) {
LOGGER.error(String.format("producer is error {}"
, e.getMessage(),e));
throw new RocketMQException(e);
}
return producer;
}
}
2)、消息消费者配置类
RocketMQConsumerConfiguration
package com.infun.orders.rocketmq.configuration;
import com.infun.orders.rocketmq.MessageListener;
import com.infun.orders.rocketmq.MessageProcessor;
import com.infun.orders.rocketmq.exception.RocketMQException;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Bean;
/**
* 消费者配置实现类
*
* @author 黄培桂
* @create 2018-08-22 12:07
**/
@SpringBootConfiguration
public class RocketMQConsumerConfiguration {
public static final Logger LOGGER = LoggerFactory.getLogger(RocketMQConsumerConfiguration.class);
@Value("${rocketmq.consumer.namesrvAddr}")
private String namesrvAddr;
@Value("${rocketmq.consumer.groupName}")
private String groupName;
@Value("${rocketmq.consumer.topic}")
private String topic;
@Value("${rocketmq.consumer.tag}")
private String tag;
@Value("${rocketmq.consumer.consumeThreadMin}")
private int consumeThreadMin;
@Value("${rocketmq.consumer.consumeThreadMax}")
private int consumeThreadMax;
@Autowired
@Qualifier("messageProcessorImpl")
private MessageProcessor messageProcessor;
@Bean
public DefaultMQPushConsumer getRocketMQConsumer() throws RocketMQException {
if (StringUtils.isBlank(groupName)){
throw new RocketMQException("groupName is null !!!");
}
if (StringUtils.isBlank(namesrvAddr)){
throw new RocketMQException("namesrvAddr is null !!!");
}
if (StringUtils.isBlank(topic)){
throw new RocketMQException("topic is null !!!");
}
if (StringUtils.isBlank(tag)){
throw new RocketMQException("tag is null !!!");
}
DefaultMQPushConsumer consumer = new DefaultMQPushConsumer(groupName);
consumer.setNamesrvAddr(namesrvAddr);
consumer.setConsumeThreadMin(consumeThreadMin);
consumer.setConsumeThreadMax(consumeThreadMax);
MessageListener messageListener = new MessageListener();
messageListener.setMessageProcessor(messageProcessor);
consumer.registerMessageListener(messageListener);
try {
consumer.subscribe(topic,this.tag);
consumer.start();
LOGGER.info("consumer is start !!! groupName:{},topic:{},namesrvAddr:{}",groupName,topic,namesrvAddr);
}catch (MQClientException e){
LOGGER.error("consumer is start !!! groupName:{},topic:{},namesrvAddr:{}",groupName,topic,namesrvAddr,e);
throw new RocketMQException(e);
}
return consumer;
}
}
三、自定义异常
package com.infun.orders.rocketmq.exception;
/**
* 自定义消息异常
*
* @author 黄培桂
* @create 2018-08-22 11:57
**/
public class RocketMQException extends Exception {
public RocketMQException() {
super();
}
public RocketMQException(String message) {
super(message);
}
public RocketMQException(String message, Throwable cause) {
super(message, cause);
}
public RocketMQException(Throwable cause) {
super(cause);
}
protected RocketMQException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
4、自定义消息监听
package com.infun.orders.rocketmq;
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.common.message.MessageExt;
import java.util.List;
/**
* 消息监听
*
* @author 黄培桂
* @create 2018-08-22 11:58
**/
public class MessageListener implements MessageListenerConcurrently{
private MessageProcessor messageProcessor;
public void setMessageProcessor(MessageProcessor messageProcessor) {
this.messageProcessor = messageProcessor;
}
@Override
public ConsumeConcurrentlyStatus consumeMessage(List msgs, ConsumeConcurrentlyContext context) {
for (MessageExt msg : msgs){
boolean result = messageProcessor.handleMessage(msg);
if (!result){
return ConsumeConcurrentlyStatus.RECONSUME_LATER;
}
}
return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
}
}
5、消费消息处理
package com.infun.orders.rocketmq;
import org.apache.rocketmq.common.message.MessageExt;
/**
* @author 黄培桂
* @create 2018-08-22 12:03
**/
public interface MessageProcessor {
/**
* 处理消息的接口
* @param messageExt
* @return
*/
public boolean handleMessage(MessageExt messageExt);
}
package com.infun.orders.rocketmq;
import org.apache.rocketmq.common.message.MessageExt;
import org.springframework.stereotype.Component;
/**
* 消息处理实现类
*
* @author 黄培桂
* @create 2018-08-22 12:04
**/
@Component
public class MessageProcessorImpl implements MessageProcessor{
@Override
public boolean handleMessage(MessageExt messageExt) {
System.out.println("receive : " + messageExt.toString());
return true;
}
}