Talking about ActiveMq (Part 1)

1. What is ActiveMQ?

    First explain MQ, mq: message queue As the name suggests, it is a message queue. MQ is a cross-process communication mechanism for passing messages between upstream and downstream. MQ is a common upstream and downstream "logical decoupling + physical decoupling" message communication service. The upstream message sending only needs to rely on MQ, and does not need to rely on other services both logically and physically.

    What is the use of MQ? First, understand a knowledge point (knock on the blackboard). The communication between java thread nodes relies on two methods. One is shared memory, which everyone should know. For example, our commonly used thread locks and semaphores are in this form. The second is message passing, and MQ is a form of message passing. Before introducing ActiveMq, we also need to introduce a knowledge point, which is JMS: java message service, Java message service, a technology in J2EE. JMS defines the interface for accessing message middleware in Java, and does not give reality. The message middleware that implements the JMS interface becomes the JMS Provider.

    Now we can introduce ActiveMq, ActiveMq: Apache launched, open source, message middleware (Message-Oriented Middleware, MOM) implemented by JMS Provider that fully supports JMS and J2EE specifications. Implement JMS Provider to help implement a high-availability, high-performance, scalable, easy-to-use and secure enterprise-level message service-oriented system. Features of ActiveMq, multi-protocol: TCP, SSL, NIO, UDP, etc., pluggable architecture, flexible customization, high-performance clustering, support for message persistence, etc.

    MQ is briefly introduced, but why use MQ? First look at the following two diagrams.


    The first mode is the traditional mode. A synchronously sends data to B, waits for the execution of B to end, and responds to A. In this scenario, A and B are bound together and have a strong coupling relationship. A good way to decouple is to use MQ. When A does not care about the return value of B, we can use the second method. The asynchronous message implemented in this way is similar to a mobile phone short message. The sender does not need to wait for the message receiver to respond, which reduces the coupling degree of software multi-system inheritance, and also ensures reliability. MQ can ensure that the message is stored reliably in the middleware, and only after it is received. Delete messages, and multiple messages can form an atomic transaction.

    What are the application scenarios of MQ, such as asynchronous processing, application decoupling, traffic cutting, log processing, message communication, etc. For example, when the sender sends the execution result of the relationship, but the receiver executes for a long time, how should this be done? design.


    When the caller requests to send a request to the receiver, the receiver will synchronously return the success of the call. After the execution is completed, the MQ will be called through the gateway, and the MQ will send the message result uniformly. Why do this instead of the receiver directly replying the result to the caller? First of all, the callback gateway should not call the upstream to notify the result. If this is the case, every time a new caller is added, the callback gateway needs to modify the code, and still Reverse dependency, using the callback gateway + MQ scheme, any new calls to WeChat payment do not need to modify the code, only the caller needs to subscribe to MQ. In addition, MQ can be used when traffic is shaving peaks. When there are some spike activities and multiple requests come in at the same time, messages are saved through MQ, and there is no need for the business side to control the traffic to ensure the stability and availability of the system.

    In short, if you care about the downstream execution result, you use RPC, and if you don't care about the downstream execution result, you use MQ.

    And what are the disadvantages of MQ. First, the system is more complex, with an additional MQ component; second, the message transmission path is longer, and the delay will increase; then the message reliability and repetition are contradictory, and it is difficult to ensure that the message is not lost or heavy; finally, the upstream cannot know the downstream. execution result.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325449183&siteId=291194637
Recommended