All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
org.yes.tools.mq.rocket.RocketMqUtils Maven / Gradle / Ivy
package org.yes.tools.mq.rocket;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.SendStatus;
import org.apache.rocketmq.common.message.Message;
import org.yes.tools.mq.properties.RocketMQProperties;
import java.nio.charset.StandardCharsets;
/**
* recketMq工具类
*/
@Slf4j
public class RocketMqUtils {
private static DefaultMQProducer producer = null;
private static volatile boolean flag = false;
/**
* 生产者单例(DCL单例,防止项目中各处发消息都初始化)
*
* @return
*/
public static DefaultMQProducer getProducer(String producerGroup) throws MQClientException {
if (!flag) {
synchronized (RocketMqUtils.class) {
if (!flag) {
//初始化生产者
producer = new DefaultMQProducer(producerGroup);
//关闭VIP通道
producer.setVipChannelEnabled(false);
//MQ地址
producer.setNamesrvAddr(RocketMQProperties.NAMESRV_ADDR);
producer.setMaxMessageSize(RocketMQProperties.MAX_MESSAGE_SIZE * 1024 * 1024);
//启动
try {
producer.start();
flag = true;
} catch (Exception e) {
producer = null;
log.error("MQ PRODUCER START FAIL:{}", e.getMessage());
throw e;
}
}
}
}
return producer;
}
/**
* 格式化消息
*
* @param topic
* @param tag
* @param msg
* @return
*/
public static Message parseMsg(String topic, String tag, String msg) {
Message message = null;
try {
message = new Message(topic, tag, msg.getBytes(StandardCharsets.UTF_8));
} catch (Exception e) {
log.error("FORMAT MESSAGE FAILED:{}", e.getMessage());
throw e;
}
return message;
}
/**
* 发送同步消息
*
* @param topic
* @param str
* @return
*/
public static boolean sendMQ(String producerGroup, String topic, String str) throws Exception {
DefaultMQProducer producer = getProducer(producerGroup);
Message msg = parseMsg(topic, "", str);
try {
SendResult sendResult = producer.send(msg);
return sendResult.getSendStatus().equals(SendStatus.SEND_OK);
} catch (Exception e) {
log.error("MESSAGE SENDING FAILED:{}", e.getMessage());
throw e;
}
}
/**
* 发送同步消息
*
* @param topic
* @param tag
* @param str
* @return
*/
public static boolean sendMQ(String producerGroup, String topic, String tag, String str) throws Exception {
DefaultMQProducer producer = getProducer(producerGroup);
Message msg = parseMsg(topic, tag, str);
try {
SendResult sendResult = producer.send(msg);
return sendResult.getSendStatus().equals(SendStatus.SEND_OK);
} catch (Exception e) {
log.error("MESSAGE SENDING FAILED:{}", e.getMessage());
throw e;
}
}
/**
* 发送同步延时消息
*
* @param topic
* @param tag
* @param str
* @param delayTimeLevel 1=1s 2=5s 3=10s 4=30s 5=1min 6=2min 7=3min 8=4min 9=5min 10=6min 11=7min 12=8min 13=9min 14=10min 15=20min 16=30min 17=1h 18=2h
* @return
*/
public static boolean sendMQ(String producerGroup, String topic, String tag, String str, int delayTimeLevel) throws Exception {
DefaultMQProducer producer = getProducer(producerGroup);
Message msg = parseMsg(topic, tag, str);
msg.setDelayTimeLevel(delayTimeLevel);
try {
SendResult sendResult = producer.send(msg);
return sendResult.getSendStatus().equals(SendStatus.SEND_OK);
} catch (Exception e) {
log.error("MESSAGE SENDING FAILED:{}", e.getMessage());
throw e;
}
}
/**
* 发送异步信息
*
* @param topic
* @param str
* @param sendCallback
*/
public static void sendMQ(String producerGroup, String topic, String str, SendCallback sendCallback) throws Exception {
DefaultMQProducer producer = getProducer(producerGroup);
Message msg = parseMsg(topic, "", str);
try {
producer.send(msg, sendCallback);
} catch (Exception e) {
log.error("MESSAGE SENDING FAILED:{}", e.getMessage());
throw e;
}
}
/**
* 发送异步信息
*
* @param topic
* @param tag
* @param str
* @param sendCallback
*/
public static void sendMQ(String producerGroup, String topic, String tag, String str, SendCallback sendCallback) throws Exception {
DefaultMQProducer producer = getProducer(producerGroup);
Message msg = parseMsg(topic, tag, str);
try {
producer.send(msg, sendCallback);
} catch (Exception e) {
log.error("MESSAGE SENDING FAILED:{}", e.getMessage());
throw e;
}
}
/**
* 发送单向消息
*
* @param topic
* @param tag
* @param str
*/
public static void sendOnWayMQ(String producerGroup, String topic, String tag, String str) throws Exception {
DefaultMQProducer producer = getProducer(producerGroup);
Message msg = parseMsg(topic, tag, str);
try {
producer.sendOneway(msg);
} catch (Exception e) {
log.error("MESSAGE SENDING FAILED:{}", e.getMessage());
throw e;
}
}
}