Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Analysis on the use of JMS message activemq

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail the analysis of the use of JMS message activemq for you. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

one。 Overview of understanding JMS1.1

For JMS, Baidu encyclopedia, it is introduced as follows: JMS, that is, Java message Service (Java Message Service) API is a Java platform about message-oriented Middleware (MOM)-oriented API, which is used to send messages between two applications or in distributed systems for asynchronous communication. Java message service is a platform-independent API, and most MOM providers provide support for JMS.

In short, JMS is a vendor-independent API that is used to access messages from the messaging system. It is similar to JDBC (Java Database Connectivity) and provides the function of asynchronous communication between applications.

JMS1.0 is the specification specified in jsr 194 (for the jsr specification, please click). The latest specification is JSR 343 JMS 2.0.

Well, having said so much, I'm just saying that JMS is just a set of api interfaces defined by sun in order to unify the manufacturer's interface specification.

1.2 JMS architecture

The description is as follows:

JMS providers (implementers of JMS, such as activemq jbossmq, etc.)

JMS client (a program or object that uses a provider to send a message, for example, in 12306, is responsible for sending a ticket purchase message to the processing queue to solve the ticket peak problem, so the program that sends the message to the queue and the program that gets the message from the queue are called customers)

JMS producers, JMS consumers (producers and customers responsible for creating and sending messages, consumers are customers responsible for receiving and processing messages)

JMS messages (objects that pass data between JMS customers)

JMS queue (an area that holds messages that are sent waiting to be read)

JMS topic (a mechanism that supports sending messages to multiple subscribers)

1.3. JMS object model

The connection factory (connectionfactory) client uses JNDI to find the connection factory, and then uses the connection factory to create an JMS connection.

A JMS connection represents an active connection between the JMS client and the server, which is established by the client by calling the method of the connection factory.

The JMS session session identifies the session state of the JMS client and server. The session is established on a JMS connection that identifies a session process between the client and the server.

The JMS destination Destinatio, also known as message queuing, is the actual message source

Producers and consumers

Message type, divided into queue type (priority first-in-first-out) and subscription type

two。 ActiveMQ installation and ideas 2.1. Installation of ActiveMQ

Download the installation package from the official website. Http://activemq.apache.org/download.html gives you permission to run chmod + XMagi windows. / active start | after stop starts, activeMQ will occupy two ports, one is the tcp port responsible for receiving and sending messages: 61616, and the other is based on the web port for user interface management: 8161. These two ports can be found in xml under conf. The http server uses jettry. The problem here is that it takes a long time for the management interface to be displayed after starting mq.

= "start the MQ server:

Depending on the operating system, go to the appropriate win64/win32 bit directory and double-click activemq.bat to start MQ.

When ActiveMQ starts by default, it starts the built-in jetty server and provides an admin application for monitoring ActiveMQ to enter the ActionMQ service monitoring address: enter http://127.0.0.1:8161/admin for browser

2.2 Integration ideas

The most powerful thing about Spring is its Bean, as well as its unique IOC (inversion of Control) and AOP (aspect-oriented programming) technology. With these, we can construct objects without the new keyword, and at the same time, we can easily initialize them with attributes injected into the class. If you have written JMS applications such as ActiveMQ, the two most important interfaces for both message producers and consumers are as follows: 1.ConnectionFactory 2.Destination

ConnectionFactory is the foundation of everything, with which we have Connection and then Session, and only through Session objects can we create message queues, build producers / consumers, and then send / receive messages. Destination is the end-result of everything. It is like a bus. Producers send messages to it and consumers take messages from it.

Just imagine, if all this can be managed by Spring's powerful Bean, we will be more convenient and concise when writing programs. Fortunately, ActiveMQ officially provides perfect Spring framework support, and all you need to do is configure it in the xml file.

Spring officially provides a class called JmsTemplate, this class is specifically used to deal with JMS, in the Bean configuration tag of this class, there are two attributes connectionFactory-ref and defaultDestination-ref corresponding to ConnectionFactory and Destination in JMS, if you are interested in viewing the source code, you can find that JmsTemplate has done a lot of work for us to create, we only need to use it to send and receive information on the ok, and ActiveMQ official also provides the corresponding implementation package.

3. Spring Integration 3.1 xml configuration mode 1 3.2 xml configuration mode 2: classpath:conf/spring-config.properties Import javax.jms.JMSException Import javax.jms.Message;import javax.jms.MessageListener;import javax.jms.TextMessage;import org.springframework.stereotype.Component;@Component (value = "simpleMsgListener") public class SimpleMsgListener implements MessageListener {/ / the action on receipt of the message @ Override public void onMessage (Message message) {TextMessage textMessage = (TextMessage) message Try {System.out.println ("received message:" + textMessage.getText ());} catch (JMSException e) {e.printStackTrace ();} IV. Spring boot configuration

Spring boot jsm official website

Integration of springboot and ActiveMQ

4.1 、 Introduce dependent configuration org.springframework.boot spring-boot-starter-activemq org.apache.activemq activemq-pool 4.2 application.properties configuration # ActiveMQ- -# Specify if the default broker URL should be in memory. Ignored if an explicit broker has been specified.#spring.activemq.in-memory=false# URL of the ActiveMQ broker. Auto-generated by default. For instance `tcp://localhost: 61616`spring.activemq.broker-url=tcp://127.0.0.1:61616# Login user of the broker.spring.activemq.user=admin# Login password of the broker.spring.activemq.password=admin# Trust all packages.#spring.activemq.packages.trust-all=false# Comma-separated list of specific packages to trust (when not trusting all packages). # spring.activemq.packages.trusted=# See PooledConnectionFactory.#spring.activemq.pool.configuration.*=# Whether a PooledConnectionFactory should be created instead of a regular ConnectionFactory. The spring.activemq.pool.enabled=true# Maximum number of pooled connections.spring.activemq.pool.max-connections=50# Connection expiration timeout in milliseconds.spring.activemq.pool.expiry-timeout=10000# Connection idle timeout in milliseconds.spring.activemq.pool.idle-timeout=30000spring.jms.pub-sub-domain=false4.3 boot class configures import javax.jms.Queue Import javax.jms.Topic;import org.apache.activemq.ActiveMQConnectionFactory;import org.apache.activemq.command.ActiveMQQueue;import org.apache.activemq.command.ActiveMQTopic;import org.springframework.beans.factory.annotation.Value;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.jms.config.DefaultJmsListenerContainerFactory;import org.springframework.jms.config.JmsListenerContainerFactory;@Configurationpublic class ActiveMQConfig {@ Value ("${queueName}") private String queueName @ Value ("${topicName}") private String topicName; @ Value ("${spring.activemq.user}") private String usrName; @ Value ("${spring.activemq.password}") private String password; @ Value ("${spring.activemq.broker-url}") private String brokerUrl; / * *

@ describe: configure queues

*

@ author: whh

*

Date: 5:56:22 on August 9, 2019

* @ return * / @ Bean public Queue queue () {return new ActiveMQQueue (queueName);} / * *

@ describe: configure theme

*

@ author: whh

*

Date: 5:56:40 on August 9, 2019

* @ return * / @ Bean public Topic topic () {return new ActiveMQTopic (topicName);} / * *

@ describe: connection factory

*

@ author: whh

*

Date: 5:57:14 on August 9, 2019

* @ return * / @ Bean public ActiveMQConnectionFactory connectionFactory () {return new ActiveMQConnectionFactory (usrName, password, brokerUrl);} / * *

@ describe: listening queue bean

*

@ author: whh

*

Date: 5:57:43 on August 9, 2019

* @ param connectionFactory * @ return * / @ Bean public JmsListenerContainerFactory jmsListenerContainerQueue (ActiveMQConnectionFactory connectionFactory) {DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory (); bean.setConnectionFactory (connectionFactory); return bean;} / * *

@ describe: listen to the topic bean

*

@ author: whh

*

Date: 5:58:17 on August 9, 2019

* @ param connectionFactory * @ return * / @ Bean public JmsListenerContainerFactory jmsListenerContainerTopic (ActiveMQConnectionFactory connectionFactory) {DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory (); / / set to publish and subscribe, and the production consumer mode bean.setPubSubDomain (true); bean.setConnectionFactory (connectionFactory); return bean is used by default 5. Sender and receiver use 5.1sender import javax.jms.Destination;import javax.jms.JMSException;import javax.jms.Message;import javax.jms.Session;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.jms.core.JmsTemplate;import org.springframework.jms.core.MessageCreator;import org.springframework.stereotype.Service @ Servicepublic class JmsSendDemo {private static final Logger LOGGER = LoggerFactory.getLogger (JmsSendDemo.class); @ Autowired private JmsTemplate jmsTemplate; public void execute () {LOGGER.debug ("perform scheduled tasks-Direct violation data timing: sent to the home page of the hospital for display -") Destination destination = (Destination) AppContext.getBean ("queue_test"); jmsTemplate.convertAndSend ("mailbox", new Email ("info@example.com", "Hello")); / / start another method boolean flag=true LOGGER.debug ("execution scheduled task ends -");} public void sendMessage (Destination destination,final String message) {LOGGER.debug ("send a message to AMQ with the message" + message) JmsTemplate.send (destination, new MessageCreator () {@ Override public Message createMessage (Session session) throws JMSException {return session.createTextMessage (message);}});}} 5.1 recipient import org.springframework.jms.annotation.JmsListener Import org.springframework.stereotype.Component;@Componentpublic class Receiver {@ JmsListener (destination = "mailbox", containerFactory = "myFactory") public void receiveMessage (Email email) {System.out.println ("Received");}} this is the end of the analysis on the use of JMS message activemq. I hope the above content can be helpful to you and learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report