1---------------------- Observer pattern ------------------------------
Observer pattern: Defines a one-to-many dependency between objects, and when an object's state changes, all objects that depend on it are notified of Automatic Updates. ACTIVEMQ Message Queuing
ActiveMQ is the most popular, powerful, open source messaging bus that Apache has produced. ActiveMQ is a JMS Provider that fully supports JMS1.1 and the Java EE 1.4 Specification Implementation , although it has been a long time since the introduction of the JMS specification , but JMS in today's Java the middle of the application still plays a special role.
2 、-------------Introduction to JMS -----------------------
the full name of JMS is the JavaMessage Service, or java messaging services. Used to send a message between two applications, or distributed systems, for asynchronous communication .
It is used primarily for messaging between producers and consumers, where producers are responsible for generating messages, and consumers are responsible for receiving messages.
To apply it to the actual business requirements we can use the producer to generate a message and send it at a specific time, and the corresponding consumer will complete the corresponding business logic after receiving the corresponding message.
There are two types of delivery for messages :
One is point-to-point. that one producer and one consumer correspond;
The other is the Publish / Subscribe mode , when a producer generates a message and sends it, it can be received by multiple consumers.
3 、--------------------- ActiveMQ 's installation -----------------------------------------------
go to http://activemq.apache.org/ download activemq
installation
Installation steps:
First step: Install jdk, need jdk1.7 above version
Step Two: unzip the ActiveMQ package.
Step Three: Enter the bin directory.
Start: [[email protected] bin]#./ACTIVEMQ start
Stop: [[email protected] bin]#./ACTIVEMQ stop
Fourth step: access to background management.
Http://192.168.25.168:8161/admin
User name:admin
Password:admin
4 、----------------------------- JMS message sending mode ---------------------------------------------------- --------------
(1) in a point-to-point or queue model , a producer posts messages to a particular queue, and a consumer reads messages from that queue. Here, the producer knows the consumer's queue and sends the message directly to the consumer's queue.
This pattern is summed up as: only one consumer will get the message. The producer does not need to be in a running state while the recipient is consuming the message , nor does the receiver need to be in a running state when the message is sent. Each successfully processed message is signed by the recipient.
(2) Publisher/subscriber Model support for publishing messages to a specific message topic. 0 or more subscribers may be interested in receiving messages from a specific message topic . In this model, publishers and Subscribers do not know each other. This pattern is like an anonymous bulletin board.
This pattern is summed up as: multiple consumers can get messages . There is a time dependency between the Publisher and the Subscriber. Publishers need to establish a subscription (subscription) so that customers can purchase subscriptions. Subscribers must maintain a continuous active state to receive messages,
Unless a subscriber establishes a persistent subscription. In that case, messages published when the Subscriber is not connected will be republished when the Subscriber is reconnected.
5 、----------------------------------Active integration Spring--------------------------------------------------- --
put ActiveMQ -dependent jar packages are added to the project.
Activemq-all-5.12.0.jar
with maven Engineering, add A dependency on the jar package:
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactid>
<version>5.11.2</version>
</dependency>
Configuration of both production and consumer messages is required----------------Spring file
6 、-------------------------------Consumers need to configure monitoring --------------------------------------
so consumers are passing Spring is implemented for our encapsulated message listener messagelistenercontainer , which is responsible for receiving information and distributing the received information to the real MessageListener for processing. Each customer must have a corresponding Messagelistenercontainer for each destination .
for a message listener, in addition to knowing which destination to listen to, you also need to know where to listen, that is, it needs to know to listen to which JMS server, which is injected with a connectionfactory at the time of configuration messageconnectionfactory to achieve.
so in the configuration of a when Messagelistenercontainer, there are three properties that must be specified:
1, one is to indicate where to listen to the connectionfactory
2, one is to indicate what to listen to Destination;
3, one is to receive the message after the message processing messagelistener.
used in the Messagelistenercontainer implementation class is defaultmessagelistenercontainer.
Public class Mymessagelistener implements MessageListener {
@Override
Public void onMessage (Message message) {
System. out. println (message);
}
}
Summary: When we go to work on some data, add or delete, etc. to make some changes to the data, we need to make corresponding changes in the place where the data is associated, using ACTIVEMQ message queue can help us to achieve this function.
The Java design pattern used here is what we call the Observer pattern, and of course, the message queue embodies some other design patterns, such as decorator patterns.
The viewer mode of the Java design pattern----combined with ACTIVEMQ message Queue description