|
1 | 1 | package com.github.cadecode.uniboot.example.svc.controller; |
2 | 2 |
|
| 3 | +import cn.hutool.core.util.ObjUtil; |
3 | 4 | import com.github.cadecode.uniboot.common.plugin.mq.model.TxMsg; |
4 | 5 | import com.github.cadecode.uniboot.common.plugin.mq.util.RabbitUtil; |
5 | 6 | import com.github.cadecode.uniboot.common.plugin.mq.util.TxMsgKit; |
|
9 | 10 | import io.swagger.annotations.ApiOperation; |
10 | 11 | import lombok.RequiredArgsConstructor; |
11 | 12 | import lombok.extern.slf4j.Slf4j; |
| 13 | +import org.springframework.amqp.rabbit.connection.SimpleResourceHolder; |
12 | 14 | import org.springframework.transaction.annotation.Transactional; |
13 | 15 | import org.springframework.web.bind.annotation.GetMapping; |
14 | 16 | import org.springframework.web.bind.annotation.RequestMapping; |
@@ -56,14 +58,24 @@ public boolean sendObj(@RequestParam String exchange, @RequestParam String routi |
56 | 58 | @Transactional(rollbackFor = Exception.class) |
57 | 59 | @ApiOperation("发送事务消息") |
58 | 60 | @GetMapping("send_tx") |
59 | | - public boolean sendTx(@RequestParam String exchange, @RequestParam String routingKey) { |
60 | | - txMsgKit.sendTx(TxMsg.builder() |
61 | | - .bizType("Test biz") |
62 | | - .bizKey("TestBiz001") |
63 | | - .exchange(exchange) |
64 | | - .routingKey(routingKey) |
65 | | - .message("Test TxMsg") |
66 | | - .build()); |
| 61 | + public boolean sendTx(@RequestParam String exchange, @RequestParam String routingKey, |
| 62 | + @RequestParam(required = false) String connectionName) { |
| 63 | + try { |
| 64 | + if (ObjUtil.isNotNull(connectionName)) { |
| 65 | + SimpleResourceHolder.bind(RabbitUtil.template().getConnectionFactory(), connectionName); |
| 66 | + } |
| 67 | + txMsgKit.sendTx(TxMsg.builder() |
| 68 | + .bizType("Test biz") |
| 69 | + .bizKey("TestBiz001") |
| 70 | + .exchange(exchange) |
| 71 | + .routingKey(routingKey) |
| 72 | + .message("Test TxMsg") |
| 73 | + .build()); |
| 74 | + } finally { |
| 75 | + if (ObjUtil.isNotNull(connectionName)) { |
| 76 | + SimpleResourceHolder.unbindIfPossible(RabbitUtil.template().getConnectionFactory()); |
| 77 | + } |
| 78 | + } |
67 | 79 | return true; |
68 | 80 | } |
69 | 81 | } |
|
0 commit comments