springmvc和activemq的整合使用
1、简介:ActiveMQ 是Apache出品,最流行的,能力强劲的开源消息总线。ActiveMQ 是一个完全支持JMS1.1和J2EE 1.4规范的 JMS Provider实现,尽管JMS规范出台已经是很久的事情了,但是JMS在当今的J2EE应用中间仍然扮演着特殊的地位。
2、建议在使用以前了解一下jms的一些知识
3、第一步:我们部署相关的activemq(我这里是采用自己本地linux虚拟机来实现的,以模拟中间推送消息的原理)
activemq下载地址:http://archive.apache.org/dist/activemq/我用的是目前最新的5.14.5版本
这里有这个zip的是windows用的,tar.gz的是Linux用的。我这里采用的Linux部署
将activemq的tar解压启动
启动方式在:
/root/apache-activemq-5.14./bin/linux-x86-
启动:./activemq start 暂停: ./activemq stop 重启:./activemq restart
启动起来后,访问地址为:http://192.168.5.10:8161/admin 账号密码:都是admin(默认)
开机自启:vi /etc/rc.local 然后在末尾加入
su - root -c '/usr/local/activemq/bin/activemq start'
这样整个activemq就部署好了,因为activemq是单独的项目,启动过后不用再理会
4、第二步:Java代码实现,具体项目的配置
1)导包: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.troy</groupId>
<artifactId>activemq</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-spring</artifactId>
<version>5.14.5</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>3.2.8.RELEASE</version>
</dependency>
</dependencies>
</project>
2)根据相关jar的属性进行xml的配置
(1)配置activemq的连接spring-config.properties
activemq_url=tcp://192.168.5.10:61616
activemq_username=admin
activemq_password=admin
(2)配置相关的activeMQ
<?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:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
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-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-4.0.xsd
http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core-5.14.5.xsd
"> <context:annotation-config/>
<context:component-scan base-package="com.troy"/> <!-- 读取配置文件 -->
<bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<array>
<value>classpath:conf/spring-config.properties</value>
</array>
</property>
</bean> <!-- 连接 activemq-->
<amq:connectionFactory id="amqConnectionFactory" brokerURL="${activemq_url}" userName="${activemq_username}" password="${activemq_password}"/> <!-- 这里可以采用连接池的方式连接PooledConnectionFactoryBean -->
<bean id="connectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
<!-- 配置连接 -->
<property name="targetConnectionFactory" ref="amqConnectionFactory"/>
<!-- 会话的最大连接数 -->
<property name="sessionCacheSize" value="100"/>
</bean> <!-- 定义消息队列topic类型,queue的方式差不多 -->
<bean id="topic" class="org.apache.activemq.command.ActiveMQTopic">
<!-- 定义名称 -->
<constructor-arg index="0" value="topic"/>
</bean> <!-- 配置JMS模板(topic),Spring提供的JMS工具类,它发送、接收消息。 -->
<!-- 为了测试发送消息,保留jmsTemplate的配置,实际不存在发送,只需要配置监听即可 -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory"/>
<property name="defaultDestination" ref="topic"/>
<!-- 非pub/sub模型(发布/订阅),true为topic,false为queue -->
<property name="pubSubDomain" value="true"/>
</bean> <!-- 监听方式,这种方式更实用,可以一直监听消息 -->
<bean id="topicMessageListen" class="com.troy.activemq.TopicMessageListen"/>
<bean id="defaultMessageListenerContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory"/>
<!-- 注册activemq名称 -->
<property name="destination" ref="topic"/>
<property name="messageListener" ref="topicMessageListen"/>
</bean> </beans>
3)代码层面我谢了两个:方便测试
(1)一个是监听接口获取数据(TopicMessageListen)
public class TopicMessageListen implements MessageListener{ public void onMessage(Message message) {
System.out.println("监听==================监听");
try {
System.out.println(message);
TextMessage tm = (TextMessage)(message);
System.out.println(tm.getText());
} catch (Exception e) {
e.printStackTrace(); } } }
因为我们在配置文件里面加入了监听,这里只需要实现MessageListener接口就可以了,然后在处理message信息
(2)发送消息(TopicSendMessage)
public class TopicSendMessage {
private ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:spring/spring-mvc.xml");
private JmsTemplate jmsTemplate = (JmsTemplate) ac.getBean("jmsTemplate");
public void send(){ jmsTemplate.send(new MessageCreator() { public Message createMessage(Session session) throws JMSException {
TextMessage msg = session.createTextMessage();
msg.setText("发送数据++++++++++++发送数据");
System.out.println("发送数据++++++++++++发送数据");
return msg;
}
});
} public void receive(){
Message msg = jmsTemplate.receive();
TextMessage tm = (TextMessage)msg;
System.out.println("非监听------------------非监听");
System.out.println(msg);
} public static void main(String[] args) {
new TopicSendMessage().send(); }
}
说明:发送数据的方式基本上大同小异通过获取jmsTemplate来实现发送的操作,因为我没有直接启动容器,所以采用获取bean的方式
接收上面没有运行,因为接收的这个receive()方法是同步运行的,会卡线程这里不做演示,我通过启动两个方式测试可以成功的,但是不建议这种方式
(3)展示结果:
(4)activemq的简单使用和配置方式就才不多这么多
5、我的项目结构
springmvc和activemq的整合使用的更多相关文章
- spring-mvc 集成 activeMq 常见问题 + 解决方案 (仅供参考)
最近整合 spring-mvc 和 activeMq ,出现了几个异常,我把他记录下来,具体的原理分析我就不太会写了,只把详细情况和解决方案给出来,希望对各位老铁有所帮助! 问题1:缺少log4j的配 ...
- 1.springMVC+spring+Mybatis的整合思路
SSM整合的过程:就是把一些东西交给spring管理,也就是添加配置文件的一个过程.那么有哪些东西我们要交给spring管理呢?大概有以下几个: 1.数据源(可配置数据库连接池) 2.SqlSessi ...
- SpringMVC中使用Swagger2整合
Swagger2是什么 Swagger 是一款RESTFUL接口的文档在线自动生成+功能测试功能软件. Swagger 是一个规范和完整的框架,用于生成.描述.调用和可视化 RESTful 风格的 W ...
- Web项目容器集成ActiveMQ & SpringBoot整合ActiveMQ
集成tomcat就是随项目启动而启动tomcat,最简单的方法就是监听器监听容器创建之后以Broker的方式启动ActiveMQ. 1.web项目中Broker启动的方式进行集成 在这里采用Liste ...
- SSM框架整合的详细过程(含每一步的分析及代码)。实质上是SpringMVC与mybatis的整合,应为spring与SpringMVC不需要整合。
为了更好的学习 springmvc和mybatis整合开发的方法,需要将springmvc和mybatis进行整合. 整合目标:控制层采用springmvc.持久层使用mybatis实现. 1.1 需 ...
- Spring+SpringMVC+MyBatis+Maven框架整合
本文记录了Spring+SpringMVC+MyBatis+Maven框架整合的记录,主要记录以下几点 一.Maven需要引入的jar包 二.Spring与SpringMVC的配置分离 三.Sprin ...
- SpringMVC:学习笔记(10)——整合Ckeditor且实现图片上传
SpringMVC:学习笔记(10)——整合Ckeditor且实现图片上传 配置CKEDITOR 精简文件 解压之后可以看到ckeditor/lang下面有很多语言的js,如果不需要那么多种语言的,可 ...
- JavaWeb_(SpringMVC框架)测试SpringMVC&Spring&MyBatis三大整合
搭建 SpringMVC&Spring&MyBatis三大整合 传送门 1.准备 测试搭建S pringMVC&Spring&MyBatis三大整合 用例 a)准备 ...
- idea spring+springmvc+mybatis环境配置整合详解
idea spring+springmvc+mybatis环境配置整合详解 1.配置整合前所需准备的环境: 1.1:jdk1.8 1.2:idea2017.1.5 1.3:Maven 3.5.2 2. ...
随机推荐
- ide 下spingboot 实现热部署
需要从maven中下载devtools插件pom.xml:step1:修改pom.xml<dependencies><dependency><groupId>org ...
- Pavilion M4-1016TX 加装固态硬盘(SSD)+UEFI+GPT安装WIN8.1
折腾了一天,终于将电脑加上SSD和装上系统,记录下,方便后面忘记使用. 步骤: 1.Pavilion M4-1016TX内置了mSata的接口,大小是全高的.ssd支持大小官方说法是测试过32g的,目 ...
- 十图详解TensorFlow数据读取机制(附代码)
在学习TensorFlow的过程中,有很多小伙伴反映读取数据这一块很难理解.确实这一块官方的教程比较简略,网上也找不到什么合适的学习材料.今天这篇文章就以图片的形式,用最简单的语言,为大家详细解释一下 ...
- Android 框架学习2:源码分析 EventBus 3.0 如何实现事件总线
Go beyond yourself rather than beyond others. 上篇文章 深入理解 EventBus 3.0 之使用篇 我们了解了 EventBus 的特性以及如何使用,这 ...
- MPAndroidChart Wiki(译文)~Part 6
22. ViewPortHandler ViewPortHandler负责处理图表的视窗.也就是说它负责图表视图中的展示给用户的那部分内容.包括图表位移,缩放级别,图表大小和绘制区域以及当前偏移量.V ...
- Java打飞机小游戏(附完整源码)
写在前面 技术源于分享,所以今天抽空把自己之前用java做过的小游戏整理贴出来给大家参考学习.java确实不适合写桌面应用,这里只是通过这个游戏让大家理解oop面向对象编程的过程,纯属娱乐.代码写的很 ...
- querySelector.. 方法相比 getElementsBy..
querySelectorAll 返回的是一个 Static Node List,而 getElementsBy 系列的返回的是一个 Live Node List. 看看下面这个经典的例子 [5]: ...
- Codeforces 15E Triangles 【组合计数】
Codeforces 15E Triangles Last summer Peter was at his granny's in the country, when a wolf attacked ...
- NullReferenceException,就不应该存在!
如果要你说出 .NET 中的三个异常,NullReferenceException 一定会成为其中一个:如果说出 .NET 中的一个异常,NullReferenceException 也会被大多数人说 ...
- 如何组织一个同时面向 UWP/WPF/.Net Core 控制台的 C# 项目解决方案
希望写一个小型工具,给自己和需要的人.考虑到代码尽可能的复用,我准备采用 .Net Standard 来编写大多数核心代码,并基于 .Net Core 编写跨平台控制台入口,用 WPF 编写桌面端 U ...