首先,我们创建一个maven项目。我们要整合spring肯定要导入相关依赖,这里我们贴出这个项目完整的依赖:
<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.0modelVersion>
<groupId>cn.shinelon.ActiveMQgroupId>
<artifactId>JMS-SpringartifactId>
<version>0.0.1-SNAPSHOTversion>
<packaging>jarpackaging>
<name>JMS-Springname>
<url>http://maven.apache.orgurl>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<spring.version>4.3.12.RELEASEspring.version>
properties>
<dependencies>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>3.8.1version>
<scope>testscope>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-coreartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-testartifactId>
<version>${spring.version}version>
<scope>testscope>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jmsartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.apache.activemqgroupId>
<artifactId>activemq-coreartifactId>
<version>5.7.0version>
<exclusions>
<exclusion>
<artifactId>spring-contextartifactId>
<groupId>org.springframeworkgroupId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>javax.annotationgroupId>
<artifactId>jsr250-apiartifactId>
<version>1.0version>
dependency>
dependencies>
project>
接下来,我们将创建一个common.xml配置文件来配置我们连接ActiveMQ的连接信息,这样避免我们重复编写代码:
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-4.3.xsd">
<context:annotation-config/>
<bean id="targetConnectionFactory" class="org.apache.activemq.spring.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://127.0.0.1:61616"/>
bean>
<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>
这里 ,我们来创建一个ProducerServer接口
public interface ProductorServer {
void SendMessage(String message);
}
接下来是它的一个实现类
import javax.annotation.Resource;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
public class ProductorServerImpl implements ProductorServer{
@Autowired
JmsTemplate jmsTemplate;
//队列模式
// @Resource(name="queueDestination") //因为我们可能在spring容器中注入多个目的地,所以这里使用@Resource注解
//主题模式,消费者和订阅者
@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);
}
}
接着我们来编写配置文件: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-4.3.xsd">
<import resource="common.xml"/>
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory"/>
bean>
<bean class="cn.shinelon.jms.ProductorServerImpl"/>
beans>
现在我们可以测试我们的生产者了:
public class AppProducer {
public static void main(String[] args) {
ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("productor.xml");
ProductorServer server=context.getBean(ProductorServer.class);
for(int i=0;i<100;i++) {
server.SendMessage("test"+i);
}
context.close();
}
}
运行上面的代码,我们可以在ActiveMQ服务器上看到100消息,这是没哟消费者,我们下面来创建消费者:
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
public class JmsMessageListener implements MessageListener{
public void onMessage(Message message) {
TextMessage textMessage=(TextMessage) message;
try {
System.out.println(textMessage.getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
}
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-4.3.xsd">
<import resource="common.xml"/>
<bean id="JmsMessageListener" class="cn.shinelon.MessageListener.JmsMessageListener"/>
<bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory"/>
<property name="destination" ref="topicDestination"/>
<property name="messageListener" ref="JmsMessageListener"/>
bean>
beans>
下面我们来加载我们的消费者并且启动就会返现100条消息被全部消费
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class AppConsumer {
public static void main(String[] args) {
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("consumer.xml");
}
}
至此,Spring和ActiveMQ整合完毕。