Commit 43e11a59ee2ea271b982916b7b46d74000170e35
1 parent
a5e982ef
金蝶对接模型创建
Showing
12 changed files
with
145 additions
and
79 deletions
etrade-core/src/main/java/com/diligrp/etrade/core/util/JsonUtils.java
@@ -26,7 +26,7 @@ import java.util.TimeZone; | @@ -26,7 +26,7 @@ import java.util.TimeZone; | ||
26 | 26 | ||
27 | public class JsonUtils { | 27 | public class JsonUtils { |
28 | 28 | ||
29 | - private static ObjectMapper objectMapper = initObjectMapper(); | 29 | + private static final ObjectMapper objectMapper = initObjectMapper(); |
30 | 30 | ||
31 | private static ObjectMapper initObjectMapper(){ | 31 | private static ObjectMapper initObjectMapper(){ |
32 | Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder = new Jackson2ObjectMapperBuilder(); | 32 | Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder = new Jackson2ObjectMapperBuilder(); |
@@ -82,6 +82,14 @@ public class JsonUtils { | @@ -82,6 +82,14 @@ public class JsonUtils { | ||
82 | } | 82 | } |
83 | } | 83 | } |
84 | 84 | ||
85 | + public static <T> T fromJsonString(String json, JavaType javaType){ | ||
86 | + try { | ||
87 | + return objectMapper.readValue(json, javaType); | ||
88 | + } catch (JsonProcessingException ex) { | ||
89 | + throw new IllegalArgumentException("Deserialize json array exception", ex); | ||
90 | + } | ||
91 | + } | ||
92 | + | ||
85 | public static <T> String toJsonString(T object) { | 93 | public static <T> String toJsonString(T object) { |
86 | try { | 94 | try { |
87 | return objectMapper.writeValueAsString(object); | 95 | return objectMapper.writeValueAsString(object); |
etrade-thirdparty/src/main/java/com/diligrp/etrade/thirdparty/co/ErrorMessageCo.java
1 | package com.diligrp.etrade.thirdparty.co; | 1 | package com.diligrp.etrade.thirdparty.co; |
2 | 2 | ||
3 | +import com.diligrp.etrade.thirdparty.type.BusinessEnum; | ||
4 | + | ||
3 | /** | 5 | /** |
4 | * @Author: zhangmeiyang | 6 | * @Author: zhangmeiyang |
5 | * @CreateTime: 2024-09-10 11:32 | 7 | * @CreateTime: 2024-09-10 11:32 |
6 | * @Version: todo | 8 | * @Version: todo |
7 | */ | 9 | */ |
8 | -public class ErrorMessageCo extends BaseCo{ | 10 | +public class ErrorMessageCo { |
11 | + private String errorMessage; | ||
12 | + | ||
13 | + private String content; | ||
14 | + | ||
15 | + private BusinessEnum business; | ||
16 | + | ||
17 | + public String getContent() { | ||
18 | + return content; | ||
19 | + } | ||
20 | + | ||
21 | + public void setContent(String content) { | ||
22 | + this.content = content; | ||
23 | + } | ||
24 | + | ||
25 | + public String getErrorMessage() { | ||
26 | + return errorMessage; | ||
27 | + } | ||
28 | + | ||
29 | + public void setErrorMessage(String errorMessage) { | ||
30 | + this.errorMessage = errorMessage; | ||
31 | + } | ||
32 | + | ||
33 | + public BusinessEnum getBusiness() { | ||
34 | + return business; | ||
35 | + } | ||
9 | 36 | ||
37 | + public void setBusiness(BusinessEnum business) { | ||
38 | + this.business = business; | ||
39 | + } | ||
10 | } | 40 | } |
etrade-thirdparty/src/main/java/com/diligrp/etrade/thirdparty/co/TestCo.java
@@ -5,7 +5,7 @@ package com.diligrp.etrade.thirdparty.co; | @@ -5,7 +5,7 @@ package com.diligrp.etrade.thirdparty.co; | ||
5 | * @CreateTime: 2024-09-10 15:06 | 5 | * @CreateTime: 2024-09-10 15:06 |
6 | * @Version: todo | 6 | * @Version: todo |
7 | */ | 7 | */ |
8 | -public class TestCo extends BaseCo{ | 8 | +public class TestCo{ |
9 | private String name; | 9 | private String name; |
10 | 10 | ||
11 | public String getName() { | 11 | public String getName() { |
etrade-thirdparty/src/main/java/com/diligrp/etrade/thirdparty/demarcate/AbstractBusinessHandler.java
0 → 100644
1 | +package com.diligrp.etrade.thirdparty.demarcate; | ||
2 | + | ||
3 | +import com.diligrp.etrade.thirdparty.type.BusinessEnum; | ||
4 | + | ||
5 | +public abstract class AbstractBusinessHandler<T> { | ||
6 | + public void handle(String json){ | ||
7 | + T t = this.covertData(json); | ||
8 | + this.handle(t); | ||
9 | + } | ||
10 | + | ||
11 | + protected abstract void handle(T t); | ||
12 | + | ||
13 | + protected abstract T covertData(String json); | ||
14 | + | ||
15 | + public abstract BusinessEnum getType(); | ||
16 | +} |
etrade-thirdparty/src/main/java/com/diligrp/etrade/thirdparty/demarcate/ErrorHandler.java
0 → 100644
etrade-thirdparty/src/main/java/com/diligrp/etrade/thirdparty/exec/ZrHolder.java
1 | package com.diligrp.etrade.thirdparty.exec; | 1 | package com.diligrp.etrade.thirdparty.exec; |
2 | 2 | ||
3 | -import com.diligrp.etrade.thirdparty.handler.AbstractBusinessHandler; | 3 | +import com.diligrp.etrade.thirdparty.demarcate.AbstractBusinessHandler; |
4 | +import com.diligrp.etrade.thirdparty.demarcate.ErrorHandler; | ||
4 | import com.diligrp.etrade.thirdparty.type.BusinessEnum; | 5 | import com.diligrp.etrade.thirdparty.type.BusinessEnum; |
5 | import jakarta.annotation.Resource; | 6 | import jakarta.annotation.Resource; |
6 | import org.springframework.beans.factory.DisposableBean; | 7 | import org.springframework.beans.factory.DisposableBean; |
@@ -19,16 +20,19 @@ import java.util.concurrent.ConcurrentHashMap; | @@ -19,16 +20,19 @@ import java.util.concurrent.ConcurrentHashMap; | ||
19 | @Component | 20 | @Component |
20 | public class ZrHolder implements InitializingBean, DisposableBean { | 21 | public class ZrHolder implements InitializingBean, DisposableBean { |
21 | 22 | ||
22 | - public final static Map<BusinessEnum, AbstractBusinessHandler> CONTEXT = new ConcurrentHashMap<>(); | 23 | + public final static Map<BusinessEnum, AbstractBusinessHandler<?>> CONTEXT = new ConcurrentHashMap<>(); |
24 | + public final static Map<BusinessEnum, ErrorHandler> ERROR_HANDLER_CONTEXT = new ConcurrentHashMap<>(); | ||
23 | 25 | ||
24 | @Resource | 26 | @Resource |
25 | - private List<AbstractBusinessHandler> abstractBusinessHandlerList; | 27 | + private List<AbstractBusinessHandler<?>> abstractBusinessHandlerList; |
28 | + | ||
29 | + @Resource | ||
30 | + private List<ErrorHandler> errorHandlers; | ||
26 | 31 | ||
27 | @Override | 32 | @Override |
28 | public void afterPropertiesSet() throws Exception { | 33 | public void afterPropertiesSet() throws Exception { |
29 | - abstractBusinessHandlerList.forEach(e->{ | ||
30 | - CONTEXT.put(e.getType(), e); | ||
31 | - }); | 34 | + abstractBusinessHandlerList.forEach(e-> CONTEXT.put(e.getType(), e)); |
35 | + errorHandlers.forEach(e-> ERROR_HANDLER_CONTEXT.put(e.getBusinessType(), e)); | ||
32 | } | 36 | } |
33 | 37 | ||
34 | @Override | 38 | @Override |
etrade-thirdparty/src/main/java/com/diligrp/etrade/thirdparty/exec/ZrReceiver.java
1 | package com.diligrp.etrade.thirdparty.exec; | 1 | package com.diligrp.etrade.thirdparty.exec; |
2 | 2 | ||
3 | import com.diligrp.etrade.core.util.JsonUtils; | 3 | import com.diligrp.etrade.core.util.JsonUtils; |
4 | -import com.diligrp.etrade.thirdparty.co.BaseCo; | 4 | +import com.diligrp.etrade.thirdparty.co.ErrorMessageCo; |
5 | +import com.diligrp.etrade.thirdparty.message.GeneralMessage; | ||
5 | import com.diligrp.etrade.thirdparty.type.BusinessEnum; | 6 | import com.diligrp.etrade.thirdparty.type.BusinessEnum; |
6 | import com.rabbitmq.client.Channel; | 7 | import com.rabbitmq.client.Channel; |
7 | import jakarta.annotation.Resource; | 8 | import jakarta.annotation.Resource; |
@@ -37,34 +38,36 @@ public class ZrReceiver { | @@ -37,34 +38,36 @@ public class ZrReceiver { | ||
37 | @RabbitListener(bindings = @QueueBinding(value = @Queue(value = ZR_NORMAL_QUEUE, autoDelete = "false"), exchange = @Exchange(value = ZR_NORMAL_EXCHANGE), key = ZR_NORMAL_ROUTING), ackMode = "MANUAL") | 38 | @RabbitListener(bindings = @QueueBinding(value = @Queue(value = ZR_NORMAL_QUEUE, autoDelete = "false"), exchange = @Exchange(value = ZR_NORMAL_EXCHANGE), key = ZR_NORMAL_ROUTING), ackMode = "MANUAL") |
38 | public void receiveMessage(Channel channel, Message message) throws IOException { | 39 | public void receiveMessage(Channel channel, Message message) throws IOException { |
39 | var content = new String(message.getBody(), StandardCharsets.UTF_8); | 40 | var content = new String(message.getBody(), StandardCharsets.UTF_8); |
40 | - BaseCo baseCo = JsonUtils.fromJsonString(content, BaseCo.class); | 41 | + GeneralMessage msg = JsonUtils.fromJsonString(content, GeneralMessage.class); |
41 | try { | 42 | try { |
42 | - ZrHolder.CONTEXT.get(baseCo.getBusinessEnum()).handle(content); | 43 | + ZrHolder.CONTEXT.get(msg.getBusiness()).handle(msg.getData()); |
43 | channel.basicAck(message.getMessageProperties().getDeliveryTag(), false); | 44 | channel.basicAck(message.getMessageProperties().getDeliveryTag(), false); |
44 | } catch (Exception e) { | 45 | } catch (Exception e) { |
45 | channel.basicAck(message.getMessageProperties().getDeliveryTag(), false); | 46 | channel.basicAck(message.getMessageProperties().getDeliveryTag(), false); |
46 | - var t = baseCo.getBusinessEnum().getBusinessParam(content); | ||
47 | - t.setRetryCount(baseCo.getRetryCount() + 1); | ||
48 | - sendDelayMsg(DELAY, JsonUtils.toJsonString(t)); | 47 | + msg.setRetryCount(msg.getRetryCount()+1); |
48 | + sendDelayMsg(DELAY, JsonUtils.toJsonString(msg)); | ||
49 | } | 49 | } |
50 | } | 50 | } |
51 | 51 | ||
52 | @RabbitListener(queues = ZR_DEAD_QUEUE, ackMode = "MANUAL") | 52 | @RabbitListener(queues = ZR_DEAD_QUEUE, ackMode = "MANUAL") |
53 | public void handleCustomDelayMsg(Channel channel, Message message) throws IOException { | 53 | public void handleCustomDelayMsg(Channel channel, Message message) throws IOException { |
54 | var content = new String(message.getBody(), StandardCharsets.UTF_8); | 54 | var content = new String(message.getBody(), StandardCharsets.UTF_8); |
55 | - BaseCo baseCo = JsonUtils.fromJsonString(content, BaseCo.class); | 55 | + GeneralMessage msg = JsonUtils.fromJsonString(content, GeneralMessage.class); |
56 | try { | 56 | try { |
57 | - ZrHolder.CONTEXT.get(baseCo.getBusinessEnum()).handle(content); | 57 | + ZrHolder.CONTEXT.get(msg.getBusiness()).handle(msg.getData()); |
58 | channel.basicAck(message.getMessageProperties().getDeliveryTag(), false); | 58 | channel.basicAck(message.getMessageProperties().getDeliveryTag(), false); |
59 | } catch (Exception e) { | 59 | } catch (Exception e) { |
60 | channel.basicAck(message.getMessageProperties().getDeliveryTag(), false); | 60 | channel.basicAck(message.getMessageProperties().getDeliveryTag(), false); |
61 | - var t = baseCo.getBusinessEnum().getBusinessParam(content); | ||
62 | - t.setRetryCount(baseCo.getRetryCount() + 1); | ||
63 | - if (t.getRetryCount() > 3) { | ||
64 | - t.setErrorMessage(e.fillInStackTrace().getMessage()); | ||
65 | - t.setBusinessEnum(BusinessEnum.ERROR); | 61 | + msg.setRetryCount(msg.getRetryCount() + 1); |
62 | + if (msg.getRetryCount() > 3) { | ||
63 | + ErrorMessageCo errorMessageCo = new ErrorMessageCo(); | ||
64 | + errorMessageCo.setContent(msg.getData()); | ||
65 | + errorMessageCo.setErrorMessage(e.fillInStackTrace().getMessage()); | ||
66 | + errorMessageCo.setBusiness(msg.getBusiness()); | ||
67 | + msg.setData(JsonUtils.toJsonString(errorMessageCo)); | ||
68 | + msg.setBusiness(BusinessEnum.ERROR); | ||
66 | } | 69 | } |
67 | - sendDelayMsg(DELAY, JsonUtils.toJsonString(t)); | 70 | + sendDelayMsg(DELAY, JsonUtils.toJsonString(msg)); |
68 | } | 71 | } |
69 | } | 72 | } |
70 | 73 |
etrade-thirdparty/src/main/java/com/diligrp/etrade/thirdparty/handler/AbstractBusinessHandler.java deleted
100644 → 0
etrade-thirdparty/src/main/java/com/diligrp/etrade/thirdparty/handler/ErrorBusinessHandler.java
@@ -2,6 +2,8 @@ package com.diligrp.etrade.thirdparty.handler; | @@ -2,6 +2,8 @@ package com.diligrp.etrade.thirdparty.handler; | ||
2 | 2 | ||
3 | import com.diligrp.etrade.core.util.JsonUtils; | 3 | import com.diligrp.etrade.core.util.JsonUtils; |
4 | import com.diligrp.etrade.thirdparty.co.ErrorMessageCo; | 4 | import com.diligrp.etrade.thirdparty.co.ErrorMessageCo; |
5 | +import com.diligrp.etrade.thirdparty.demarcate.AbstractBusinessHandler; | ||
6 | +import com.diligrp.etrade.thirdparty.exec.ZrHolder; | ||
5 | import com.diligrp.etrade.thirdparty.type.BusinessEnum; | 7 | import com.diligrp.etrade.thirdparty.type.BusinessEnum; |
6 | import org.springframework.stereotype.Component; | 8 | import org.springframework.stereotype.Component; |
7 | 9 | ||
@@ -11,11 +13,14 @@ import org.springframework.stereotype.Component; | @@ -11,11 +13,14 @@ import org.springframework.stereotype.Component; | ||
11 | * @Version: todo | 13 | * @Version: todo |
12 | */ | 14 | */ |
13 | @Component | 15 | @Component |
14 | -public class ErrorBusinessHandler extends AbstractBusinessHandler { | 16 | +public class ErrorBusinessHandler extends AbstractBusinessHandler<ErrorMessageCo> { |
15 | @Override | 17 | @Override |
16 | - public void handle(String json) { | ||
17 | - ErrorMessageCo co = JsonUtils.fromJsonString(json, ErrorMessageCo.class); | ||
18 | - System.out.println(co.getErrorMessage()); | 18 | + protected void handle(ErrorMessageCo co) { |
19 | + ZrHolder.ERROR_HANDLER_CONTEXT.get(co.getBusiness()).saveAndRecordError(co.getContent(),co.getErrorMessage()); | ||
20 | + } | ||
21 | + @Override | ||
22 | + protected ErrorMessageCo covertData(String json) { | ||
23 | + return JsonUtils.fromJsonString(json, ErrorMessageCo.class); | ||
19 | } | 24 | } |
20 | 25 | ||
21 | @Override | 26 | @Override |
etrade-thirdparty/src/main/java/com/diligrp/etrade/thirdparty/handler/TestBusinessHandler.java
@@ -2,6 +2,8 @@ package com.diligrp.etrade.thirdparty.handler; | @@ -2,6 +2,8 @@ package com.diligrp.etrade.thirdparty.handler; | ||
2 | 2 | ||
3 | import com.diligrp.etrade.core.util.JsonUtils; | 3 | import com.diligrp.etrade.core.util.JsonUtils; |
4 | import com.diligrp.etrade.thirdparty.co.TestCo; | 4 | import com.diligrp.etrade.thirdparty.co.TestCo; |
5 | +import com.diligrp.etrade.thirdparty.demarcate.AbstractBusinessHandler; | ||
6 | +import com.diligrp.etrade.thirdparty.demarcate.ErrorHandler; | ||
5 | import com.diligrp.etrade.thirdparty.type.BusinessEnum; | 7 | import com.diligrp.etrade.thirdparty.type.BusinessEnum; |
6 | import org.springframework.stereotype.Component; | 8 | import org.springframework.stereotype.Component; |
7 | 9 | ||
@@ -11,15 +13,29 @@ import org.springframework.stereotype.Component; | @@ -11,15 +13,29 @@ import org.springframework.stereotype.Component; | ||
11 | * @Version: todo | 13 | * @Version: todo |
12 | */ | 14 | */ |
13 | @Component | 15 | @Component |
14 | -public class TestBusinessHandler extends AbstractBusinessHandler{ | 16 | +public class TestBusinessHandler extends AbstractBusinessHandler<TestCo> implements ErrorHandler { |
15 | @Override | 17 | @Override |
16 | - public void handle(String json) { | ||
17 | - TestCo testCo = JsonUtils.fromJsonString(json, TestCo.class); | 18 | + protected void handle(TestCo t) { |
18 | var s = 1/0; | 19 | var s = 1/0; |
19 | } | 20 | } |
21 | + @Override | ||
22 | + protected TestCo covertData(String json) { | ||
23 | + return JsonUtils.fromJsonString(json, TestCo.class); | ||
24 | + } | ||
20 | 25 | ||
21 | @Override | 26 | @Override |
22 | public BusinessEnum getType() { | 27 | public BusinessEnum getType() { |
23 | return BusinessEnum.TEST; | 28 | return BusinessEnum.TEST; |
24 | } | 29 | } |
30 | + | ||
31 | + @Override | ||
32 | + public BusinessEnum getBusinessType() { | ||
33 | + return BusinessEnum.TEST; | ||
34 | + } | ||
35 | + @Override | ||
36 | + public void saveAndRecordError(String json,String errorMessage) { | ||
37 | + System.out.println(json); | ||
38 | + System.out.println(errorMessage); | ||
39 | + System.out.println("save db"); | ||
40 | + } | ||
25 | } | 41 | } |
etrade-thirdparty/src/main/java/com/diligrp/etrade/thirdparty/co/BaseCo.java renamed to etrade-thirdparty/src/main/java/com/diligrp/etrade/thirdparty/message/GeneralMessage.java
1 | -package com.diligrp.etrade.thirdparty.co; | 1 | +package com.diligrp.etrade.thirdparty.message; |
2 | 2 | ||
3 | import com.diligrp.etrade.thirdparty.type.BusinessEnum; | 3 | import com.diligrp.etrade.thirdparty.type.BusinessEnum; |
4 | 4 | ||
5 | /** | 5 | /** |
6 | * @Author: zhangmeiyang | 6 | * @Author: zhangmeiyang |
7 | - * @CreateTime: 2024-09-10 11:30 | 7 | + * @CreateTime: 2024-09-12 14:45 |
8 | * @Version: todo | 8 | * @Version: todo |
9 | */ | 9 | */ |
10 | -public class BaseCo { | ||
11 | - private String errorMessage; | 10 | +public class GeneralMessage { |
11 | + private BusinessEnum business; | ||
12 | + private String data; | ||
12 | private Integer retryCount; | 13 | private Integer retryCount; |
13 | - private BusinessEnum businessEnum; | ||
14 | 14 | ||
15 | - public BaseCo() { | 15 | + public GeneralMessage() { |
16 | this.retryCount = 0; | 16 | this.retryCount = 0; |
17 | } | 17 | } |
18 | 18 | ||
19 | - public BusinessEnum getBusinessEnum() { | ||
20 | - return businessEnum; | 19 | + public BusinessEnum getBusiness() { |
20 | + return business; | ||
21 | } | 21 | } |
22 | 22 | ||
23 | - public void setBusinessEnum(BusinessEnum businessEnum) { | ||
24 | - this.businessEnum = businessEnum; | 23 | + public void setBusiness(BusinessEnum business) { |
24 | + this.business = business; | ||
25 | } | 25 | } |
26 | 26 | ||
27 | - public Integer getRetryCount() { | ||
28 | - return retryCount; | 27 | + public String getData() { |
28 | + return data; | ||
29 | } | 29 | } |
30 | 30 | ||
31 | - public void setRetryCount(Integer retryCount) { | ||
32 | - this.retryCount = retryCount; | 31 | + public void setData(String data) { |
32 | + this.data = data; | ||
33 | } | 33 | } |
34 | 34 | ||
35 | - public String getErrorMessage() { | ||
36 | - return errorMessage; | 35 | + public Integer getRetryCount() { |
36 | + return retryCount; | ||
37 | } | 37 | } |
38 | 38 | ||
39 | - public void setErrorMessage(String errorMessage) { | ||
40 | - this.errorMessage = errorMessage; | 39 | + public void setRetryCount(Integer retryCount) { |
40 | + this.retryCount = retryCount; | ||
41 | } | 41 | } |
42 | } | 42 | } |
etrade-thirdparty/src/main/java/com/diligrp/etrade/thirdparty/type/BusinessEnum.java
1 | package com.diligrp.etrade.thirdparty.type; | 1 | package com.diligrp.etrade.thirdparty.type; |
2 | 2 | ||
3 | -import com.diligrp.etrade.core.util.JsonUtils; | ||
4 | -import com.diligrp.etrade.thirdparty.co.BaseCo; | ||
5 | import com.diligrp.etrade.thirdparty.co.ErrorMessageCo; | 3 | import com.diligrp.etrade.thirdparty.co.ErrorMessageCo; |
6 | import com.diligrp.etrade.thirdparty.co.TestCo; | 4 | import com.diligrp.etrade.thirdparty.co.TestCo; |
7 | 5 | ||
@@ -10,30 +8,17 @@ import com.diligrp.etrade.thirdparty.co.TestCo; | @@ -10,30 +8,17 @@ import com.diligrp.etrade.thirdparty.co.TestCo; | ||
10 | * @CreateTime: 2024-09-10 10:53 | 8 | * @CreateTime: 2024-09-10 10:53 |
11 | * @Version: todo | 9 | * @Version: todo |
12 | */ | 10 | */ |
13 | -public enum BusinessEnum implements BusinessKlass{ | 11 | +public enum BusinessEnum{ |
14 | 12 | ||
15 | - ERROR("ERROR"){ | ||
16 | - @Override | ||
17 | - public ErrorMessageCo getBusinessParam(String content) { | ||
18 | - return JsonUtils.fromJsonString(content,ErrorMessageCo.class); | ||
19 | - } | ||
20 | - }, | 13 | + ERROR("ERROR", ErrorMessageCo.class), |
21 | 14 | ||
22 | - TEST("TEST"){ | ||
23 | - @Override | ||
24 | - public TestCo getBusinessParam(String content) { | ||
25 | - return JsonUtils.fromJsonString(content,TestCo.class); | ||
26 | - } | ||
27 | - }; | 15 | + TEST("TEST",TestCo.class); |
28 | 16 | ||
29 | public final String code; | 17 | public final String code; |
18 | + public final Class<?> clazz; | ||
30 | 19 | ||
31 | - BusinessEnum(String code) { | 20 | + BusinessEnum(String code, Class<?> clazz) { |
32 | this.code = code; | 21 | this.code = code; |
22 | + this.clazz = clazz; | ||
33 | } | 23 | } |
34 | } | 24 | } |
35 | - | ||
36 | -interface BusinessKlass<T extends BaseCo> { | ||
37 | - T getBusinessParam(String content); | ||
38 | - | ||
39 | -} |