本文只实现了Topic,queue改点配置就行了

一、pom依赖

Spring的太长了,具体可以看下面源码里面

            <dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-core</artifactId>
<version>5.7.</version>
</dependency>
<dependency>
<groupId>org.apache.activemq.protobuf</groupId>
<artifactId>activemq-protobuf</artifactId>
<version>1.1</version>
<type>maven-plugin</type>
</dependency>
<dependency>
<groupId>org.apache.xbean</groupId>
<artifactId>xbean-spring</artifactId>
<version>4.4</version>
</dependency>

二、目录结构

三、配置web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>ActiveMQ</display-name> <filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!--扫描配置-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:conf/spring/application-*.xml
</param-value>
</context-param> <servlet>
<servlet-name>Spring-Servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:conf/spring/springMvc-context.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Spring-Servlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping> </web-app>

四、生产者类

package com.activeMq.service;

import org.springframework.jms.core.JmsTemplate;

public class SendTopic {

    private JmsTemplate template;

    private String destination;

    private  String node;

    private String allNodeStr;

    public void sendNode(String message){

        template.convertAndSend(node,message);
} public void sendAllNodes(String message){ for(String node:allNodeStr.split(",")){ template.convertAndSend(node,message);
} } public JmsTemplate getTemplate() {
return template;
} public void setTemplate(JmsTemplate template) {
this.template = template;
} public String getDestination() {
return destination;
} public void setDestination(String destination) {
this.destination = destination;
} public String getNode() {
return node;
} public void setNode(String node) {
this.node = node;
} public String getAllNodeStr() {
return allNodeStr;
} public void setAllNodeStr(String allNodeStr) {
this.allNodeStr = allNodeStr;
} }

五、消费者类

package com.activeMq.service;

import javax.jms.Message;
import javax.jms.MessageListener; public class ReceiveMessage implements MessageListener{
private String destName; public void setDestName(String destName) {
this.destName = destName;
} public void onMessage(Message arg0) {
// TODO Auto-generated method stub
System.out.println(arg0 + " -- " + destName);
System.out.println();
} }

六、生产者配置文件 application-jms.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.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.8.0.xsd">
<!-- ActiveMQ connectionFactory -->
<amq:connectionFactory id="jmsConnectionFactory" brokerURL="${jms_server}" /> <bean id="myJmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory">
<bean class="org.springframework.jms.connection.SingleConnectionFactory">
<property name="targetConnectionFactory" ref="jmsConnectionFactory" />
</bean>
</property>
</bean> <bean id="sendTopic" class="com.activeMq.service.SendTopic">
<property name="template" ref="myJmsTemplate" />
<property name="destination" value="viki_sense" />
<property name="node" value="${jms_send_node}" />
<property name="allNodeStr" value="${jms_send_allNodeStr}" />
</bean>
</beans>

七、消费者配置文件 application-processer.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:amq="http://activemq.apache.org/schema/core"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core-5.5.0.xsd"> <!-- 监听节点 -->
<amq:queue name="viki_sense" physicalName="${jms_receive_node}" /> <!-- 消息接收 -->
<bean id="receiveMessage" class="com.activeMq.service.ReceiveMessage">
<property name="destName"><value>${jms_receive_node}</value></property>
</bean> <!-- 订阅 -->
<bean id="senseTopicMessageListener" class="org.springframework.jms.listener.adapter.MessageListenerAdapter">
<constructor-arg ref="receiveMessage" />
</bean> <!-- 消息监听 TOPIC 模式-->
<bean class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="jmsConnectionFactory" />
<property name="destination" ref="${jms_receive_node}" />
<property name="messageListener" ref="senseTopicMessageListener" />
</bean>
</beans>

八、其他配置文件

springMvc-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-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/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd"> <description>Spring-web MVC配置</description> <bean
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<bean
class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean> </list>
</property>
</bean> <mvc:annotation-driven /> <context:component-scan base-package="com.activeMq.controller">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Service" />
</context:component-scan> </beans>

application-beans.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"
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"> <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/> <context:annotation-config /> <context:component-scan base-package="com.activeMq">
<!-- 排除vst.back目录下Controller的service注入 -->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan> <bean id="configProperties"
class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath*:conf/activeMq.properties</value>
</list>
</property>
</bean> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
<property name="properties" ref="configProperties" />
</bean> </beans>

activeMq.properties

jms_server=tcp://localhost:61616
jms_send_node=viki_sense
jms_receive_node=viki_sense
jms_send_allNodeStr=x,x,x

log4j.properties

### direct log messages to stdout and logFile###
log4j.rootCategory=INFO, stdout,logFile # OpenSymphony Stuff
log4j.logger.com.opensymphony=INFO
log4j.logger.org.apache.struts2=INFO
log4j.logger.org.apache.commons=INFO # Spring Stuff
log4j.logger.org.springframework=INFO
log4j.logger.org.springframework.oxm=INFO # Hibernate Stuff
log4j.logger.org.hibernate=INFO
log4j.logger.org.hibernate.type=INFO
log4j.logger.org.hibernate.tool.hbm2ddl=INFO log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[\u65F6\u95F4\:%d{yyyy-MM-dd hh\:mm\:ss}] [\u7EA7\u522B\:%p] [\u7C7B\:%c] [\u6D88\u606F\:%m] %n log4j.appender.logFile=org.apache.log4j.RollingFileAppender
log4j.appender.logFile.File=D\:\\demo.log
log4j.appender.logFile.layout=org.apache.log4j.PatternLayout
log4j.appender.logFile.layout.ConversionPattern=[\u65F6\u95F4\:%d{yyyy-MM-dd hh\:mm\:ss}] [\u7EA7\u522B\:%p] [\u7C7B\:%c] [\u6D88\u606F\:%m] %n
log4j.appender.logFile.MaxFileSize = 5MB
log4j.appender.logFile.MaxBackupIndex =3

九、测试用到的action

package com.activeMq.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.ResponseBody; import com.activeMq.service.SendTopic; @Controller
public class ActiveMqController { @Autowired
private SendTopic sendTopic; @RequestMapping("/topicSend")
@ResponseBody
public String topicSend(String message){
String result = "";
sendTopic.sendNode("testtesttest123123123123");
result = "success";
return result;
}
}

没写页面,直接用url访问测试即可

十、源码

源码

基于Maven,Spring+ActiveMQ实现,贴近实际的更多相关文章

  1. Spring 3整合Quartz 1实现定时任务一:常规整合(基于maven构建)

    Spring配置Quartz例子(基于maven构建) 在Spring中使用Quartz有两种方式实现:第一种是任务类继承QuartzJobBean,第二种则是在配置文件里定义任务类和要执行的方法,类 ...

  2. 基于maven进行spring 和mybatis的整合(Myeclpise)

    学习日记:基于maven进行spring和mybatis的整合,进行分页查询 什么是maven:maven是一个项目管理工具,使用maven可以自动管理java项目的整个生命周期,包括编译.构建.测试 ...

  3. 基于Maven构建的Spring+Mybatis项目

    项目的目录结构: 1.基于Maven构建Web项目 参考:基于Maven构建Web项目 2.导入项目依赖 Spring 核心容器(Beans.Core.Context.Context support. ...

  4. 基于Maven的S2SH(Struts2+Spring+Hibernate)框架搭建

    1. 前言 基于Maven的开发方式开发项目已经成为主流.Maven能很好的对项目的层次及依赖关系进行管理.方便的解决大型项目中复杂的依赖关系.S2SH(Struts2+Spring+Hibernat ...

  5. 基于Maven的Spring + Spring MVC + Mybatis的环境搭建

    基于Maven的Spring + Spring MVC + Mybatis的环境搭建项目开发,先将环境先搭建起来.上次做了一个Spring + Spring MVC + Mybatis + Log4J ...

  6. Java ActiveMQ 讲解(二)Spring ActiveMQ整合+注解消息监听

    对于ActiveMQ消息的发送,原声的api操作繁琐,而且如果不进行二次封装,打开关闭会话以及各种创建操作也是够够的了.那么,Spring提供了一个很方便的去收发消息的框架,spring jms.整合 ...

  7. IntelliJ IDEA上创建maven Spring MVC项目

    IntelliJ IDEA上创建Maven Spring MVC项目 各软件版本 利用maven骨架建立一个webapp 建立相应的目录 配置Maven和SpringMVC 配置Maven的pom.x ...

  8. 基于Maven构建整合SpringMVC+Mybtis+Druid

    前几天趁空闲时间整合了下SpringMVC+Mybatis+Druid,这里小记录下,这个Demo是基于Maven构建的,数据源用的是阿里巴巴温少的开源项目Druid,数据库用的是Mysql. 由于E ...

  9. 基于Maven的Springboot+Mybatis+Druid+Swagger2+mybatis-generator框架环境搭建

    基于Maven的Springboot+Mybatis+Druid+Swagger2+mybatis-generator框架环境搭建 前言 最近做回后台开发,重新抓起以前学过的SSM(Spring+Sp ...

  10. Java系列--第一篇 Maven+Spring+Spring MVC+mybatis 示例

    基于Maven的Spring+SpringMVC+Mybatis的一个小项目的搭建,由于使用Maven3.1.0管理,所以Spring等都将使用的是时下(2013/9/8)最新的版本.即从http:/ ...

随机推荐

  1. 洛谷 P2360 地下城主

    P2360 地下城主 题目描述 你参加了一项秘密任务,在任务过程中你被困在了一个3D的地下监狱里面,任务是计时的,你现在需要在最短的时间里面从地牢里面逃出来继续你的任务.地牢由若干层组成,每一层的形状 ...

  2. 一句SQL按照某个字段数值拆分出对应的数据条数,借助数据库常量表【master..spt_values】实现

    简介:master..spt_values,数据行拆分简单小技巧 SELECT ProjGUID , CostGUID , SUM(FtAmount) AS FtAmount , BeginMonth ...

  3. 通过NFS、FTP、HTTP三种方法安装Redhat Linux (高清版)

          本节教程讲述了通过在Red Hat Linux服务器端假设NSF Server来进行Linux系统安装的过程,并详细介绍了如何制作网络启动盘的细节.演示直观,讲解通俗易懂,特别适合初学者学 ...

  4. 13.constexpr

    #include <iostream> using namespace std; //声明返回值为常量表达式 constexpr int get() { ; return num; } v ...

  5. event事件对象 兼容写法:var oEvent=ev||event;和取消事件冒泡

    要在整个页面添加事件要使用document,例如要捕抓鼠标位置 document.onclink=function(ev)  //FireFox Chrome默认都是有一个值传进来的 { var oE ...

  6. 1.1 Introduction中 Kafka as a Messaging System官网剖析(博主推荐)

    不多说,直接上干货! 一切来源于官网 http://kafka.apache.org/documentation/ Kafka as a Messaging System kafka作为一个消息系统 ...

  7. TeamViewer的下载、安装和使用(windows7、CentOS6.5和Ubuntu14.04(64bit))(图文详解)

    不多说,直接上干货! TeamViewr是远程支持.远程访问.在线协作和会议软件. 分为从windows7.CentOS6.5和Ubuntu14.04(64bit) 系统来详解下载.安装和初步使用! ...

  8. Spark SQL概念学习系列之Spark SQL基本原理

    Spark SQL基本原理 1.Spark SQL模块划分 2.Spark SQL架构--catalyst设计图 3.Spark SQL运行架构 4.Hive兼容性 1.Spark SQL模块划分 S ...

  9. Dotfuscator use for .net4.0 error solve

    在混淆的时候报错了,错误描述大致如下: Could not find a compatible version of ildasm to run on assembly C:\xxx.dll This ...

  10. Node知识总结

    一. 伪装URL-SEO 伪URL重写 把一个动态页面的地址重写为静态页面的地址,为了方便网站的SEO优化 真实地址:http://item.jd.com/detail.php?id=12261336 ...