1、发送消息类 HelloSender
package com.test.mq.springmq;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
/**
* Hello world!
*send message
*/
public class HelloSender
{
public static void main( String[] args )
{
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("application_mq.xml");
JmsTemplate jmsTemplate = (JmsTemplate) applicationContext.getBean("jmsTemplate");
Destination destination = (Destination)applicationContext.getBean("destination");
jmsTemplate.send(destination,new MessageCreator() {
@Override
public Message createMessage(Session session) throws JMSException {
return session.createTextMessage("send success message : message2! ");
}
});
}
}
2、接收消息类ProxyJMSConsumer
package com.test.mq.springmq;
import javax.jms.Destination;
import javax.jms.TextMessage;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;
/**
* Hello world!
*send message
*/
public class ProxyJMSConsumer
{
private JmsTemplate jmsTemplate;
public ProxyJMSConsumer() {
}
public JmsTemplate getJmsTemplate() {
return jmsTemplate;
}
public void setJmsTemplate(JmsTemplate jmsTemplate) {
this.jmsTemplate = jmsTemplate;
}
public void recive(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("application_mq.xml");
Destination destination = (Destination) applicationContext.getBean("destination");
while(true){
try{
TextMessage txtmsg = (TextMessage) jmsTemplate.receive(destination);
if(null != txtmsg){
System.out.println("[DB Proxy] " + txtmsg);
System.out.println("[DB Proxy] 收到消息内容为: "
+ txtmsg.getText());
}else{
break;
}
}catch(Exception e){
}
}
}
}
3、测试类,让接收消息类一直处于接收状态
package com.test.mq.springmq;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class JMSTest
{
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("application_mq.xml");
ProxyJMSConsumer proxyJMSConsumer = (ProxyJMSConsumer) applicationContext.getBean("messageReceiver");
proxyJMSConsumer.recive();
System.out.println("初始化消费者信息");
}
}
4、application_mq.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"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd">
<bean id="jmsFactory" class="org.apache.activemq.pool.PooledConnectionFactory"
destroy-method="stop">
<property name="connectionFactory">
<bean class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL">
<value>tcp://127.0.0.1:61616</value>
</property>
</bean>
</property>
<property name="maxConnections" value="100"></property>
</bean>
<!-- Spring JMS Template -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory">
<ref local="jmsFactory" />
</property>
<property name="defaultDestinationName" value="subject" /> <!-- 发送的目的地址 -->
<!-- 区别它采用的模式为false是p2p为true是订阅 -->
<property name="pubSubDomain" value="false" />
</bean>
<bean id="destination" class="org.apache.activemq.command.ActiveMQTopic">
<!-- 设置消息队列的名字 -->
<constructor-arg index="0" value="subject" />
</bean>
<!-- 配置接收消息的类 -->
<bean id="messageReceiver" class="com.test.mq.springmq.ProxyJMSConsumer">
<property name="jmsTemplate" ref="jmsTemplate"></property>
</bean>
</beans>
5、maven 配置(pom.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.0</modelVersion>
<groupId>com.test.mq</groupId>
<artifactId>springmq</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springmq</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>3.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>org.springframework.jms</artifactId>
<version>3.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.jms</groupId>
<artifactId>javax.jms-api</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-core</artifactId>
<version>5.7.0</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-pool</artifactId>
<version>5.10.0</version>
</dependency>
</dependencies>
</project>
6、install activemq :在 官网上面下载activemq-****.zip,
直接运行D:\soft\activeMq\apache-activemq-5.10.0\bin\win64>activemq.bat
看到 ActiveMQ WebConsole available at http://0.0.0.0:8161/ 说明运行OK
访问 http://localhost:8161/admin/ 可以看到activemq页面
如果需要输入用户名密码
进入目录D:\soft\activeMq\apache-activemq-5.10.0\conf
打开jetty.xml
将鉴权标签的value修改成FALSE <property name="authenticate" value="false" />
重启activedmq
7、收发消息: run as application----> JMSTest.java ,
run as application----> HelloSender.java ,
看到控制台消息 说明 OK.......end
----------------------------------------------------------------------------------------------------
另一种方式不需要application_mq.xml的配置文件
MQConfigLoader.java 获取 jmsTemplate
package com.test.mq.springmq;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.jms.connection.SingleConnectionFactory;
import org.springframework.jms.core.JmsTemplate;
public class MQConfigLoader {
private static final MQConfigLoader instance = new MQConfigLoader();
private ActiveMQConnectionFactory activeMQConnectionFactory;
private ActiveMQQueue activeMQQueue;
private SingleConnectionFactory singleConnectionFactory;
private JmsTemplate jmsTemplate;
protected MQConfigLoader() {
this.activeMQConnectionFactory = new ActiveMQConnectionFactory();
this.activeMQQueue = new ActiveMQQueue("test.send");
this.jmsTemplate = new JmsTemplate();
}
public static MQConfigLoader getInstance() {
return instance;
}
public JmsTemplate load() {
this.activeMQConnectionFactory.setBrokerURL("tcp://127.0.0.1:61616");
this.activeMQQueue.setPhysicalName("test.send");
this.singleConnectionFactory = new SingleConnectionFactory(this.activeMQConnectionFactory);
this.jmsTemplate.setConnectionFactory(this.singleConnectionFactory);
this.jmsTemplate.setDefaultDestination(this.activeMQQueue);
return this.jmsTemplate;
}
public JmsTemplate getJmsTemplate() {
return jmsTemplate;
}
}
TestSender.java
package com.test.mq.springmq;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
public class TestSender {
private static JmsTemplate jmsTemplate = MQConfigLoader.getInstance().load();
public static void sendMessage(){
jmsTemplate.send(new MessageCreator() {
@Override
public Message createMessage(Session session) throws JMSException {
return session.createTextMessage("test sender..............................");
}
});
}
public static void main(String[] args) {
sendMessage();
}
}
TestReceive
package com.test.mq.springmq;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.TextMessage;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.jms.core.JmsTemplate;
/**
* Hello world!
*send message
*/
public class TestRecever
{
private static JmsTemplate jmsTemplate = MQConfigLoader.getInstance().load();
static Destination destination = new ActiveMQQueue("test.send");
public static void receiveMessage() throws JMSException{
while(true){
TextMessage txtmsg = (TextMessage) jmsTemplate.receive(destination);
if(txtmsg != null){
System.out.println(txtmsg.getText());
}else{
break;
}
}
}
public static void main(String[] args) throws JMSException {
receiveMessage();
}
}