ActiveMQ自行安装
pox.xml文件配置
<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>comgroupId>
<artifactId>ActiveMQartifactId>
<version>0.0.1-SNAPSHOTversion>
<packaging>jarpackaging>
<name>ActiveMQname>
<url>http://maven.apache.orgurl>
<properties>
<spring.version>4.2.9.RELEASEspring.version>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
properties>
<dependencies>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.11version>
<scope>testscope>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-beansartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jmsartifactId>
<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-webartifactId>
<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>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-compiler-pluginartifactId>
<configuration>
<source>1.8source>
<target>1.8target>
configuration>
plugin>
plugins>
build>
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-4.2.xsd">
<context:annotation-config>context:annotation-config>
<bean id="MQConnectionFactory" class="org.apache.activemq.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="MQConnectionFactory">property>
bean>
<bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg value="queue">constructor-arg>
bean>
<bean id="topicDestination" class="org.apache.activemq.command.ActiveMQTopic">
<constructor-arg value="topic">constructor-arg>
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-4.2.xsd">
<import resource="common.xml" />
<bean id="consumerMessageListener" class="com.jms.spring.consumer.ConsumerMessageListener">bean>
<bean id="jmsContainer"
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory">property>
<property name="destination" ref="topicDestination">property>
<property name="messageListener" ref="consumerMessageListener">property>
bean>
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-4.2.xsd">
<import resource="common.xml"/>
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory">property>
bean>
<bean id="ProducerServerImpl" class="com.jms.spring.producer.ProducerServerImpl">bean>
beans>
消费者代码实现
//ConsumerMessageListener.java
package com.jms.spring.consumer;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
public class ConsumerMessageListener implements MessageListener {
//进行 监听
@Override
public void onMessage(Message message) {
// TODO Auto-generated method stub
TextMessage textMessage =(TextMessage) message;
System.out.println(textMessage);
}
}
//AppConsumer.java 启动消费者
package com.jms.spring.consumer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class AppConsumer {
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext context = new ClassPathXmlApplicationContext("/consumer.xml");
}
}
生产者代码实现
//定义接口
package com.jms.spring.producer;
public interface ProducerServer {
void sendMessage(String message);
}
//实现接口
package com.jms.spring.producer;
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 ProducerServerImpl implements ProducerServer {
@Autowired
JmsTemplate jmsTemplate;
//使用 队列模式
//@Resource(name = "queueDestination")
//使用 主题模式
@Resource(name="topicDestination")
Destination destination;
@Override
public void sendMessage(final String message) {
// TODO Auto-generated method stub
//jmsTemplate 发送消息
jmsTemplate.send(destination, new MessageCreator() {
//创建消息
@Override
public Message createMessage(Session session) throws JMSException {
// TODO Auto-generated method stub
TextMessage textMessage = session.createTextMessage(message);
return textMessage;
}
});
System.out.println("生产 消息:" + message);
}
}
//启动生产者
package com.jms.spring.producer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
//import org.springframework.context.Applic
public class AppProducer {
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext context=new ClassPathXmlApplicationContext("/producer.xml");
ProducerServer server=context.getBean(ProducerServer.class);
for (int i = 0; i < 50; i++) {
server.sendMessage("test:"+i);
}
}
}