com.eworkcloud.rocket.RocketTemplate Maven / Gradle / Ivy
package com.eworkcloud.rocket;
import com.aliyun.mq.http.MQClient;
import com.aliyun.mq.http.MQProducer;
import com.aliyun.mq.http.model.AsyncCallback;
import com.aliyun.mq.http.model.AsyncResult;
import com.aliyun.mq.http.model.TopicMessage;
import org.springframework.util.StringUtils;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.UUID;
public class RocketTemplate {
private final MQProducer producer;
public RocketTemplate(RocketProperties properties, MQClient client) {
if (!StringUtils.hasText(properties.getTopic())) {
throw new IllegalStateException("Topic is null or empty.");
}
this.producer = client.getProducer(properties.getTopic());
}
private void transferMessage(TopicMessage message) {
// 检测Key
if (!StringUtils.hasText(message.getProperties().get("KEYS"))) {
message.setMessageKey(UUID.randomUUID().toString().replaceAll("-", "").toUpperCase());
}
}
public TopicMessage send(TopicMessage message) {
transferMessage(message);
return this.producer.publishMessage(message);
}
public TopicMessage send(TopicMessage message, long delay) {
transferMessage(message);
message.setStartDeliverTime(System.currentTimeMillis() + delay);
return this.producer.publishMessage(message);
}
public TopicMessage send(TopicMessage message, Date date) {
long longTime = date.getTime();
if (longTime < System.currentTimeMillis()) {
longTime = System.currentTimeMillis();
}
transferMessage(message);
message.setStartDeliverTime(longTime);
return this.producer.publishMessage(message);
}
public TopicMessage send(TopicMessage message, LocalDateTime date) {
ZoneId zoneId = ZoneId.systemDefault();
long longTime = date.atZone(zoneId).toInstant().getEpochSecond();
if (longTime < System.currentTimeMillis()) {
longTime = System.currentTimeMillis();
}
transferMessage(message);
message.setStartDeliverTime(longTime);
return this.producer.publishMessage(message);
}
public AsyncResult sendAsync(TopicMessage message, AsyncCallback callback) {
transferMessage(message);
return this.producer.asyncPublishMessage(message, callback);
}
public AsyncResult sendAsync(TopicMessage message, long delay, AsyncCallback callback) {
transferMessage(message);
message.setStartDeliverTime(System.currentTimeMillis() + delay);
return this.producer.asyncPublishMessage(message, callback);
}
public AsyncResult sendAsync(TopicMessage message, Date date, AsyncCallback callback) {
long longTime = date.getTime();
if (longTime < System.currentTimeMillis()) {
longTime = System.currentTimeMillis();
}
transferMessage(message);
message.setStartDeliverTime(longTime);
return this.producer.asyncPublishMessage(message, callback);
}
public AsyncResult sendAsync(TopicMessage message, LocalDateTime date, AsyncCallback callback) {
ZoneId zoneId = ZoneId.systemDefault();
long longTime = date.atZone(zoneId).toInstant().getEpochSecond();
if (longTime < System.currentTimeMillis()) {
longTime = System.currentTimeMillis();
}
transferMessage(message);
message.setStartDeliverTime(longTime);
return this.producer.asyncPublishMessage(message, callback);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy