1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
| package com.rocketmq.producer;
import lombok.extern.slf4j.Slf4j; import org.apache.rocketmq.spring.core.RocketMQTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component;
import java.time.LocalDateTime; import java.util.HashMap; import java.util.Map;
@Slf4j @Component public class MessageProducer { @Autowired private RocketMQTemplate rocketMQTemplate;
public void sendSyncMessage(String topic, String message) { try { log.info("发送同步消息: topic={}, message={}", topic, message); rocketMQTemplate.syncSend(topic, message); log.info("同步消息发送成功: topic={}", topic); } catch (Exception e) { log.error("同步消息发送失败: topic={}", topic, e); throw new RuntimeException("同步消息发送失败", e); } }
public void sendAsyncMessage(String topic, String message) { try { log.info("发送异步消息: topic={}, message={}", topic, message); rocketMQTemplate.asyncSend(topic, message, new org.apache.rocketmq.client.producer.SendCallback() { @Override public void onSuccess(org.apache.rocketmq.client.producer.SendResult sendResult) { log.info("异步消息发送成功: topic={}, msgId={}", topic, sendResult.getMsgId()); } @Override public void onException(Throwable e) { log.error("异步消息发送失败: topic={}", topic, e); } }); } catch (Exception e) { log.error("异步消息发送失败: topic={}", topic, e); throw new RuntimeException("异步消息发送失败", e); } }
public void sendOneWayMessage(String topic, String message) { try { log.info("发送单向消息: topic={}, message={}", topic, message); rocketMQTemplate.sendOneWay(topic, message); log.info("单向消息发送成功: topic={}", topic); } catch (Exception e) { log.error("单向消息发送失败: topic={}", topic, e); throw new RuntimeException("单向消息发送失败", e); } }
public void sendDelayMessage(String topic, String message, int delayLevel) { try { log.info("发送延迟消息: topic={}, message={}, delayLevel={}", topic, message, delayLevel); rocketMQTemplate.syncSend(topic, message, 3000, delayLevel); log.info("延迟消息发送成功: topic={}, delayLevel={}", topic, delayLevel); } catch (Exception e) { log.error("延迟消息发送失败: topic={}", topic, e); throw new RuntimeException("延迟消息发送失败", e); } }
public void sendOrderMessage(String topic, String message, String orderKey) { try { log.info("发送顺序消息: topic={}, message={}, orderKey={}", topic, message, orderKey); rocketMQTemplate.syncSendOrderly(topic, message, orderKey); log.info("顺序消息发送成功: topic={}, orderKey={}", topic, orderKey); } catch (Exception e) { log.error("顺序消息发送失败: topic={}", topic, e); throw new RuntimeException("顺序消息发送失败", e); } }
public void sendTransactionMessage(String topic, String message) { try { log.info("发送事务消息: topic={}, message={}", topic, message); rocketMQTemplate.sendMessageInTransaction(topic, message, null); log.info("事务消息发送成功: topic={}", topic); } catch (Exception e) { log.error("事务消息发送失败: topic={}", topic, e); throw new RuntimeException("事务消息发送失败", e); } }
public void sendBatchMessage(String topic, String[] messages) { try { log.info("发送批量消息: topic={}, count={}", topic, messages.length); for (String message : messages) { rocketMQTemplate.syncSend(topic, message); } log.info("批量消息发送成功: topic={}, count={}", topic, messages.length); } catch (Exception e) { log.error("批量消息发送失败: topic={}", topic, e); throw new RuntimeException("批量消息发送失败", e); } }
public void sendTagMessage(String topic, String tag, String message) { try { log.info("发送带标签的消息: topic={}, tag={}, message={}", topic, tag, message); rocketMQTemplate.syncSend(topic + ":" + tag, message); log.info("带标签的消息发送成功: topic={}, tag={}", topic, tag); } catch (Exception e) { log.error("带标签的消息发送失败: topic={}", topic, e); throw new RuntimeException("带标签的消息发送失败", e); } } }
|