I. Overview of messaging middleware
What is the message middleware
发送者将消息发送给消息服务器,消息服务器将消感存放在若千队列中,在合适的时候再将消息转发给接收者。
这种模式下,发送和接收是异步的,发送者无需等待; 二者的生命周期未必相同: 发送消息的时候接收者不一定运行,接收消息的时候发送者也不一定运行;一对多通信: 对于一个消息可以有多个接收者。
Two, JMS Introduction
2.1 What is JMS?
JMS是java的消息服务,JMS的客户端之间可以通过JMS服务进行异步的消息传输。
2.2 Message Model
- P2P (Point to Point):
1.P2P schematic diagram
which relate to the concept of the characteristics and wherein
1. the message queue (Queue)
2. Sender (Sender)
3. receivers (Receiver)
4. Each message is sent to a specific queue, the receiver acquires the message from the queue. It retains the message queue until they are consumed or timeout.
Features:
1. Each message has only one consumer (Consumer) (i.e., once the consumer, it is no longer message in the message queue)
between sender and receiver 2. no dependence on time, that is to say when the transmission after the messages were sent, whether the recipient has no running, it will not affect the message is sent to the queue
3. the recipient need to reply to the message queue after successful reception success - Pub / Sub (publish and subscribe)
Pub / Sub mode map
Which involves concepts and features
1. Theme (Topic)
2. Publisher (Publisher)
3. Subscribers (Subscriber)
client sends a message to the topic. More publishers send a message to Topic, the system will deliver these messages to multiple subscribers.
Pub / Sub characteristics
1. Each message can have multiple consumers
have a dependency on the time between 2 publishers and subscribers. After for a theme (Topic) subscribers, it must create a subscriber, the publisher's message to the consumer, and in order to consume the message, the subscriber must keep the state running.
- Consumer two way messages
- Synchronization
subscription or recipient to call the receive method to receive messages, receive method before the message can be received (or timeout before) will remain blocked.
2. Asynchronous
subscribers or recipients can be registered as a message listener. When the message arrives, the system automatically calls onMessage method listener.
Scenario:
User registration, modify the order inventory, log storage
ActiveMQ is used
Under window ActiveMQ installation
Extracting current activeMQ
enter start in 64-bit systems in the bin directory
to start a successful visit to page:
ActiveMQ is completed using peer (p2p) communications mode
The introduction of file dependencies pom
<dependencies>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-core</artifactId>
<version>5.7.0</version>
</dependency>
</dependencies>
Producer Code
public static void main(String[] args) throws JMSException {
//连接工厂JMS 用它创建连接
ConnectionFactory collectionFactory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER,
ActiveMQConnection.DEFAULT_PASSWORD, "tcp://127.0.0.1:61616");
Connection createConnection = collectionFactory.createConnection();
createConnection.start();
// Session: 一个发送或接收消息的线程
Session session = createConnection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
// Destination :消息的目的地;消息发送给谁.
// 获取session注意参数值xiaobai是Query的名字
Destination destination = session.createQueue("xiaobai");
//MessageProducer:消息生产者
MessageProducer producer = session.createProducer(destination);
//设置持久化
producer.setDeliveryMode(DeliveryMode.PERSISTENT);
for (int i = 0; i < 5; i++) {
System.out.println("我是生产者" + i);
sendMsg(session, producer, "我是生产者" + i);
session.commit();
}
System.out.println("我是生产者发送完毕" );
}
public static void sendMsg(Session session, MessageProducer producer, String i) throws JMSException {
TextMessage textMessage = session.createTextMessage("hello activemq" + i);
producer.send(textMessage);
}
Consumer Code:
//连接工厂,JMS 用它创建连接
ConnectionFactory collectionFactory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER,
ActiveMQConnection.DEFAULT_PASSWORD, "tcp://127.0.0.1:61616");
Connection createConnection = collectionFactory.createConnection();
createConnection.start();
//Session: 一个发送或接收消息的线程
Session session = createConnection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue("xiaobai");
MessageConsumer createConsumer = session.createConsumer(destination);
while (true) {
TextMessage textMessage = (TextMessage) createConsumer.receive();
if (textMessage != null) {
String text = textMessage.getText();
System.out.println(text);
// textMessage.acknowledge();
session.commit();
} else {
break;
}
System.out.println("消费者消费完毕");
}
Producers started to see in the middle of the message data
starts consumers to view consumption
Publish and subscribe
Manufacturer Code:
ConnectionFactory collectionFactory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER,
ActiveMQConnection.DEFAULT_PASSWORD, "tcp://127.0.0.1:61616");
Connection createConnection = collectionFactory.createConnection();
createConnection.start();
Session session = createConnection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);
MessageProducer producer = session.createProducer(null);
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
for (int i = 0; i < 5; i++) {
System.out.println("我是生产者" + i);
sendMsg(session, producer, "我是生产者" + i);
}
System.out.println("我是生产者发送完毕" );
}
public static void sendMsg(Session session, MessageProducer producer, String i) throws JMSException {
TextMessage textMessage = session.createTextMessage("hello activemq" + i);
Destination destination = session.createTopic("xiao_topic");
producer.send(destination,textMessage);
}
Consumer Code
ConnectionFactory collectionFactory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER,
ActiveMQConnection.DEFAULT_PASSWORD, "tcp://127.0.0.1:61616");
Connection createConnection = collectionFactory.createConnection();
createConnection.start();
Session session = createConnection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createTopic("xiao_topic");
MessageConsumer createConsumer = session.createConsumer(destination);
while (true) {
TextMessage textMessage = (TextMessage) createConsumer.receive();
if (textMessage != null) {
String text = textMessage.getText();
System.out.println(text);
} else {
break;
}
System.out.println("消费者消费完毕");
}
}
Consumers first start, after starting the producers