1. What is ActiveMQ?
ActiveMQ is an open source provided by apache, an intermediate plug-in for message delivery, which can be integrated with spring and is currently the most popular open source message bus. ActiveMQ is a JMS Provider implementation that fully supports the JMS1.1 and J2EE 1.4 specifications. Similar ones are rabbitMQ and kafka, which are the most messaging plugins.
2. Two ways for ActiveMQ to deliver messages
Prerequisite: The jar package of activemq needs to be introduced
Point-to-point mode (PTP): one consumer corresponds to one producer
Publish/Subscribe mode (Publish/Sub): After a producer sends a message, it can be received by multiple consumers.
JMS defines five message body formats, as well as message invocation types, allowing some different types of data to be sent and received, providing some level of compatibility with existing message formats.
StreamMessage : --JAVA original data stream
TextMessage: a string object
ObjectMessage : a serialized java object
BytesMessage : a bytes object
MapMessage: key -value pair in key/value mode
(1) Point-to-point method (PTP)
That is: a message producer corresponds to a consumer
Producer (Producer) implementation steps:
Step 1: Create a ConnectionFactory object and pass the ip and port of the server activemq as construction parameters
Step 2: Obtain the connection object Connection through the factory object created in the first step
Step 3: Open the connection and directly call the start method of the connection object.
Step 4: Create a Session object, which is created through the connection object
Step 5: Create a Destination object through the Session object (there are two ways for this object: topic and queue), here use queue
Step 6: Create a producer Producer object through the Session object
Step 7: Create a Message object, use the TextMessage object here to set the message content
Step 8: Send a message using the created producer object Producer
Step 9: Close resources (Producer object, Connection object, Session object)
@Test public void testQueueProducer() throws Exception { // The first step: Create a ConnectionFactory object, you need to specify the server ip and port number. // The ip and port number of the brokerURL server ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://ip address:61616" ); // Step 2: Use the ConnectionFactory object to create a Connection object. Connection connection = connectionFactory.createConnection(); // The third step: open the connection and call the start method of the Connection object. connection.start(); // The fourth step: use the Connection object to create a Session object. // The first parameter: whether to open the transaction. true: open the transaction, the second parameter is ignored. // Second parameter: Only meaningful when the first parameter is false. The reply mode for the message. 1. Automatic answer 2. Manual answer. Usually an automatic answer. Session session = connection.createSession( false , Session.AUTO_ACKNOWLEDGE); // Step 5: Use the Session object to create a Destination object (topic, queue), where a Queue object is created. // Parameters: The name of the queue. Queue queue = session.createQueue("test-queue" ); // Step 6: Use the Session object to create a Producer object. MessageProducer producer = session.createProducer(queue); // Step 7: Create a Message object and create a TextMessage object. /* TextMessage message = new ActiveMQTextMessage(); message.setText("hello activeMq,this is my first test."); */ TextMessage textMessage = session.createTextMessage("hello activeMq,this is my first test." ); // Step 8: Use the Producer object to send messages . producer.send(textMessage); // Step 9: Close the resource. producer.close(); session.close(); connection.close(); }
The consumer implements:
Step 1: Create a ConnectionFactory object and pass the ip and port of the server activemq as construction parameters
Step 2: Obtain the connection object Connection through the factory object created in the first step
Step 3: Open the connection and directly call the start method of the connection object.
Step 4: Create a Session object, which is created through the connection object
Step 5: Create a Destination object, use queue, which needs to be consistent with the producer's queue
Step 6: Create a Consumer Object
Step 7: Receive the message
Step 8: Print Received Messages
Step 9: Close the resource
Consumer code:
@Test public void testQueueConsumer() throws Exception { // Step 1: Create a ConnectionFactory object. ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://192.168.25.168:61616" ); // Step 2: Get a Connection object from the ConnectionFactory object. Connection connection = connectionFactory.createConnection(); // The third step: open the connection. Call the start method of the Connection object. connection.start(); // The fourth step: use the Connection object to create a Session object. Session session = connection.createSession( false , Session.AUTO_ACKNOWLEDGE); // Step 5: Use the Session object to create a Destination object. Keep the same queue as the sender, and the name of the queue is the same. Queue queue = session.createQueue("test-queue" ); // Step 6: Use the Session object to create a Consumer object. MessageConsumer consumer = session.createConsumer(queue); // Step 7: Receive messages. consumer.setMessageListener( new MessageListener() { @Override public void onMessage(Message message) { try { TextMessage textMessage = (TextMessage) message; String text = null ; // Get the content of the message text = textMessage.getText(); // Step 8: Print the message. System.out.println(text); } catch (JMSException e) { e.printStackTrace (); } } }); // Wait for keyboard input System.in.read(); // Step 9: Close the resource consumer.close(); session.close(); connection.close(); }
(2) Subscription and publishing to deliver messages: Topic
Producer implementation steps:
The steps are exactly the same as PTP, the difference is that when creating a Destination object, you need to create a topic object
Go directly to the code:
@Test public void testTopicProducer() throws Exception { // The first step: to create a ConnectionFactory object, you need to specify the server ip and port number. // The ip and port number of the brokerURL server ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://IP address:61616" ); // Step 2: Use the ConnectionFactory object to create a Connection object. Connection connection = connectionFactory.createConnection(); // The third step: open the connection and call the start method of the Connection object. connection.start(); // The fourth step: use the Connection object to create a Session object. // The first parameter: whether to open the transaction. true: open the transaction, the second parameter is ignored. // Second parameter: Only meaningful when the first parameter is false. The reply mode for the message. 1. Automatic answer 2. Manual answer. Usually an automatic answer. Session session = connection.createSession( false , Session.AUTO_ACKNOWLEDGE); // Step 5: Use the Session object to create a Destination object (topic, queue), where a topic object is created. // Parameters: The name of the topic. Topic topic = session.createTopic("test-topic" ); // Step 6: Use the Session object to create a Producer object. MessageProducer producer = session.createProducer(topic); // Step 7: Create a Message object and create a TextMessage object. /* * TextMessage message = new ActiveMQTextMessage(); message.setText( * "hello activeMq,this is my first test."); */ TextMessage textMessage = session.createTextMessage("hello activeMq,this is my topic test" ); // Step 8: Use the Producer object to send messages. producer.send(textMessage); // Step 9: Close the resource. producer.close(); session.close(); connection.close(); }
Steps for consumer implementation:
The steps are the same as those implemented by PTP consumers. The only difference is that when creating a Destination object, create a topic object, which must be consistent with the topic of the producer who publishes and subscribes.
Consumer code:
@Test public void testTopicConsumer() throws Exception { // Step 1: Create a ConnectionFactory object. ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://IP address:61616" ); // Step 2: Get a Connection object from the ConnectionFactory object. Connection connection = connectionFactory.createConnection(); // The third step: open the connection. Call the start method of the Connection object. connection.start(); // The fourth step: use the Connection object to create a Session object. Session session = connection.createSession( false , Session.AUTO_ACKNOWLEDGE); //Step 5: Use the Session object to create a Destination object. Keep the same topic as the sender, and the name of the topic is the same. Topic topic = session.createTopic("test-topic" ); // Step 6: Use the Session object to create a Consumer object. MessageConsumer consumer = session.createConsumer(topic); // Step 7: Receive messages. consumer.setMessageListener( new MessageListener() { @Override public void onMessage(Message message) { try { TextMessage textMessage = (TextMessage) message; String text = null ; // Get the content of the message text = textMessage.getText(); // Step 8: Print the message. System.out.println(text); } catch (JMSException e) { e.printStackTrace (); } } }); System.out.println( "topic consumer 03..." ); // Wait for keyboard input System.in.read(); // Step 9: close the resource consumer.close(); session.close(); connection.close(); }
Summary: The similarities and differences between the two ways of delivering messages
Similarities: The implementation steps are basically the same, with little differences
The difference: the method of PTP message delivery, after the message producer sends, the message will be persisted on the activemq server, if the message is consumed by the consumer, the message persisted on the server will be deleted at the same time.
The method of publishing and subscribing messages: After the message producer sends the message, if there is no consumer consumption, the message will not be persisted in the activemq client, and will disappear immediately. If the created message is consumed, the activemq server will display the message-related content. This is the exact opposite of PTP.
Note: The way of publishing and subscribing messages: it is also possible to achieve message persistence on the server side. Consumers need to subscribe to messages (register) on the activemq server first, and use the ID of the consumer client (as a unique identifier, because there can be multiple Consumer) and the ID of the message can be passed to the server.