Java消息中间件之Spring中的ActiveMQ

时间:2021-10-05 10:42:42

创建一个普通的maven项目

引入项目依赖

<?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>com.ysk</groupId>
<artifactId>jms-spring</artifactId>
<version>1.0-SNAPSHOT</version>

<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-core</artifactId>
<version>5.7.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>4.3.0.RELEASE</version>
<exclusions>
<exclusion>
<artifactId>spring-context</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
</exclusions>
</dependency>

</dependencies>
</project>

创建资源文件
common.xml

<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"
>

<context:annotation-config/>

<!--ActiveMQ 为我们提供的ConnectionFactory-->
<bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://127.0.0.1:61616"/>
</bean>
<!--Spring jms为我们提供的连接池-->
<bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
<property name="targetConnectionFactory" ref="targetConnectionFactory"/>
</bean>
<!--一个队列目的地,点对点的-->
<bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg value="queue"/>
</bean>
<!--一个主题模式目的地,发布订阅模式-->
<bean id="topicDestination" class="org.apache.activemq.command.ActiveMQTopic">
<constructor-arg value="topic"/>
</bean>
</beans>

消费者Consumer.xml

<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"
>

<!--导入公共配置-->
<import resource="common.xml"/>
<!--配置消息监听容器-->
<bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory"/>
<!--<property name="destination" ref="queueDestination"/>-->
<property name="destination" ref="topicDestination"/>
<property name="messageListener" ref="consumerMessageListener"/>
</bean>
<!--配置消息监听器-->
<bean id="consumerMessageListener" class="com.ysk.jms.consumer.ConsumerMessageListener"/>
</beans>

Producer.xml

<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"
>

<import resource="common.xml"/>
<!--配置jmsTemplate,用于发送消息-->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory"/>
</bean>
<bean class="com.ysk.jms.producer.ProducerServiceImpl"></bean>
</beans>

生产者服务接口

package com.ysk.jms.producer;

/**
* Created by Y.S.K on 2017/8/29 in jmsspring.
*/

public interface ProducerService {
void sendMessage(String message);
}

生产者服务实现类

package com.ysk.jms.producer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;

import javax.annotation.Resource;
import javax.jms.*;


/**
* Created by Y.S.K on 2017/8/29 in jmsspring.
*/

public class ProducerServiceImpl implements ProducerService {
@Autowired
JmsTemplate jmsTemplate;
// @Resource(name = "queueDestination")
@Resource(name = "topicDestination")
Destination destination;

public void sendMessage(final String message) {
//使用jmsTemplate发送消息
jmsTemplate.send(destination, new MessageCreator() {
//创建一个消息
public Message createMessage(Session session) throws JMSException {
TextMessage textMessage = session.createTextMessage(message);
return textMessage;
}
});
System.out.println("发送消息:" + message);
}
}

生产者测试类

package com.ysk.jms.producer;

import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
* Created by Y.S.K on 2017/8/29 in jmsspring.
*/

public class AppProducer {
public static void main(String[] args) {
ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("classpath:producer.xml");
ProducerService producerService = classPathXmlApplicationContext.getBean(ProducerService.class);
for (int i = 0; i < 100; i++) {
producerService.sendMessage("Y.S.K " + i);
}
classPathXmlApplicationContext.close();
}
}

消费者消息监听类

package com.ysk.jms.consumer;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

/**
* Created by Y.S.K on 2017/8/29 in jmsspring.
*/

public class ConsumerMessageListener implements MessageListener {
public void onMessage(Message message) {
TextMessage textMessage = (TextMessage) message;
try {
System.out.println("接受消息:" + textMessage.getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
}

消费者测试类

package com.ysk.jms.consumer;

import com.ysk.jms.producer.ProducerService;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
* Created by Y.S.K on 2017/8/29 in jmsspring.
*/

public class AppConsumer {
public static void main(String[] args) {
ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("classpath:consumer.xml");
}

}

Java消息中间件之Spring中的ActiveMQ
ActiveMQ登录页面
Java消息中间件之Spring中的ActiveMQ
ActiveMQ管理页面
Java消息中间件之Spring中的ActiveMQ