依据不通音讯类型,进行不同的解决。
————————— 重构前 ———————————————-
@Override
public void onMessage(MapRecord<String, String, String> message) {log.info("MessageId: {}", message.getId());
log.info("Stream: {}", message.getStream());
log.info("Body: {}", message.getValue());
Map<String, String> msgValueMap = message.getValue();
String type = msgValueMap.get("type"); // 音讯类型
if ("音讯类型 1".equals(type)) {// 音讯类型 1 解决 例:userService.save(...);
} else if ("音讯类型 2".equals(type)) {// 音讯类型 2 解决 例:roleService.delete(...);
} else if ("音讯类型 3".equals(type)) {// 音讯类型 3 解决 例:taskService.save(...);
}
}
————————— 重构后 ———————————————-
@Autowired
private MessageHandlerContext messageHandlerContext;
@Override
public void onMessage(MapRecord<String, String, String> message) {log.info("MessageId: {}", message.getId());
log.info("Stream: {}", message.getStream());
log.info("Body: {}", message.getValue());
Map<String, String> msgValueMap = message.getValue();
MessageInfo<String, String> messageIfo = new MassageInfo();
messageInfo.setType(msgValueMap.get("type"));
messageInfo.setData(msgValueMap.get("data"));
messageHandlerContext.handler(messageIfo);
}
重构 -1:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
public @interface Message {
/** 音讯类型 */
String value();}
public interface MessageHandler {default String getType() {return this.getClass().getAnnotation(Message.class).value();}
@PostConstruct
default void register() {MessageHandlerContext.register(this);
}
void handler(MessageInfo messageInfo);
}
@Slf4j
@Component
public class MessageHandlerContext {private static final Map<String, MessageHandler> MESSAGE_HANDLER_MAP = new HashMap<>();
public static void register(MessageHandler messageHandler) {MESSAGE_HANDLER_MAP.put(messageHandler.getType(), messageHandler);
log.info("注册音讯解决:{}", messageHandler.getType());
}
public void handler(MessageInfo messageInfo) {Optional.ofNullable(MESSAGE_HANDLER_MAP.get(messageInfo.getType()))
.orElseThrow(() -> new IllegalArgumentException("未辨认的音讯类型:" + messageInfo.getType()))
.handler(messageInfo);
}
}
@Data
public class MessageInfo<T> {
/** 音讯类型 */
private String type;
/** 音讯内容 */
private T data;
}
重构 -2:
@Message("ExampleMessage")
@Slf4j
@Component
public class ExampleMessageHandler implements MessageHandler {
@Override
public void handler(MessageInfo messageInfo) {log.debug("ExampleMessageHandler->handler:{}", messageInfo);
}
}
@Message("TestMessage")
@Component
public class TestMessageHandler implements MessageHandler {
@Override
public void handler(MessageInfo messageInfo) {log.debug("TestMessageHandler->handler:{}", messageInfo);
}
}
测试:
@SpringBootTest
class DemoApplicationTests {
@Autowired
private MessageHandlerContext messageHandlerContext;
@Test
void contextLoads() {}
@Test
public void testMessage() {MessageInfo<String> testMessage = new MessageInfo<>();
testMessage.setType("TestMessage");
testMessage.setData("test");
messageHandlerContext.handler(testMessage);
MessageInfo<String> exampleMessage = new MessageInfo<>();
exampleMessage.setType("ExampleMessage");
exampleMessage.setData("example");
messageHandlerContext.handler(exampleMessage);
}
}
// 输入:// ......
// 注册音讯解决:ExampleMessage
// 注册音讯解决:TestMessage
// ......
// TestMessageHandler->handler:MessageInfo{type='TestMessage', data=test}
// ExampleMessageHandler->handler:MessageInfo{type='ExampleMessage', data=example}