|
@@ -1,13 +1,5 @@
|
|
|
package com.fjhx.rocketmq.producer;
|
|
|
|
|
|
-import com.alibaba.fastjson.JSONObject;
|
|
|
-import com.fjhx.rocketmq.config.RocketMQProperties;
|
|
|
-import org.apache.rocketmq.client.producer.*;
|
|
|
-import org.apache.rocketmq.common.message.Message;
|
|
|
-import org.springblade.core.tool.api.R;
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
-import org.springframework.web.bind.annotation.PostMapping;
|
|
|
-import org.springframework.web.bind.annotation.RequestBody;
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
|
import java.util.UUID;
|
|
@@ -19,80 +11,5 @@ import java.util.UUID;
|
|
|
*/
|
|
|
@RestController
|
|
|
public class ProducerController {
|
|
|
- @Autowired
|
|
|
- private DefaultMQProducer defaultProducer;
|
|
|
-
|
|
|
- @Autowired
|
|
|
- private RocketMQProperties rocketMQProperties;
|
|
|
-
|
|
|
- private static final String TOPIC="rocketmq_test";
|
|
|
- private static final String TAG="rocketmq_official";
|
|
|
-
|
|
|
-
|
|
|
- * 同步发送消息
|
|
|
- */
|
|
|
- @PostMapping("/sendSyncMessage")
|
|
|
- private R sync(@RequestBody JSONObject map) throws Exception {
|
|
|
- Message msg = new Message();
|
|
|
- msg.setTopic(map.get("topic").toString());
|
|
|
- msg.setTags(map.get("tag").toString());
|
|
|
- msg.setKeys(String.valueOf(UUID.randomUUID()));
|
|
|
- msg.setBody(JSONObject.toJSONBytes(map.get("msg")));
|
|
|
-
|
|
|
- SendResult result = defaultProducer.send(msg);
|
|
|
- if(result.toString().contains("SEND_OK")){
|
|
|
- return R.success();
|
|
|
- }
|
|
|
- return R.fail("发送失败");
|
|
|
- }
|
|
|
-
|
|
|
- * 异步发送消息
|
|
|
- */
|
|
|
- @PostMapping("/sendAsyncMessage")
|
|
|
- private R async(@RequestBody JSONObject map) throws Exception {
|
|
|
- if(!map.get("topic").toString().contains("rocketmq_erp")){
|
|
|
- if(!map.get("msg").toString().contains("table")||map.get("msg").toString()==null||"".equals(map.get("msg").toString())){
|
|
|
- return R.fail("消息格式有误");
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- Message message = new Message(map.get("topic").toString(),map.get("tag").toString(),String.valueOf(UUID.randomUUID()),JSONObject.toJSONBytes(map.get("msg")));
|
|
|
-
|
|
|
- defaultProducer.send(message, new SendCallback() {
|
|
|
- @Override
|
|
|
- public void onSuccess(SendResult sendResult) {
|
|
|
- System.out.println("异步发送消息:"+sendResult);
|
|
|
- }
|
|
|
- @Override
|
|
|
- public void onException(Throwable e) {
|
|
|
- e.printStackTrace();
|
|
|
-
|
|
|
- try {
|
|
|
- defaultProducer.setRetryTimesWhenSendFailed(5);
|
|
|
- defaultProducer.send(message,5000L);
|
|
|
- } catch (Exception ex) {
|
|
|
- ex.printStackTrace();
|
|
|
- }
|
|
|
- }
|
|
|
- });
|
|
|
- return R.success();
|
|
|
- }
|
|
|
-
|
|
|
- * 单项发送消息
|
|
|
- */
|
|
|
-
|
|
|
- @PostMapping("/sendOneWayMessage")
|
|
|
- private R oneWay(@RequestBody JSONObject map) throws Exception {
|
|
|
- if(!map.get("topic").toString().contains("rocketmq_erp")){
|
|
|
- if(!map.get("msg").toString().contains("table")||map.get("msg").toString()==null||"".equals(map.get("msg").toString())){
|
|
|
- return R.fail("消息格式有误");
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- Message message = new Message(map.get("topic").toString(),map.get("tag").toString(),String.valueOf(UUID.randomUUID()),JSONObject.toJSONBytes(map.get("msg")));
|
|
|
-
|
|
|
- defaultProducer.sendOneway(message);
|
|
|
- return R.success();
|
|
|
- }
|
|
|
|
|
|
}
|