其实无论在win下还是在linux下,都可以运行得很爽

下载安装包地址:

http://www.apache.org/dyn/closer.cgi?path=/activemq/5.12.1/apache-activemq-5.12.1-bin.tar.gz

安装

1)解压文件

tar zxvf apache-activemq-5.12.1-bin.tar.gz

2)改个文件名字

mv apache-activemq-5.12.1 ../system/activemq

3)配置用户密码,比如我在user.properties中添加了信息

admin=admin
chenweixian=chenweixian

4)启动,根据你自己当前部署的系统环境选择启动,如果是win下的,就根据自己系统的位数,选择32或64.如果是linux就直接启动activemq就可以了

执行命令:*****为具体路径。。

******/activemq/bin/activemq start

执行命令前请去确认是否已经授权,如果没有授权,到当前bin目录下,执行:

chmod 777 *

java工程中的配置

5)启动完成后

测试访问:

http://ip:8161/admin/

正常如下图:

6)maven工程中添加引入jar包,不是maven工程,自己找以下两个jar包加入工程中。

<!-- 使用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-spring</artifactId>
<version>5.7.0</version>
</dependency>

7)在web.xml中添加引入:

    <!-- ContextLoaderListener初始化Spring上下文时需要使用到的contextConfigLocation参数 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<!-- 配置spring.xml和spring-mybatis.xml这两个配置文件的位置,固定写法 -->
<param-value>
classpath:spring.xml,
classpath:spring-mybatis.xml,
classpath:spring-activitymq.xml,
classpath:dubbo.xml
</param-value>
</context-param>

8)spring-activitymq.xml文件配置

位置:

内容:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-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/jee http://www.springframework.org/schema/jee/spring-jee-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/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"> <!--创建连接工厂-->
<bean id="targetConnectionFactory"
class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://192.168.10.102:61616"></property>
</bean>
<!-- 声明ActiveMQ消息目标,目标可以是一个队列,也可以是一个主题ActiveMQTopic,消息队列名称
<bean id="destination" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg index="0" value="myQueue"></constructor-arg>
</bean>-->
<!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory -->
<bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
<!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory -->
<property name="targetConnectionFactory" ref="targetConnectionFactory"/>
</bean>
<!--模板消息-->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory" />
<property name="receiveTimeout" value="600"></property>
</bean> <!-- 队列A start-->
<!--这个是队列目的地-->
<bean id="demoQueueA" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg><value>demoQueueA</value></constructor-arg>
</bean>
<!-- 消息监听容器 -->
<bean id="demoMessageListenerA"
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory" />
<property name="destination" ref="demoQueueA" />
<property name="messageListener" ref="demoMessageListener" />
</bean>
<!-- 队列A end-->
<!-- 队列B start--> <!-- 队列A start-->
<!--这个是队列目的地-->
<bean id="demoQueueB" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg><value>demoQueueB</value></constructor-arg>
</bean>
<!-- 消息监听容器 -->
<bean id="demoMessageListenerB"
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory" />
<property name="destination" ref="demoQueueB" />
<property name="messageListener" ref="demoMessageListener2" />
</bean>
<!-- 队列A end-->
<!-- 队列B end-->
</beans>

9)java监听器

DemoMessageListener.java

package com.iafclub.demo.activityMq;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage; import org.apache.log4j.Logger;
import org.springframework.stereotype.Component; /**接收mq消息
*
* @author chenweixian
*
*/
@Component
public class DemoMessageListener implements MessageListener {
private static final Logger logger = Logger.getLogger(DemoMessageListener.class); public void onMessage(Message message) {
String messageStr = "DemoMessageListener接收消息";
//这里我们知道生产者发送的就是一个纯文本消息,所以这里可以直接进行强制转换
TextMessage textMsg = (TextMessage) message;
logger.info(messageStr + "..........start......");
try { logger.info("消息内容是:" + textMsg.getText());
} catch (JMSException e) {
logger.error(messageStr, e);
}
logger.info(messageStr + "..........end.");
}
}

DemoMessageListener2.java

package com.iafclub.demo.activityMq;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage; import org.apache.log4j.Logger;
import org.springframework.stereotype.Component; import com.iafclub.baseTools.contants.ControllerContants; /**接收mq消息
*
* @author chenweixian
*
*/
@Component
public class DemoMessageListener2 implements MessageListener {
private static final Logger logger = Logger.getLogger(DemoMessageListener2.class); public void onMessage(Message message) {
String messageStr = "DemoMessageListener2接收消息";
//这里我们知道生产者发送的就是一个纯文本消息,所以这里可以直接进行强制转换
TextMessage textMsg = (TextMessage) message;
logger.info(messageStr + ControllerContants.MESSAGE_START);
try { logger.info("消息内容是:" + textMsg.getText());
} catch (JMSException e) {
logger.error(messageStr, e);
}
logger.info(messageStr + ControllerContants.MESSAGE_START);
}
}

10)使用junit测试:

package test.iafclub.mq;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID; import net.sf.json.JSONArray;
import net.sf.json.JSONObject; import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.iafclub.baseTools.mq.ActiveMqUtil;
import com.iafclub.demo.domain.Dictionary; @RunWith(SpringJUnit4ClassRunner.class)
//配置了@ContextConfiguration注解并使用该注解的locations属性指明spring和配置文件之后,
@ContextConfiguration(locations = {"classpath:spring.xml","classpath:spring-activitymq-test.xml", "classpath:spring-mybatis.xml", "classpath:dubbo-test.xml" })
public class ActiveMqTest {
@Autowired
private ActiveMqUtil activeMqUtil;
String queueName = "chenweixianQueue"; @Test
public void testSenderMq(){
for (int i=0;i<10;i++){
Dictionary dictionary = new Dictionary();
dictionary.setId(UUID.randomUUID().toString());
dictionary.setTypeId("002");
dictionary.setTypeName("字典分类");
dictionary.setFieldKey("username"+i);
dictionary.setFieldValue("陈惟鲜");
dictionary.setFieldBack("back1");
dictionary.setFieldBack2("back2");
dictionary.setFieldBack3("back3");
dictionary.setRemark("备注"+i);
String messageContent = JSONObject.fromObject(dictionary).toString();
System.out.println("发送消息:" + messageContent);
activeMqUtil.sendMq("chenweixianQueue", messageContent);
} List<Dictionary> dictionarys = new ArrayList<Dictionary>();
for (int i=0;i<10;i++){
Dictionary dictionary = new Dictionary();
dictionary.setId(UUID.randomUUID().toString());
dictionary.setTypeId("002");
dictionary.setTypeName("字典分类");
dictionary.setFieldKey("username"+i);
dictionary.setFieldValue("陈惟鲜");
dictionary.setFieldBack("back1");
dictionary.setFieldBack2("back2");
dictionary.setFieldBack3("back3");
dictionary.setRemark("备注"+i);
dictionarys.add(dictionary);
}
String messageContent = JSONArray.fromObject(dictionarys).toString();
System.out.println("发送消息:" + messageContent);
activeMqUtil.sendMq("chenweixianQueue2", messageContent); System.out.println("发送完成");
} // @Test
// public void testReceiverMq(){
// String result = activeMqUtil.receiveMq(queueName);
// System.out.println(result);
// System.out.println("接收完成");
// }
// @Test
// public void testParam(){
// System.out.println(System.getProperty("webAppRootKey"));
// }
}

因为使用简单方便,都是用json进行数据传输,所以,封装了一个工具类ActiveMqUtil.java

package com.iafclub.baseTools.mq;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage; import org.apache.activemq.command.ActiveMQTextMessage;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Component; @Component
public class ActiveMqUtil { private Logger logger = Logger.getLogger(ActiveMqUtil.class); @Autowired
private JmsTemplate jmsTemplate; /**发送消息
*
* @param messageString
*/
public void sendMq(String destinationName, final String messageString) { jmsTemplate.send(destinationName, new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
// 消息内容
TextMessage message = session.createTextMessage(messageString);
return message;
}
});
} /**接收消息系统启动的时候
*
* @param messageString
*/
public String receiveMq(String destinationName) {
String result = "";
ActiveMQTextMessage message = (ActiveMQTextMessage)jmsTemplate.receive(destinationName);
try {
result = message.getText();
} catch (JMSException e) {
logger.error(e);
}
return result;
}
}

运行junit:

进入mq管理界面:可以看到我们刚刚发送的消息队列中有的消息个数,消费次数。。。

进入一个消息中,能看到消息具体内容,因为我们发送过来的是json格式的信息,所以,在这个服务器上能看到的内容也是json格式的内容。

11)现在消息在服务器上,因为我们刚刚已经配置了与之关联的spring-activemq.xml中指定了消息名字,所以。当我们的web服务启动后,就自动订阅这个服务器上的消息。

当然也可以自己手动去触发,刚刚的测试例子中也有手动触发的个例。

12)完毕。。。

activemq的配置与结合spring使用的更多相关文章

  1. ActiveMQ安装配置及使用 转发 https://www.cnblogs.com/hushaojun/p/6016709.html

    ActiveMQ安装配置及使用 ActiveMQ介绍 ActiveMQ 是Apache出品,最流行的,能力强劲的开源消息总线.ActiveMQ 是一个完全支持JMS1.1和J2EE 1.4规范的 JM ...

  2. activemq持久化配置,设置为主从模式(带复制的主从模式,应用mysql数据库)

    配置文件如下<!--    Licensed to the Apache Software Foundation (ASF) under one or more    contributor l ...

  3. Spring配置与第一Spring HelloWorld

    林炳文Evankaka原创作品. 转载请注明出处http://blog.csdn.net/evankaka 本文将主讲了Spring在Eclipse下的配置,并用Spring执行了第一个HelloWo ...

  4. Spring Boot + Spring Cloud 构建微服务系统(九):配置中心(Spring Cloud Config)

    技术背景 如今微服务架构盛行,在分布式系统中,项目日益庞大,子项目日益增多,每个项目都散落着各种配置文件,且随着服务的增加而不断增多.此时,往往某一个基础服务信息变更,都会导致一系列服务的更新和重启, ...

  5. ActiveMQ内存配置和密码设置

    1.配置内存 bin中activemq.bat 中的第一行 加上 : REM 配置内存 set ACTIVEMQ_OPTS=-Xms1G -Xmx1G 2.修改控制台密码 1.打开conf/jetty ...

  6. (转)关于ActiveMQ的配置

    目前常用的消息队列组建无非就是MSMQ和ActiveMQ,至于他们的异同,这里不想做过多的比较.简单来说,MSMQ内置于微软操作系统之中,在部署上包含一个隐性条件:Server需要是微软操作系统.(对 ...

  7. 第八章 分布式配置中心:Spring Cloud Config

    Spring Cloud Config 是 Spring Cloud 团队创建的一个全新项目,用来为分布式系统中的基础设施和微服务应用提供集中化的外部配置支持, 它分为服务端与客户端两个部分. 其中服 ...

  8. 分布式配置中心介绍--Spring Cloud学习第六天(非原创)

    文章大纲 一.分布式配置中心是什么二.配置基本实现三.Spring Cloud Config服务端配置细节(一)四.Spring Cloud Config服务端配置细节(二)五.Spring Clou ...

  9. 创建客户端项目并读取服务化的配置中心(Consul + Spring Cloud Config)

    创建客户端项目并读取服务化的配置中心 将配置中心注册到服务中心(Consul) POM文件添加依赖: <dependency> <groupId>org.springframe ...

随机推荐

  1. VS2010 MFC的按钮风格改变

    改变VS2010 MFC的按钮风格 VS2010建的MFC工程按钮默认的风格类似VC6.0(直角矩形),如想美观按钮改为WIN7的按钮风格(圆角矩形),只需在代码中找到头文件"stdafx. ...

  2. 剑指offer30:连续子数组的最大和

    1 题目描述 HZ偶尔会拿些专业问题来忽悠那些非计算机专业的同学.今天测试组开完会后,他又发话了:在古老的一维模式识别中,常常需要计算连续子向量的最大和,当向量全为正数的时候,问题很好解决.但是,如果 ...

  3. Excel逻辑运算函数

    Excel逻辑运算函数 1.FALSE和TRUE的使用 ​ 筛选出表中salary>6.gender为男.age>28至少满足这三个条件中的两个的数据 1.依次使用:=C2>6.=D ...

  4. Python 中集合使用

    集合在使用中由于自动虑重,而且效率特高,故在提取数据时用上,但是由于集合没有切片功能没有取第几个元素的功能,但是一直使用集合切片不报错,但是执行不下去,导致一直存在问题. 修改为list后正常 例如: ...

  5. spring cloud Eureka 配置信息

    Eureka instance 一个服务,如:订单系统,会部署多台服务器,而每台服务器上提供的服务就是instance; 负载配置. Eureka service 指的是服务,提供一种特定功能的服务, ...

  6. SQLAlchemy 在查询期间丢失与MySQL服务器的连接

    遇到问题 pymysql.err.OperationalError: (2013, 'Lost connection to MySQL server during query') 建立的 pymysq ...

  7. Spring3.2.2中相关Jar包的作用

    今天在看Spring的源码的时候不知道从什么地方开启应该合适,因为不太清楚实现类所在的具体Jar包,就从网上找了些,可是网上有的说的是不清不楚,甚至是有些错误的,所以就把相关Jar包的大致作用给整理了 ...

  8. 【Distributed】分布式锁

    一.概述 1.1 分布式解决的核心思路 1.2 分布式锁一般有三种实现方式 二.基于Redis的分布式锁 2.1 使用常用命令 2.2 实现思路 2.3 核心代码 Maven依赖信息 LockRedi ...

  9. IDEA部署项目到远程服务器

    一.idea安装阿里插件Alibaba Cloud Toolkit 二.添加Host 三.应用部署 四.修改源程序重新部署 五.查看实时日志 欲买桂花同载酒,终不似,少年游

  10. MySQL主从复制以及在本地环境搭建

    MySQL主从复制原理: master(主服务器),slave(从服务器) MySQL master 将数据变更写入二进制日志( binary log, 其中记录叫做二进制日志事binary log ...