消息服务:一个中间件,用于解决两个活多个程序之间的耦合,底层由Java 实现。
优势:异步、可靠
消息模型:点对点,
JMS中的对象
需要安装apache-activemq,下载安装之后到暗转目录下的bin,找到activemq.bat双击运行。浏览器:http://localhost:8161/admin/ 用户名和密码都为admin,保持运行状态
由于我自己的项目是搭建好的,所以直接有了springmvc+spring+maven所以我要做的就是把ActiveMq添加上去;
第一步jar:
完整的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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>activemq</groupId>
<artifactId>com.activemq</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>activemq</name>
<url>http://maven.apache.org</url>
<properties>
<springframework>4.2.6.RELEASE</springframework>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${springframework}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${springframework}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${springframework}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${springframework}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>${springframework}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.xbean</groupId>
<artifactId>xbean-spring</artifactId>
<version>4.2</version>
</dependency>
<!-- activemq -->
<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.12.1</version>
</dependency>
<!--alibabajson转换jar -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.9</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.1.34</version>
</dependency>
<!-- 所有的Java日志实现提供一个统一的接口jar -->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-web</artifactId>
<version>2.0.1</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-1.2-api</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.5.4</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>commons-pool</groupId>
<artifactId>commons-pool</artifactId>
<version>1.6</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.18</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.7</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>3.0-alpha-1</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<warSourceDirectory>Web</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
<finalName>com.activemq</finalName>
</build>
</project>
因为有很多别的东西所以这里看起来东西比较多。
第二步:ActiveMq配置,
applicationContext-ActiveMQ.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:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p"
xmlns:amq="http://activemq.apache.org/schema/core" xmlns:jms="http://www.springframework.org/schema/jms"
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.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/jms
http://www.springframework.org/schema/jms/spring-jms-4.1.xsd
http://activemq.apache.org/schema/core
http://activemq.apache.org/schema/core/activemq-core-5.7.0.xsd ">
<mvc:annotation-driven />
<!-- 对包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 需要更改 -->
<context:component-scan base-package="com.java.spring" />
<!-- activemq连接池 -->
<amq:connectionFactory id="amqConnectionFactory" brokerURL="tcp://localhost:61616" userName="admin" password="admin" />
<!-- 配置JMS连接工厂 -->
<bean id="connectionFactory"
class="org.springframework.jms.connection.CachingConnectionFactory">
<constructor-arg ref="amqConnectionFactory" />
<property name="sessionCacheSize" value="100" />
</bean>
<!-- 定义消息队列(Queue) -->
<bean id="demoQueueDestination" class="org.apache.activemq.command.ActiveMQQueue">
<!-- 设置消息队列的名字 -->
<constructor-arg>
<value>Jaycekon</value>
</constructor-arg>
</bean>
<!-- 配置JMS模板(Queue),Spring提供的JMS工具类,它发送、接收消息。 -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory" />
<property name="defaultDestination" ref="demoQueueDestination" />
<property name="receiveTimeout" value="10000" />
<!-- true是topic,false是queue,默认是false,此处显示写出false -->
<property name="pubSubDomain" value="false" />
</bean>
<!-- 配置消息队列监听者(Queue) -->
<bean id="queueMessageListener" class="com.java.spring.util.listener.QueueMessageListener" />
<!-- 显示注入消息监听容器(Queue),配置连接工厂,监听的目标是demoQueueDestination,监听器是上面定义的监听器 -->
<bean id="queueListenerContainer"
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory" />
<property name="destination" ref="demoQueueDestination" />
<property name="messageListener" ref="queueMessageListener" />
</bean>
</beans>
红色部分为重点,主要为加载ActiveMq所用到标签库,一定要注意和pom.xml中版本相同
这个部分代码可以先注释(这是消息监听,初学者第一步一般都是手动获取消息所以用不到监听获取)
第三步:创建生产者(Producer)和消费者(Consumer);这里我把生产者和消费都放在了一个接口实现类中
接口分别为:
package com.java.spring.service;
import javax.jms.Destination;
import org.springframework.stereotype.Service;
/**
* @author 作者:zhaofq
* @version 创建时间:2017年3月28日 上午10:19:15
* 类说明
*/
@Service
public interface ProducerService {
public void sendMessage(Destination destination,final String msg);
public void sendMessage(final String msg);
}
package com.java.spring.service;
import javax.jms.Destination;
import javax.jms.TextMessage;
/**
* @author 作者:zhaofq
* @version 创建时间:2017年3月28日 上午10:29:24
* 类说明
*/
public interface ConsumerService {
public TextMessage receive();
}
package com.java.spring.serviceImpl;
import javax.annotation.Resource;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import org.apache.log4j.Logger;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Service;
import com.java.spring.service.ProducerService;
/**
* @author 作者:zhaofq
* @version 创建时间:2017年3月28日 上午10:19:15
* 类说明
*/
@Service
public class ProducerServiceImpl implements ProducerService{
private static Logger loger = Logger.getLogger(ProducerServiceImpl.class.getName());
//指定消息目的地的发送方法
@Resource(name="jmsTemplate")
private JmsTemplate jmsTemplate;
public void sendMessage(Destination destination,final String msg){
System.out.println(Thread.currentThread().getName()+" 向队列"+destination.toString()+"发送消息---------------------->"+msg);
loger.info(Thread.currentThread().getName()+" 向队列"+destination.toString()+"发送消息---------------------->"+msg);
jmsTemplate.send(destination,new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
return session.createTextMessage(msg);
}
});
}
//默认消息目的地的方法
public void sendMessage(final String msg){
Destination destination = jmsTemplate.getDefaultDestination();
System.out.println(Thread.currentThread().getName()+" 向队列"+destination+"发送消息---------------------->"+msg);
loger.info(Thread.currentThread().getName()+" 向队列"+destination+"发送消息---------------------->"+msg);
jmsTemplate.send(new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
return session.createTextMessage(msg);
}
});
}
}
package com.java.spring.serviceImpl;
import javax.annotation.Resource;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.TextMessage;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Service;
import com.java.spring.service.ConsumerService;
/**
* @author 作者:zhaofq
* @version 创建时间:2017年3月28日 上午10:29:24
* 类说明
*/
@Service
public class ConsumerServiceImpl implements ConsumerService{
//消息目的地
@Resource(name = "demoQueueDestination")
Destination destination;
@Resource(name="jmsTemplate")
JmsTemplate jmsTemplate;
public TextMessage receive(){
TextMessage textMessage = (TextMessage) jmsTemplate.receive(destination);
try{
System.out.println("从队列" + destination+ "收到了消息:\t"
+ textMessage.getText().toString());
} catch (JMSException e) {
e.printStackTrace();
}
return textMessage;
}
}
由于我的项目已经是从浏览器访问的,ssm项目,所以我直接用controller来调用接口方法实现消息发送和接收controller的内容为
package com.java.spring.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.java.spring.service.ConsumerService;
import com.java.spring.service.ProducerService;
/**
* @author 作者:zhaofq
* @version 创建时间:2017年3月28日 上午10:51:48
* 类说明
*/
@Controller
public class MessageController {
@Autowired(required=false)
ProducerService producerService;
@Autowired(required=false)
ConsumerService consumerService;
@RequestMapping(value="/sendMessage",method=RequestMethod.GET)
public @ResponseBody void send(String msg) {
msg="helloActiveMq";
producerService.sendMessage(msg);
}
@RequestMapping(value="/receiveMessage",method=RequestMethod.GET)
public @ResponseBody void receive() {
consumerService.receive();
}
}
启动tomcat测试没问题:分别访问/sendMessage和/receiveMessage
打印结果是:
发送打印:
接收打印:
记得要是没有注释监听那段代码的时候手动访问是接收不到的,所以监听和手动不能同时进行,因为监听只要消费了就在队列中么有了.
下面看下通过监听收到的消息:监听需要自己实现监听类MessageListener
package com.java.spring.util.listener;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import org.springframework.jms.listener.adapter.MessageListenerAdapter;
/**
* @author 作者:zhaofq
* @version 创建时间:2017年3月28日 下午1:35:43
* 类说明
*/
public class QueueMessageListener implements MessageListener {
public void onMessage(Message message) {
TextMessage tm = (TextMessage) message;
try {
System.out.println("QueueMessageListener监听到了文本消息:\t"+ tm.getText());
//do something ...
} catch (JMSException e) {
e.printStackTrace();
}
}
}
可以在监听消息收到之后做一些业务实现,测试方法,把applicationContext-ActiveMQ.xml中注释的监听部分配置打开运行项目,访问发送消息方法:
代码可以github下载:
https://github.com/zhaofq/activemq.git
下一篇说发布/订阅模式