Message adapter implementation system and method
The system is implemented through the message adapter, and the message sending executor and processor module are combined with the Spring framework, solving the problem of code reusability and maintenance difficulty of different message middleware adaptation, and achieving seamless support for multiple message middleware and flexibility.
Patent Information
- Application Number
- CN202411168872.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-08-23
- Publication Date
- 2024-12-31
AI Technical Summary
In modern distributed system development, different institutions use different message middleware to cause developers to write adaptive code for specific message middleware, which increases the difficulty and cost of project maintenance and reduces the reusability and universality of the code.
Provide a message adapter implementation system, including a message sending executor module and a message processor module, by obtaining the message type matching the pre-stored implementation class, using the Spring framework to mark the target implementation class as a bean, execute annotation methods, and implement multiple types of Seamless support for message middleware.
Reduces project maintenance difficulty and cost, improves code reusability and universality, and achieves seamless support for multiple message middleware. Easily switch message queue types through configuration rather than encoding, improving flexibility in software development. Sex and efficiency.
Smart Images

Figure CN119225996A_ABST
Abstract
Description
Technical Field
[0001] The present application belongs to the technical field of message adapters, and in particular, relates to a message adapter implementation system and method. Background Art
[0002] In the development of modern distributed systems, message middleware (such as Apache Kafka, Pulsar, etc.) is widely used to decouple services and improve system scalability and reliability. However, different organizations may prefer different message queue solutions due to factors such as technology stack, performance requirements, or existing infrastructure. This requires developers to write adaptation code for specific message middleware when designing the system, which increases the difficulty and cost of project maintenance and reduces the reusability and versatility of the code. Summary of the invention
[0003] Based on this, it is necessary to provide a message adapter implementation system and method to address the above technical issues.
[0004] In a first aspect, the present application provides a message adapter implementation system, the system comprising: a message sending executor module and a message processor module, the message sending executor module being connected to the message processor module;
[0005] The message sending executor module is used to obtain the type of the message and match it with multiple pre-stored implementation classes to obtain a target implementation class corresponding to the message, wherein the target implementation class is one of the multiple implementation classes;
[0006] The message sending executor module is also used to process the message according to the message processing logic of the target implementation class, and mark the target implementation class as a bean that needs to be managed through the spring framework to form an annotated message;
[0007] The message processor module stores all methods marked with the HrfaxMessageListener annotation. After receiving the annotated message, it is used to call the corresponding HrfaxMessageListener annotated method according to the annotated message and execute the processing logic of the annotated message.
[0008] In some practicable ways, the message sending executor module has an ApplicationContextAware interface;
[0009] The ApplicationContextAware interface is used to monitor the system startup that sends the message through the spring framework;
[0010] The message sending executor module is also used for:
[0011] Construct and initialize a class variable;
[0012] The implementation class of all HrfaxMessageProducer messages is stored in the class variable;
[0013] After the message is obtained, the message type is matched with the implementation classes of all HrfaxMessageProducer messages pre-stored in the class variable to obtain a target implementation class.
[0014] In some practicable ways, the message sending executor module includes a message sender interface;
[0015] The message sender interface is used to inherit the implementation class of the message sending executor module and implement the message sending logic of each implementation class accordingly;
[0016] The message sender interface is also used to hand over the instance of the implementation class to the spring framework for management through the spring framework annotation.
[0017] In some implementations, the message processor module includes a message receiving processor;
[0018] The message receiving processor has a postProcessAfterInitialization method that implements the BeanPostProcessor interface of the spring framework, including storing all methods marked with the HrfaxMessageListener annotation, so that when receiving a message with an annotation, the corresponding message receiving method is called;
[0019] The message receiving processor is used to:
[0020] After initializing all beans, use Java reflection to obtain the bean method Method for monitoring and get the monitoring result;
[0021] According to the monitoring results, filter out the methods marked with the HrfaxMessageListener annotation;
[0022] Store the method marked with the HrfaxMessageListener annotation into a class variable;
[0023] When receiving a message with an annotation, the corresponding method annotated by HrfaxMessageListener is called in the class variable to execute the processing logic of the message with an annotation.
[0024] In some implementations, the message receiving processor includes a message receiving processor interface;
[0025] The message receiving processor interface has a method that implements the HrfaxMessageConsumer interface and provides a marking annotation;
[0026] The message receiving processor interface is used to:
[0027] Initialize message monitoring logic;
[0028] When a message with an annotation is monitored, in the message processor module, the method Method marked with the HrfaxMessageListener annotation corresponding to the topic of the message with the annotation is obtained, and the Method is executed.
[0029] Some practicable methods further include a confirmation message interface module; the confirmation message interface module is connected to the message processor module;
[0030] The confirmation message interface module is used to inherit a new implementation class, wherein the new implementation class is different from the plurality of pre-stored implementation classes;
[0031] The message confirmation interface module is also used to use the new implementation class to implement the message confirmation logic of the message type, and after the annotated message is successfully executed, call the message confirmation method of the new implementation class to obtain that the message has been consumed.
[0032] Secondly, the present application provides a message adapter implementation method, which is applied to the aforementioned message adapter implementation system, and the method includes:
[0033] Obtaining the type of the message, and matching it with a plurality of pre-stored implementation classes to obtain a target implementation class corresponding to the message, wherein the target implementation class is one of the plurality of implementation classes;
[0034] According to the message processing logic of the target implementation class, the message is processed, and the target implementation class is marked as a bean that needs to be managed through the spring framework to form an annotated message;
[0035] When the annotated message is received, the corresponding method annotated by HrfaxMessageListener is called according to the annotated message to execute the processing logic of the annotated message.
[0036] In a third aspect, the present application provides a computer device, including a memory and a processor, wherein the memory stores a computer program, and the processor implements the aforementioned message adapter implementation system when executing the computer program.
[0037] In a fourth aspect, the present application provides a computer storage medium having a computer program stored thereon, wherein the computer program, when executed by a processor, implements the aforementioned message adapter implementation system.
[0038] Fifthly, the present application provides a computer program, which, when executed by a processor, implements the aforementioned message adapter implementation system.
[0039] Beneficial Effects
[0040] The present application provides a message adapter implementation system and method, the system includes: a message sending executor module and a message processor module, the message sending executor module is connected to the message processor module; the message sending executor module is used to obtain the type of the message, and match it with multiple pre-stored implementation classes to obtain the target implementation class corresponding to the message, wherein the target implementation class is one of the multiple implementation classes; the message sending executor module is also used to process the message according to the message processing logic of the target implementation class, and mark the target implementation class as a bean to be managed through the spring framework to form an annotated message; the message processor module stores all methods marked with the HrfaxMessageListener annotation, and after receiving the annotated message, it is used to call the corresponding HrfaxMessageListener annotated method according to the annotated message to execute the processing logic of the annotated message. The above system enables developers to only write adaptation code for specific message middleware when designing the system, reducing the difficulty and cost of project maintenance and improving the reusability and versatility of the code. This enables seamless support for multiple message middlewares and allows easy switching of message queue types through configuration rather than coding, thereby improving the flexibility and efficiency of software development. BRIEF DESCRIPTION OF THE DRAWINGS
[0041] In order to more clearly illustrate the technical solutions in the embodiments of the present application or the conventional technology, the drawings required for use in the embodiments or the conventional technology descriptions are briefly introduced below. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without creative work.
[0042] Figure 1 The schematic diagram is a message adapter implementation system in one embodiment. DETAILED DESCRIPTION
[0043] In order to facilitate understanding of the present application, the present application will be described more fully below with reference to the relevant drawings. Embodiments of the present application are provided in the drawings. However, the present application can be implemented in many different forms and is not limited to the embodiments described herein. On the contrary, the purpose of providing these embodiments is to make the disclosure of the present application more thorough and comprehensive.
[0044] Unless otherwise defined, all technical and scientific terms used herein have the same meaning as those commonly understood by those skilled in the art to which this application belongs. The terms used herein in the specification of this application are only for the purpose of describing specific embodiments and are not intended to limit this application. The term "and / or" used herein includes any and all couplings of one or more related listed items.
[0045] It can be understood that the terms "first", "second", etc. used in the present application can be used in this article to describe various elements, but these elements are not limited by these terms. These terms are only used to distinguish the first element from another element.
[0046] Some terms involved in this application are explained below to facilitate understanding of this application:
[0047] hrfax-message-spring-boot-starter is a hypothetical component name that represents a starter with specific features designed for Spring Boot applications.
[0048] Spring Framework, Spring Framework is an open source Java platform that provides comprehensive infrastructure support for developing Java applications. Spring handles the infrastructure so that developers can focus on the business logic of the application.
[0049] Bean refers to a Java object instantiated, configured, and controlled by the Spring IoC container. Beans are the core concept of the Spring framework, and they are managed by the Spring container for life cycle management, including creation, initialization, use, and destruction.
[0050] Implementation classes, in the Spring framework, refer to Java classes that implement an interface or inherit an abstract class. These classes provide specific implementation details of methods declared in an interface or abstract methods in an abstract class. When an implementation class is marked as a Spring Bean and created and managed by the Spring container, it is directly related to the IoC container of the Spring framework. In other words, when an implementation class is managed by the Spring container, it can be called a Bean in Spring.
[0051] HrfaxMessageListener refers to a custom annotation used for message listening and processing in the Spring framework.
[0052] Annotation, in Java, an annotation is a special tag that can be added to elements such as classes, methods, variables, parameters, etc. to provide metadata or instruct the compiler and runtime how to process the code.
[0053] The ApplicationContextAware interface is an interface in the Spring framework, which belongs to the Spring Aware interface series. It defines a mechanism that allows Spring beans that implement this interface to access Spring's ApplicationContext. By implementing the ApplicationContextAware interface, a bean can obtain a reference to the Spring application context, which is usually used to directly access other beans or resources provided by the Spring container from within the bean.
[0054] postProcessAfterInitialization is a method of the BeanPostProcessor interface in the Spring framework. BeanPostProcessor is a hook interface provided by the Spring framework that allows developers to insert custom logic in the process of initializing and managing beans in the Spring container.
[0055] BeanPostProcessor is an interface in the Spring framework. It is one of the extension points of Spring and allows developers to insert custom logic in the process of initializing and managing beans in the Spring container.
[0056] Method, in Java programming language, is a class. Method is a part of Java reflection API.
[0057] Reflection is a mechanism provided by the Java language that allows programs to query and use class information at runtime. Through reflection, the structure of a class (such as fields, methods, constructors, etc.) can be dynamically obtained.
[0058] like Figure 1 As shown, in the first aspect of the present application, a message adapter implementation system is provided, which is based on the Spring Boot framework and uses the starter method to design the hrfax-message-spring-boot-starter component system, which includes an abstract message sending executor module and a message processor module, and defines a general message sending and receiving logic to achieve abstract isolation of the message processing logic. The system includes: a message sending executor module and a message processor module, and the message sending execution module is connected to the message processor module.
[0059] The message sending executor module is used to obtain the type of message and match it with multiple pre-stored implementation classes to obtain the target implementation class corresponding to the message.
[0060] The target implementation class is one of the multiple implementation classes.
[0061] Specifically, the message sending executor module has an ApplicationContextAware interface, and the ApplicationContextAware interface is used to monitor the system startup of sending the message through the spring framework; the message sending executor can be understood as a message service executor, and the message sending executor module is also used to:
[0062] Construct and initialize a class variable;
[0063] The implementation class of all HrfaxMessageProducer messages is stored in the class variable;
[0064] After the message is obtained, the message type is matched with the implementation classes of all HrfaxMessageProducer messages pre-stored in the class variable to obtain a target implementation class.
[0065] It should be noted that in the Spring framework, the function of the message sending executor module (HrfaxMessageProcessor) is to obtain the message sending implementation class and send messages. Its implementation principle is to implement the ApplicationContextAware interface of the spring framework to monitor the startup of the system, store all the message implementation classes of HrfaxMessageProducer in a class variable of type Map (stored in a class variable of type Map, the key of Map can be the message type or other attributes used to identify different implementations, and the value is the instance of the corresponding HrfaxMessageProducer implementation class), and then, when calling to send a message, get the corresponding message sending implementation class according to the configured message type to send the message. In other words, the ApplicationContextAware interface allows the bean that implements the ApplicationContextAware interface to obtain a reference to the ApplicationContext when the Spring container is initialized. This enables the bean to access other beans or configuration information in the Spring container. For the startup of the monitoring system A, the purpose is to be able to access and use various resources and functions provided by the ApplicationContext after the Spring container is fully initialized. That is, the implementation class that implements the ApplicationContextAware interface can get notification after the Spring container is fully started and all beans are initialized by overriding the setApplicationContext method, and the bean that implements ApplicationContextAware can automatically assemble dependencies through the previous and next messages. This enables the bean to access other beans it depends on without hard-coding these dependencies in the code, thereby achieving automatic assembly of dependencies. The implementation of the ApplicationContextAware interface ensures that the bean correctly manages its resources during the life cycle of the Spring container. For example, when the Spring container is closed, the database connection or other resources can be released, thereby improving resource utilization. When the system that sends the message starts, it can dynamically decide which implementation class to use or configure specific bean properties based on the configuration of the previous and next messages of the system that sends the message, increasing the flexibility and configurability of the application.
[0066] It should also be noted that the purpose of the construction and initialization of class variables by the message sending executor module is to uniformly manage and access all message sending implementation classes in the application. When each implementation class implements the HrfaxMessageProducer interface, it means that each implementation class provides specific logic for sending messages, using a Map or other data structure as a class variable, so that the key is the message type or identifier, and the value is the corresponding HrfaxMessageProducer implementation class instance. When a message is sent, the message sending executor module first obtains the message to be sent and its type; next, it matches the implementation class. According to the type of the message (the type of the message refers to the type of the message middleware, such as Kafka is KafkaMessageProducer, Pulsa is PulsarMessageProducerr, etc.), the executor module searches and matches the corresponding HrfaxMessageProducer implementation class in the Map class variable. When the target implementation class is found, the message sending executor module calls the sendMessage method of the target implementation class to send the message. The above steps make it possible for the component system to send messages without knowing the specific sending logic, but only need to focus on the message type. It is also easy to expand. When adding new message sending implementation classes, you only need to ensure that they implement the HrfaxMessageProducer interface and register them in the Spring container without modifying the existing code.
[0067] The message sending executor module is also used to process the message according to the message processing logic of the target implementation class, and mark the target implementation class as a Bean that needs to be managed through the spring framework to form an annotated message.
[0068] Specifically, the message sending executor module includes a message sender interface; the message sender interface is used to inherit the implementation class of the message sending executor module and implement the message sending logic of each implementation class accordingly; the message sender interface is also used to hand over the instance of the implementation class to the spring framework for management through the spring framework annotation.
[0069] It should be noted that the message sender interface (HrfaxMessageProducer) defines a standard interface that specifies the basic method of sending messages, such as sendMessage, which provides a method for sending messages. The implementation principle is to inherit the implementation class of the interface, implement the message sending logic of each message type, and then hand over the implementation class instance to spring for management through spring's @Component annotation. This message sender interface provides a unified programming contract for all message sending implementation classes.
[0070] For the inheritance of the implementation class of the message sender interface, multiple implementation classes can be created for different types of messages or different message middleware. Each implementation class inherits from the HrfaxMessageProducer interface. In this way, each implementation class encapsulates the message type of the implementation class itself or the sending logic of the middleware. The message sender interface uses Spring annotations (such as @Component) to hand over the instance of the implementation class to the Spring container for management. These annotations not only mark the implementation class as a Spring Bean, but also allow Spring to automatically assemble dependencies and execute lifecycle callbacks. In this way, the Spring container will automatically discover these annotated implementation classes when it starts, create their instances, and inject the required dependencies. This automatic management mechanism simplifies the code, allowing developers to focus on the implementation of business logic. In other words, the message sending executor module is responsible for coordinating and managing the use of all implementation classes.
[0071] By defining a message sender interface (HrfaxMessageProducer), and allowing multiple implementation classes to inherit the interface to provide different message sending logics. These implementation classes are marked with annotations of the Spring framework and managed by the Spring container, thereby achieving automatic assembly and dependency injection. This not only improves the organization and manageability of the code, but also enhances the adaptability of the component system to different message types and middleware.
[0072] The message processor module stores all methods marked with the HrfaxMessageListener (annotation for listening to messages) annotation. After receiving the annotated message, it is used to call the corresponding HrfaxMessageListener annotated method according to the annotated message and execute the processing logic of the annotated message.
[0073] Among them, the function of the message processor module (HrfaxMessageListenerProcessor) is to store all methods marked with the HrfaxMessageListener annotation, so that the specific message receiving method can be called when the message is received; the implementation principle is to implement the postProcessAfterInitialization method of the spring BeanPostProcessor interface, which is used to monitor all beans after initialization, obtain the bean method Method by using Java reflection, and then filter out the methods marked with the HrfaxMessageListener annotation and store them in a class variable of type Map for the message receiving processor (consumer) to call.
[0074] Specifically, the message receiving processor has a postProcessAfterInitialization method that implements the BeanPostProcessor interface of the spring framework, including storing all methods marked with the HrfaxMessageListener annotation, so that when receiving a message with an annotation, the corresponding message receiving method is called;
[0075] The message receiving processor is used to:
[0076] After initializing all beans, use Java reflection to obtain the bean method Method for monitoring and get the monitoring result;
[0077] According to the monitoring results, filter out the methods marked with the HrfaxMessageListener annotation;
[0078] Store the method marked with the HrfaxMessageListener annotation into a class variable;
[0079] When receiving a message with an annotation, the corresponding method annotated by HrfaxMessageListener is called in the class variable to execute the processing logic of the message with an annotation.
[0080] It should be noted that the message receiving processor implements Spring's BeanPostProcessor interface, which allows custom logic to be executed after the Spring container initializes the bean. By implementing the postProcessAfterInitialization method, the message receiving processor can process after all beans are initialized. The message receiving processor is responsible for storing all methods marked with the @HrfaxMessageListener annotation. These methods define how to receive and process messages of a specific topic or type. Using Java's reflection API, the message receiving processor obtains all methods of the bean after the bean is initialized and checks whether they are marked with the @HrfaxMessageListener annotation. Next, the methods marked with the @HrfaxMessageListener annotation are filtered out from the obtained methods, and the references of these methods are stored in a class variable, such as a Map collection, in which the key represents the message topic and the value represents the corresponding method reference.
[0081] In addition, the message receiving processor includes a message receiving processor interface; the message receiving processor interface has a method that implements the HrfaxMessageConsumer interface and provides a marking annotation.
[0082] Among them, the receiving processor (consumer) interface (HrfaxMessageConsumer) is used to provide the function of receiving messages; the implementation principle is to implement the HrfaxMessageConsumer interface and provide a method marked with @PostConstruct annotation, which is used to initialize the message listening logic when the system starts. When a message is listened to, the method of receiving the message corresponding to the topic of the message is obtained from the message receiving processor (HrfaxMessageListenerProcessor), and then the method is executed. It can be understood that the logic of listening to messages in various message queues is basically different, so one implementation class is required for each message type, but the basic logic is to start a new thread and use a while loop to listen to or obtain messages.
[0083] The message receiving processor interface is used to:
[0084] Initialize message monitoring logic;
[0085] When a message with an annotation is monitored, in the message processor module, the method Method marked with the HrfaxMessageListener annotation corresponding to the topic of the message with the annotation is obtained, and the Method is executed.
[0086] It should be noted that the message receiving processor interface refers to an interface defined to implement the logic of message receiving. This message receiving processor interface will be extended from HrfaxMessageConsumer, which defines the basic method of consuming messages to ensure that the unified message consumption specifications can be followed. One or more methods are defined in the message receiving processor interface, which are marked with specific annotations (such as @HrfaxMessageListener) so that they can be called when annotated messages arrive. In addition, using the reflection mechanism of Java, at runtime, the methods of the bean are dynamically obtained, and these methods are marked as being able to process topic messages. The methods marked with the @HrfaxMessageListener annotation are filtered out from all bean methods, and they are stored in a centralized location, such as a Map collection, according to the topic. When a message with an annotation arrives, the message receiving processor will search and call the corresponding processing method in the stored methods according to the topic of the annotated message.
[0087] The message receiving processor is a component used to process asynchronous messages in the Spring framework. It initializes the message listening logic by implementing the HrfaxMessageConsumer interface and using specific annotation marking methods, and dynamically calls the corresponding processing method when a message is received. The effect is that the producer only needs to send messages to the specified topic, and the consumer marks the topic it listens to by annotating it, thus achieving decoupling between the producer and the consumer. New message types or topics can be easily integrated by simply adding new methods and using the @HrfaxMessageListener annotation without modifying existing code. By automatically discovering and calling marked methods, the need for manual configuration and code writing is reduced, reducing the possibility of errors. Centrally managing message listening logic makes the code easier to maintain and extend. It is easy for developers to add, modify, or delete message processing logic. In addition, message processing can be performed asynchronously, which improves the responsiveness and throughput of the component system.
[0088] In one embodiment, a message adapter implementation system further includes a confirmation message interface module; the confirmation message interface module is connected to the message processor module; the confirmation message interface module is used to inherit a new implementation class. The new implementation class is different from the pre-stored multiple implementation classes;
[0089] The message confirmation interface module is also used to use the new implementation class to implement the message confirmation logic of the message type, and after the annotated message is successfully executed, call the message confirmation method of the new implementation class to obtain that the message has been consumed.
[0090] It should be noted that the message confirmation interface module (HrfaxAcknowledge) is used when the message is configured to require manual confirmation. This function is used to manually confirm that the message has been consumed. The implementation principle is to inherit the implementation class of the message confirmation interface module interface, for example, KafkaMessageProducer, Kafka message sending implementation class, PulsarMessageProducer, Pulsar message sending implementation class, to implement the manual confirmation message logic of each message type, and then call the confirmation message method of this class after the message is consumed successfully to confirm that the message has been consumed.
[0091] Developers can create new implementation classes, for example, extended other message sending implementation classes of other message middleware, which inherit from the confirmation message interface module and implement confirmation logic specific to the message type. New implementation classes are different from pre-stored implementation classes. They provide customized confirmation logic for specific types of messages. Through the confirmation message interface, the new implementation class defines how to confirm that the message has been consumed, which may include interactions with the message middleware, such as submitting offsets or sending confirmation signals. The confirmation message interface module is triggered when the message processor module processes and executes messages marked with specific annotations (such as @HrfaxMessageListener). After the message processing logic is executed successfully, the confirmation message interface module calls the confirmation message method of the new implementation class to ensure that the message middleware knows that the message has been successfully processed.
[0092] The Confirmation Message Interface module is a component in the Spring framework that ensures that messages are processed and confirmed correctly. It provides customized confirmation logic for different types of messages by implementing specific interfaces and connecting with the message processor module. This design pattern allows developers to define how to confirm that a message has been consumed for each message type, thereby ensuring the reliability of message processing. By confirming that a message has been consumed, the component system can ensure that the message will not be processed repeatedly, improving the reliability of message processing.
[0093] In some embodiments, a message adapter implementation system further includes a message listening annotation component (HrfaxMessageListener), which is used to listen to message reception; the implementation principle is to mark the method that needs to receive the message with the annotation and indicate the monitored message topic, and when the message is received, the method call will be triggered to send the message to system B. The specific triggering principle of the message listening can refer to the description of the message receiving processor interface and the message receiving processor, which will not be repeated here.
[0094] It should be noted that the message listener annotation of the message listener annotation component is a custom annotation used in message-driven applications in the Spring framework to simplify the processing of message listeners. The HrfaxMessageListener annotation defines a standard for marking methods that need to be automatically called to process received messages. Use this annotation on methods that need to receive topic messages and specify the topic to be listened to. Among them, the topic is an identifier used to distinguish different message streams in the messaging system. Producers send messages to specific topics, and consumers subscribe to these topics to receive messages. The component that implements the message receiving processor interface using the message listener annotation is responsible for listening to messages from a specific topic and calling methods marked with the HrfaxMessageListener annotation when messages arrive. The HrfaxMessageListener annotation and its related message receiving processor mechanism provide a declarative way of message listening and processing. Developers can easily define message processing logic by adding annotations to methods and specifying topics. The message receiving processor is responsible for listening to messages and triggering corresponding processing methods, thereby realizing the decoupling of asynchronous message processing and business logic. It not only improves the reliability of message processing and the overall performance of the component system, but also enhances the adaptability of the component system to different message types and middleware. Each marked method is an independent component that specializes in handling the message receiving task of the topic associated with it.
[0095] Example
[0096] Send message implementation example
[0097] @Autowired
[0098] private HrfaxMessageProcessor hrfaxMessageProcessor;
[0099] / / Send message
[0100] hrfaxMessageProcessor.sendMessage(topic,message);
[0101] 6.8) Message Receiving Example
[0102] Use @HrfaxMessageListener annotation for message listening
[0103] @HrfaxMessageListener(topic="test-topic1")
[0104] public void consume(String message){
[0105] System.out.println("test-topic1-Received message: "+message);
[0106] }
[0107] In summary, the content of a message adapter implementation system provided by this application is as follows:
[0108] 1) Component design
[0109] The message adapter implementation system is based on the Spring Boot framework and uses the starter method to design the hrfax-message-spring-boot-starter component. The hrfax-message-spring-boot-starter component contains an abstract message sender and a message receiving processor, and defines a common message sending and receiving logic to achieve abstract isolation of the message processing logic.
[0110] Message sending executor: HrfaxMessageProcessor
[0111] Message receiving processor: HrfaxMessageListenerProcessor
[0112] Listening message annotation: HrfaxMessageListener
[0113] For users, they only need to pay attention to the message sending executor and the message listening annotation, and do not need to pay attention to the message receiving processor.
[0114] 2) Middleware Adaptation Layer
[0115] Implement specific message senders, receivers and confirmation messages for Kafka and Pulsar respectively, encapsulating their respective message production and consumption logic. Through the strategy mode (get the corresponding message producer implementation class through the configured message type to send messages, that is, get the currently configured message sending implementation class from the collection of message sending implementation classes), dynamically load the corresponding message middleware implementation according to the configuration, and shield the differences of the underlying message middleware (because each message queue implements a message sending implementation class, and does not affect the previously existing message queue implementation. When you need to switch message queues, you only need to change the configured message queue. For example, if the current configuration is kafka, when it is changed to Pulsar, you can switch message queues without changing any code or even re-publishing the project. For users, there is no need to pay attention to the differences between different message middleware implementations, thus shielding the differences of the underlying message middleware).
[0116] 3) Configuration and automatic assembly
[0117] Provides a unified configuration model, allowing developers to specify the type of message middleware used, connection parameters, etc. by simply modifying the configuration file. By using the automatic assembly function of Spring Boot, the message sending system (system A and system B) can automatically identify and assemble the appropriate middleware implementation when it starts.
[0118] 4) Extension mechanism
[0119] The plug-in architecture is designed to allow developers to easily expand support for other message middleware. Developers only need to implement the following interface and register it to the component's extension point without changing the core code, ensuring the openness and future scalability of the component system.
[0120] Message sender (producer) interface: HrfaxMessageProducer
[0121] Receiving processor (consumer) interface: HrfaxMessageConsumer
[0122] Confirmation message interface: HrfaxAcknowledge
[0123] The beneficial effects are as follows:
[0124] Improve code reusability: Through unified interface design, reduce code changes caused by changes in message middleware.
[0125] Reduce maintenance costs: Simplify configuration management and make it easy to switch and maintain different message queue systems.
[0126] Enhance system flexibility: quickly adapt to future technological developments and facilitate the introduction of new message middleware.
[0127] Improve development efficiency: Developers can focus on business logic without having to deeply understand the implementation details of each message queue.
[0128] This component system can provide software development teams with an efficient, flexible and future-oriented message processing solution, significantly improving the message middleware integration experience under the microservice architecture.
[0129] Based on the above content, the message adapter implementation system and method provided by the present application has the following differences and advantages compared with the general message middleware adapter:
[0130] 1) Difference
[0131] 1. Targeted design: It is customized for Spring Boot applications and directly integrated into the Spring Boot ecosystem. It uses the automatic configuration feature of Spring Boot to make integration easier and faster. Some other general adapters may require more manual configuration or are not fully optimized for Spring Boot.
[0132] 2. Initial support scope: This application can support popular message middleware such as Kafka and Pulsar, and has deeper integration and better optimization on some specific middleware.
[0133] 2) Advantages:
[0134] 1. Lightweight: This application focuses on several message middlewares, which are more lightweight, reduce unnecessary complexity, and are easy to understand and maintain.
[0135] 2. Extensibility: This application reserves an extension interface, which means that support for more message middleware can be easily added in the future, keeping the component forward-looking and adaptable.
[0136] 3. Code universality and reusability: By providing a unified API, the universality and reusability of the code are improved, and the cost of code changes caused by replacing the message middleware is reduced.
[0137] 4. Integration convenience: As a Spring Boot Starter, it can be seamlessly integrated into existing Spring Boot applications, using Spring's dependency injection and configuration management to greatly simplify integration work.
[0138] Secondly, the present application provides a message adapter implementation method, which is applied to the aforementioned message adapter implementation system, and the method includes:
[0139] Obtaining the type of the message, and matching it with a plurality of pre-stored implementation classes to obtain a target implementation class corresponding to the message, wherein the target implementation class is one of the plurality of implementation classes;
[0140] According to the message processing logic of the target implementation class, the message is processed, and the target implementation class is marked as a bean that needs to be managed through the spring framework to form an annotated message;
[0141] When the annotated message is received, the corresponding method annotated by HrfaxMessageListener is called according to the annotated message to execute the processing logic of the annotated message.
[0142] The specific explanation and description of a message adapter implementation can be referred to a message adapter implementation system, which will not be repeated here.
[0143] In a third aspect, the present application provides a computer device, including a memory and a processor, wherein the memory stores a computer program, and the processor implements the aforementioned message adapter implementation system when executing the computer program.
[0144] In a fourth aspect, the present application provides a computer storage medium having a computer program stored thereon, wherein the computer program, when executed by a processor, implements the aforementioned message adapter implementation system.
[0145] Fifthly, the present application provides a computer program, which, when executed by a processor, implements the aforementioned message adapter implementation system.
[0146] Those skilled in the art can understand that all or part of the processes in the above-mentioned embodiment methods can be completed by instructing the relevant hardware through a computer program, and the computer program can be stored in a non-volatile computer-readable storage medium. When the computer program is executed, it can include the processes of the embodiments of the above-mentioned methods. Among them, any reference to memory, storage, database or other media used in the embodiments provided in this application can include non-volatile and / or volatile memory. Non-volatile memory can include read-only memory (ROM), programmable ROM (PROM), electrically programmable ROM (EPROM), electrically erasable programmable ROM (EEPROM) or flash memory. Volatile memory can include random access memory (RAM) or external cache memory. As an illustration and not limitation, RAM is available in many forms, such as static RAM (SRAM), dynamic RAM (DRAM), synchronous DRAM (SDRAM), double data rate SDRAM (DDRSDRAM), enhanced SDRAM (ESDRAM), synchronous link (Synchlink) DRAM (SLDRAM), memory bus (Rambus) direct RAM (RDRAM), direct memory bus dynamic RAM (DRDRAM), and memory bus dynamic RAM (RDRAM).
[0147] The various embodiments in the present disclosure are described in a progressive manner, and the same or similar parts between the various embodiments can be referenced to each other, and each embodiment focuses on the differences from other embodiments.
[0148] The protection scope of the present disclosure is not limited to the above-mentioned embodiments. Obviously, those skilled in the art can make various changes and modifications to the present disclosure without departing from the scope and spirit of the present disclosure. If these changes and modifications fall within the scope of the claims of the present disclosure and their equivalents, the intention of the present disclosure also includes these changes and modifications.
Claims
1. A message adapter implementation system, characterized in that: The system comprises: a message sending executor module and a message processor module, wherein the message sending executor module is connected to the message processor module; The message sending executor module is used to obtain the type of the message and match it with multiple pre-stored implementation classes to obtain a target implementation class corresponding to the message, wherein the target implementation class is one of the multiple implementation classes; The message sending executor module is also used to process the message according to the message processing logic of the target implementation class, and mark the target implementation class as a bean that needs to be managed through the spring framework to form an annotated message; The message processor module stores all methods marked with the HrfaxMessageListener annotation. After receiving the annotated message, it is used to call the corresponding HrfaxMessageListener annotated method according to the annotated message and execute the processing logic of the annotated message.
2. The message adapter implementation system according to claim 1, characterized in that: The message sending executor module has an ApplicationContextAware interface; The ApplicationContextAware interface is used to monitor the system startup that sends the message through the spring framework; The message sending executor module is also used for: Construct and initialize a class variable; The implementation class of all HrfaxMessageProducer messages is stored in the class variable; After the message is obtained, the message type is matched with the implementation classes of all HrfaxMessageProducer messages pre-stored in the class variable to obtain a target implementation class.
3. The message adapter implementation system according to claim 1, characterized in that: The message sending executor module includes a message sender interface; The message sender interface is used to inherit the implementation class of the message sending executor module and implement the message sending logic of each implementation class accordingly; The message sender interface is also used to hand over the instance of the implementation class to the spring framework for management through the spring framework annotation.
4. The message adapter implementation system according to claim 1, characterized in that: The message processor module includes a message receiving processor; The message receiving processor has a postProcessAfterInitialization method that implements the BeanPostProcessor interface of the spring framework, including storing all methods marked with the HrfaxMessageListener annotation, so that when receiving a message with an annotation, the corresponding message receiving method is called; The message receiving processor is used to: After initializing all beans, use Java reflection to obtain the bean method Method for monitoring and get the monitoring result; According to the monitoring results, filter out the methods marked with the HrfaxMessageListener annotation; Store the method marked with the HrfaxMessageListener annotation into a class variable; When receiving a message with an annotation, the corresponding method annotated by HrfaxMessageListener is called in the class variable to execute the processing logic of the message with an annotation.
5. The message adapter implementation system according to claim 4, characterized in that: The message receiving processor comprises a message receiving processor interface; The message receiving processor interface has a method that implements the HrfaxMessageConsumer interface and provides a marking annotation; The message receiving processor interface is used to: Initialize message monitoring logic; When a message with an annotation is monitored, in the message processor module, the method Method marked with the HrfaxMessageListener annotation corresponding to the topic of the message with the annotation is obtained, and the Method is executed.
6. The message adapter implementation system according to claim 1, characterized in that: It also includes a message confirmation interface module; the message confirmation interface module is connected to the message processor module; The confirmation message interface module is used to inherit a new implementation class, wherein the new implementation class is different from the plurality of pre-stored implementation classes; The message confirmation interface module is also used to use the new implementation class to implement the message confirmation logic of the message type, and after the annotated message is successfully executed, call the message confirmation method of the new implementation class to obtain that the message has been consumed.
7. A message adapter implementation method, characterized in that: The message adapter implementation system applied to any one of claims 1 to 6, the method comprising: Obtaining the type of the message, and matching it with a plurality of pre-stored implementation classes to obtain a target implementation class corresponding to the message, wherein the target implementation class is one of the plurality of implementation classes; According to the message processing logic of the target implementation class, the message is processed, and the target implementation class is marked as a bean that needs to be managed through the spring framework to form an annotated message; When the annotated message is received, the corresponding method annotated by HrfaxMessageListener is called according to the annotated message to execute the processing logic of the annotated message.
8. A computer device comprising a memory and a processor, wherein the memory stores a computer program, wherein: When the processor executes the computer program, the message adapter implementation system according to any one of claims 1 to 6 is implemented.
9. A computer storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the message adapter implementation system according to any one of claims 1 to 6 is implemented.
10. A computer program, characterized in that When the computer program is executed by a processor, the message adapter implementation system according to any one of claims 1 to 6 is implemented.
