消息队列在实际开发应用中,跨应用间通信,发送消息等,通史也可起到很好的削峰填谷的作用。
下面是基于activeMq的两种开发,第一种用原生的jms接口,第二种使用spring对mq进行整合
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test</groupId>
<artifactId>spring</artifactId>
<version>1.0-SNAPSHOT</version>
<name>spring</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.1.6.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.6.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.1.6.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context-support -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>5.1.6.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.activemq/activemq-spring -->
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-spring</artifactId>
<version>5.15.9</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jms -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>5.1.6.RELEASE</version>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
spring的配置文件 app-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="demoApp" class="test.DemoApp" />
<bean id="connectionFaactory" class="org.apache.activemq.ActiveMQConnectionFactory"/>
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFaactory"/>
</bean>
<bean id="destination" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg index="0" value="springMq-Test"/>
</bean>
<!-- 自定义的监听器-->
<bean id="myListener" class="test.MyListener"/>
<!-- 配置自动监听器-->
<bean id="javaConsumer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="destination" ref="destination"/>
<property name="connectionFactory" ref="connectionFaactory"/>
<property name="messageListener" ref="myListener"/>
</bean>
</beans>
MyListener.java自定义监听器
package test;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
/**
* 消息监听类
*/
public class MyListener implements MessageListener {
/**
* 当有消息被写入消息队列,会自动触发此方法
* @param message
*/
@Override
public void onMessage(Message message) {
try{
System.out.println("监听到有数据.."+((TextMessage)message).getText());
}catch (Exception e){
e.printStackTrace();
}
}
}
JmsTest.java测试类
package test;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import javax.jms.*;
public class JmsTest {
//直接使用jms生产者
public static void producers()throws Exception{
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
Connection conn = connectionFactory.createConnection();
Session session = conn.createSession(true, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue("test-queue");
MessageProducer producer = session.createProducer(destination);
for(int i=0;i<3;i++){
TextMessage message = session.createTextMessage("我是测试"+i);
Thread.sleep(2000);
producer.send(message);
}
session.commit();
session.close();
conn.close();
}
//直接使用jms消费者
public static void consumers()throws Exception{
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
Connection conn = connectionFactory.createConnection();
conn.start();
Session session = conn.createSession(true, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue("test-queue");
MessageConsumer consumer = session.createConsumer(destination);
int i=0;
while(i<3){
i++;
TextMessage message = (TextMessage) consumer.receive();
session.commit();
System.out.println("收到消息.."+message.getText());
}
session.close();
conn.close();
}
//整合spring的生产者
public static void producers1()throws Exception{
ApplicationContext ac = new ClassPathXmlApplicationContext("app-context.xml");
JmsTemplate jmsTemplate = (JmsTemplate) ac.getBean("jmsTemplate");
Destination destination = (Destination) ac.getBean("destination");
//发送消息
jmsTemplate.send(destination, new MessageCreator() {
@Override
public Message createMessage(Session session) throws JMSException {
return session.createTextMessage("我是spring测试队列");
}
});
}
/**
* 整合spring的消费者
* 当配置了自动监听后,此方法便可省略
* @param args
* @throws Exception
*/
public static void consumers1()throws Exception{
ApplicationContext ac = new ClassPathXmlApplicationContext("app-context.xml");
JmsTemplate jmsTemplate = (JmsTemplate) ac.getBean("jmsTemplate");
Destination destination = (Destination) ac.getBean("destination");
//接收消息
TextMessage msg = (TextMessage) jmsTemplate.receive(destination);
//System.out.println("接收消息.."+msg.getText());
}
public static void main(String[] args)throws Exception{
producers1();
//consumers1();
}
}
activeMq的web管理页面地址为http://127.0.0.1:8161/admin 默认账号密码为:admin/admin