A brief analysis of spring integration of JMS Active MQ
1. Integrate with spring to realize synchronous receiving of messages by ptp
pom.xml:
<!-- --><dependency><groupid>org.springframework</groupid><artifactid>spring-jms</artifactid><version>4.3.7.RELEASE</version></dependency> <!-- --><dependency><groupid>org.apache.activemq</groupid><artifactid>activemq-pool</artifactid><version>5.9.0</version></dependency>
spring-jms.xml:
<?xml version="1.0" encoding="UTF-8"?><beans http:><!-- ActiveMQConnectionFactory就是JMS中负责创建到ActiveMQ连接的工厂类 --><bean> <property></property> </bean><!-- 创建连接池 --><bean> <property></property> <property></property> </bean> <!-- Spring为我们提供了多个ConnectionFactory,有SingleConnectionFactory和CachingConnectionFactory --><bean> <property></property> </bean> <!-- Spring提供的JMS工具类,它可以进行消息发送、接收等 --> <bean> <!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 --> <property></property> </bean> <!--这个是队列目的地,点对点的--> <bean> <constructor-arg></constructor-arg> </bean> </beans>
ConnectionFactory is used to generate links to the JMS server. Spring provides us with multiple ConnectionFactory, including SingleConnectionFactory and CachingConnectionFactory. SingleConnectionFactory will always return the same link for requests to establish a JMS server link, and will ignore the Connection's close method call. CachingConnectionFactory inherits SingleConnectionFactory, so it has all the functions of SingleConnectionFactory, and it also adds a new caching function, which can cache Session, MessageProducer and MessageConsumer. Here we use CachingConnectionFactory as an example.
Message producer:
= ClassPathXmlApplicationContext("spring-jms.xml"=(JmsTemplate) context.getBean("jmsTemplate"=(Destination) context.getBean("queueDestination" Message createMessage(Session session) session.createTextMessage("Hello spring JMS"
Consumer:
package com.jalja.org.jms.spring;import javax.jms.Destination;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.jms.core.JmsTemplate;public class SpringJmsReceive {public static void main(String[] args) { ApplicationContext context=new ClassPathXmlApplicationContext("spring-jms.xml"); JmsTemplate jmsTemplate=(JmsTemplate) context.getBean("jmsTemplate"); Destination queueDestination=(Destination) context.getBean("queueDestination"); String msg=(String) jmsTemplate.receiveAndConvert(queueDestination); System.out.println(msg); } }
2. Asynchronous call of PTP
We directly configure the listener for asynchronously receiving messages in spring, which is equivalent to configuring the consumer in spring. There is no need to start the consumer when receiving messages.
spring-jms.xml:
<?xml version="1.0" encoding="UTF-8"?><beans http:><!-- ActiveMQConnectionFactory就是JMS中负责创建到ActiveMQ连接的工厂类 --><bean> <property></property> </bean><!-- 创建连接池 --><bean> <property></property> <property></property> </bean> <!-- Spring为我们提供了多个ConnectionFactory,有SingleConnectionFactory和CachingConnectionFactory --><bean> <property></property> </bean> <!-- Spring提供的JMS工具类,它可以进行消息发送、接收等 --> <bean> <!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 --> <property></property> </bean> <!--这个是队列目的地,点对点的--> <bean> <constructor-arg></constructor-arg> </bean> <!-- 消息监听器 --> <bean></bean> <!-- 消息监听容器 --> <bean> <property></property> <property></property> <property></property> </bean> </beans>
After the producer sends a message to the specified destination Destination, the next step is for the consumer to consume the message at the specified destination. . So how does the consumer know that a producer has sent a message to the specified destination? This is achieved through the message listening container MessageListenerContainer that Spring encapsulates for us. It is responsible for receiving information and distributing the received information to the real MessageListener for processing. Each consumer needs a corresponding MessageListenerContainer for each destination. For the message listening container, in addition to knowing which destination to listen to, it also needs to know where to listen. That is to say, it also needs to know which JMS server to listen to. This is done by injecting a MessageConnectionFactory into it when configuring the MessageConnectionFactory. ConnectionFactory to achieve. So when we configure a MessageListenerContainer, we have three attributes that must be specified. One is the ConnectionFactory that indicates where to listen; one is the Destination that indicates what to listen to; and the other is the MessageListener that processes the message after receiving the message. Spring provides us with two types of MessageListenerContainer, SimpleMessageListenerContainer and DefaultMessageListenerContainer.
SimpleMessageListenerContainer: SimpleMessageListenerContainer will create a session and consumer Consumer at the beginning, and will use the standard JMS MessageConsumer.setMessageListener() method to register the listener and let the JMS provider call the listener. callback function. It does not dynamically adapt to runtime needs and participate in external transaction management. In terms of compatibility, it is very close to the standalone JMS specification, but is generally not compatible with Java EE's JMS limitations.
DefaultMessageListenerContainer:In most cases we still use DefaultMessageListenerContainer. Compared with SimpleMessageListenerContainer, DefaultMessageListenerContainer will dynamically adapt to runtime needs and can participate in external transaction management. It is a good balance between low requirements on JMS providers, advanced features such as transaction participation and compatibility with Java EE environments.
Message producer:
public static void main(String[] args) { ApplicationContext context=new ClassPathXmlApplicationContext("spring-jms.xml"); JmsTemplate jmsTemplate=(JmsTemplate) context.getBean("jmsTemplate"); Destination queueDestination=(Destination) context.getBean("queueDestination"); System.out.println("异步调用执行开始"); jmsTemplate.send(queueDestination, new MessageCreator(){ @Overridepublic Message createMessage(Session session) throws JMSException {return session.createTextMessage("Hello spring JMS"); } }); System.out.println("异步调用执行结束"); }
Message listener: MyMessageListener
public class MyMessageListener implements MessageListener{ @Overridepublic void onMessage(Message message) { TextMessage msg= (TextMessage) message;try { System.out.println("你好:"+msg.getText()); } catch (JMSException e) { e.printStackTrace(); } } }
Start the message producer and the execution result of the listener Yes:
异步调用执行开始 异步调用执行结束 你好:Hello spring JMS
3. Publish and subscribe to receive simultaneously
spring-jms.xml:
<?xml version="1.0" encoding="UTF-8"?><beans http:><!-- ActiveMQConnectionFactory就是JMS中负责创建到ActiveMQ连接的工厂类 --><bean> <property></property> </bean><!-- 创建连接池 --><bean> <property></property> <property></property> </bean> <!-- Spring为我们提供了多个ConnectionFactory,有SingleConnectionFactory和CachingConnectionFactory --><bean> <property></property> </bean> <!-- Spring提供的JMS工具类,它可以进行消息发送、接收等 --> <bean> <!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 --> <property></property> </bean> <!--这个是队列目的地,发布订阅--> <bean> <constructor-arg></constructor-arg> </bean> </beans>
Producer:
public static void main(String[] args) { ApplicationContext context=new ClassPathXmlApplicationContext("spring-jms.xml"); JmsTemplate jmsTemplate=(JmsTemplate) context.getBean("jmsTemplate"); Destination topicDestination=(Destination) context.getBean("topicDestination"); jmsTemplate.send(topicDestination, new MessageCreator(){ @Overridepublic Message createMessage(Session session) throws JMSException {return session.createTextMessage("Hello spring JMS topicDestination"); } }); }
Consumer:
public class SpringJmsSubscriber {public static void main(String[] args) { ApplicationContext context=new ClassPathXmlApplicationContext("spring-jms.xml"); JmsTemplate jmsTemplate=(JmsTemplate) context.getBean("jmsTemplate"); Destination topicDestination=(Destination) context.getBean("topicDestination"); String msg=(String) jmsTemplate.receiveAndConvert(topicDestination); System.out.println(msg); } }
The above is the detailed content of A brief analysis of spring integration of JMS Active MQ. For more information, please follow other related articles on the PHP Chinese website!

The class loader ensures the consistency and compatibility of Java programs on different platforms through unified class file format, dynamic loading, parent delegation model and platform-independent bytecode, and achieves platform independence.

The code generated by the Java compiler is platform-independent, but the code that is ultimately executed is platform-specific. 1. Java source code is compiled into platform-independent bytecode. 2. The JVM converts bytecode into machine code for a specific platform, ensuring cross-platform operation but performance may be different.

Multithreading is important in modern programming because it can improve program responsiveness and resource utilization and handle complex concurrent tasks. JVM ensures the consistency and efficiency of multithreads on different operating systems through thread mapping, scheduling mechanism and synchronization lock mechanism.

Java's platform independence means that the code written can run on any platform with JVM installed without modification. 1) Java source code is compiled into bytecode, 2) Bytecode is interpreted and executed by the JVM, 3) The JVM provides memory management and garbage collection functions to ensure that the program runs on different operating systems.

Javaapplicationscanindeedencounterplatform-specificissuesdespitetheJVM'sabstraction.Reasonsinclude:1)Nativecodeandlibraries,2)Operatingsystemdifferences,3)JVMimplementationvariations,and4)Hardwaredependencies.Tomitigatethese,developersshould:1)Conduc

Cloud computing significantly improves Java's platform independence. 1) Java code is compiled into bytecode and executed by the JVM on different operating systems to ensure cross-platform operation. 2) Use Docker and Kubernetes to deploy Java applications to improve portability and scalability.

Java'splatformindependenceallowsdeveloperstowritecodeonceandrunitonanydeviceorOSwithaJVM.Thisisachievedthroughcompilingtobytecode,whichtheJVMinterpretsorcompilesatruntime.ThisfeaturehassignificantlyboostedJava'sadoptionduetocross-platformdeployment,s

Containerization technologies such as Docker enhance rather than replace Java's platform independence. 1) Ensure consistency across environments, 2) Manage dependencies, including specific JVM versions, 3) Simplify the deployment process to make Java applications more adaptable and manageable.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 English version
Recommended: Win version, supports code prompts!