TaxReceiver.java
2.41 KB
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
package com.diligrp.tax.boot.receiver;
import com.diligrp.tax.boot.service.TaxReceiveService;
import com.diligrp.tax.central.domain.MessageContext;
import com.diligrp.tax.central.process.ProcessorChain;
import com.diligrp.tax.central.type.SystemType;
import com.rabbitmq.client.Channel;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.Optional;
import static com.diligrp.tax.boot.queue.TaxAutoPush.*;
/**
* @Author: zhangmeiyang
* @CreateTime: 2024-09-02 16:32
* @Version: todo
*/
@Component
@Slf4j
public class TaxReceiver {
@Resource
private TaxReceiveService taxReceiveService;
@Resource
private Map<SystemType, ProcessorChain> processorChainMap;
@RabbitListener(bindings =
@QueueBinding(
value = @Queue(value = NORMAL_QUEUE, autoDelete = "false"),
exchange = @Exchange(value = NORMAL_EXCHANGE),
key = NORMAL_ROUTING),
ackMode = "MANUAL"
)
@Transactional
public void receiveMessage(Channel channel, Message message) throws IOException {
var content = new String(message.getBody(), StandardCharsets.UTF_8);
log.info("tax-agent收到消息:{}", content);
MessageContext ctx = null;
try {
ctx = taxReceiveService.messagePreHandle(content);
var trans = processorChainMap.get(SystemType.from(ctx.getSystemType())).startProcess(ctx);
taxReceiveService.recordMapping(trans);
channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
log.info("tax-agent消息处理成功:{}", content);
} catch (Exception e) {
channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
Optional.ofNullable(ctx).ifPresentOrElse(c -> taxReceiveService.recordMappingError(e, c), () -> log.error("tax-agent解析错误:", e));
log.error("tax-agent消息处理失败:", e);
}
}
}