【分布式系列之ActiveMq】ActiveMq入门示例
前言
github地址:https://github.com/AndyFlower/web-back/tree/master/ActiveMq01
下载ActiveMQ :http://activemq.apache.org/download.html
放到自己的目录,大致目录如下:
- bin存放的是脚本文件
- conf存放的是基本配置文件
- data存放的是日志文件
- docs存放的是说明文档
- examples存放的是简单的实例
- lib存放的是activemq所需jar包
- webapps用于存放项目的目录
然后启动ActiveMQ:比如我的目录是:D:\develop tools\apache-activemq-5.15.2\bin\win64下的activemq.bat

出现如下消息则说明启动成功了。
登录上述启动成功的地址:http://127.0.0.1:8161用户名和密码是admin:admin
一、创建一个java项目,加入maven依赖
<dependencies>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.15.2</version>
</dependency>
</dependencies>
二、项目目录如下

三、编写具体的生产者和消费者
package com.slp.activemq; import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory; import javax.jms.*;
import java.util.concurrent.atomic.AtomicInteger; /**
* @author sanglp
* @create 2017-12-05 11:30
* @desc 生产者
**/
public class Producer {
//ActiveMQ默认用户名
private static final String USERNAME= ActiveMQConnection.DEFAULT_USER;
//ActiveMQ默认登陆密码
private static final String PASSWORD= ActiveMQConnection.DEFAULT_PASSWORD;
//ActiveMQ链接地址
private static final String BROKEN_URL= ActiveMQConnection.DEFAULT_BROKER_URL; AtomicInteger count = new AtomicInteger(0);
//链接工厂
ConnectionFactory connectionFactory;
//链接对象
Connection connection;
//事务管理
Session session;
ThreadLocal<MessageProducer> threadLocal = new ThreadLocal<MessageProducer>(); public void init(){
try {
//创建一个链接工厂
connectionFactory = new ActiveMQConnectionFactory(USERNAME,PASSWORD,BROKEN_URL);
//从工厂中创建一个链接
connection = connectionFactory.createConnection();
//开启链接
connection.start();
//创建一个事务(通过参数设置事务的级别)
session = connection.createSession(true,Session.SESSION_TRANSACTED);
} catch (JMSException e) {
e.printStackTrace();
}
} public void sendMessage(String disname){
try {
//创建一个消息队列
Queue queue = session.createQueue(disname);
//消息生产者
MessageProducer messageProducer = null;
if (threadLocal.get()!=null){
messageProducer = threadLocal.get();
}else {
messageProducer = session.createProducer(queue);
threadLocal.set(messageProducer);
}
while (true){
Thread.sleep(1000);
int num = count.getAndIncrement();
//创建一条消息
TextMessage msg = session.createTextMessage(Thread.currentThread().getName()+"productor:我正在生产东西!,count:"+count);
System.out.println(Thread.currentThread().getName()+"productor:我正在生产东西!,count:"+count);
//发送消息
messageProducer.send(msg);
//提交事务
session.commit();
}
} catch (JMSException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} }
}
package com.slp.activemq; import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory; import javax.jms.*;
import java.util.concurrent.atomic.AtomicInteger; /**
* @author sanglp
* @create 2017-12-05 11:30
* @desc 消费者
**/
public class Consumer {
private static final String USERNAME = ActiveMQConnection.DEFAULT_USER; private static final String PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD; private static final String BROKEN_URL = ActiveMQConnection.DEFAULT_BROKER_URL; ConnectionFactory connectionFactory; Connection connection; Session session; ThreadLocal<MessageConsumer> threadLocal = new ThreadLocal<MessageConsumer>();
AtomicInteger count = new AtomicInteger(); public void init(){
try {
connectionFactory = new ActiveMQConnectionFactory(USERNAME,PASSWORD,BROKEN_URL);
connection = connectionFactory.createConnection();
connection.start();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
} catch (JMSException e) {
e.printStackTrace();
}
} public void getMessage(String disname){
try {
Queue queue = session.createQueue(disname);
MessageConsumer consumer ; if(threadLocal.get()!=null){
consumer = threadLocal.get();
}else{
consumer = session.createConsumer(queue);
threadLocal.set(consumer);
}
while(true){
Thread.sleep(1000);
TextMessage msg = (TextMessage) consumer.receive();
if(msg!=null) {
msg.acknowledge();
System.out.println(Thread.currentThread().getName()+": Consumer:我是消费者,我正在消费Msg"+msg.getText()+"--->"+count.getAndIncrement());
}else {
break;
}
}
} catch (JMSException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
四、运行测试
package com.slp.activemq; /**
* @author sanglp
* @create 2017-12-05 11:31
* @desc mq测试
**/
public class TestMq {
public static void main(String[] args){
Producer producer = new Producer();
producer.init();
TestMq testMq = new TestMq();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//Thread 1
new Thread(testMq.new ProducorMq(producer)).start();
//Thread 2
new Thread(testMq.new ProducorMq(producer)).start();
//Thread 3
new Thread(testMq.new ProducorMq(producer)).start();
//Thread 4
new Thread(testMq.new ProducorMq(producer)).start();
//Thread 5
new Thread(testMq.new ProducorMq(producer)).start();
} private class ProducorMq implements Runnable{
Producer producer;
public ProducorMq(Producer producer){
this.producer = producer;
}
public void run() {
while(true){
try {
producer.sendMessage("Jaycekon-MQ");
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}

运行消费者
package com.slp.activemq; /**
* @author sanglp
* @create 2017-12-05 11:31
* @desc 消费者测试
**/
public class TestConcumer {
public static void main(String[] args){
Consumer consumer = new Consumer();
consumer.init();
TestConcumer testConsumer = new TestConcumer();
new Thread(testConsumer.new ConsumerMq(consumer)).start();
new Thread(testConsumer.new ConsumerMq(consumer)).start();
new Thread(testConsumer.new ConsumerMq(consumer)).start();
new Thread(testConsumer.new ConsumerMq(consumer)).start();
} private class ConsumerMq implements Runnable{
Consumer consumer;
public ConsumerMq(Consumer consumer){
this.consumer = consumer;
} public void run() {
while(true){
try {
consumer.getMessage("Jaycekon-MQ");
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}

【分布式系列之ActiveMq】ActiveMq入门示例的更多相关文章
- 【java开发系列】—— spring简单入门示例
1 JDK安装 2 Struts2简单入门示例 前言 作为入门级的记录帖,没有过多的技术含量,简单的搭建配置框架而已.这次讲到spring,这个应该是SSH中的重量级框架,它主要包含两个内容:控制反转 ...
- 分布式服务框架 dubbo/dubbox 入门示例
dubbo是一个分布式的服务架构,可直接用于生产环境作为SOA服务框架. 官网首页:http://dubbo.io/ ,官方用户指南 http://dubbo.io/User+Guide-zh.htm ...
- 分布式服务框架 dubbo/dubbox 入门示例(转)
dubbo是一个分布式的服务架构,可直接用于生产环境作为SOA服务框架. 官网首页:http://dubbo.io/ ,官方用户指南 http://dubbo.io/User+Guide-zh.htm ...
- 【java开发系列】—— struts2简单入门示例
前言 最近正好有时间总结一下,过去的知识历程,虽说东西都是入门级的,高手肯定是不屑一顾了,但是对于初次涉猎的小白们,还是可以提供点参考的. struts2其实就是为我们封装了servlet,简化了js ...
- ActiveMQ详细入门教程系列(一)
一.什么是消息中间件 两个系统或两个客户端之间进行消息传送,利用高效可靠的消息传递机制进行平台无关的数据交流,并基于数据通信来进行分布式系统的集成.通过提供消息传递和消息排队模型,它可以在分布式环境下 ...
- ActiveMQ入门示例
1.ActiveMQ下载地址 http://activemq.apache.org/download.html 2.ActiveMQ安装,下载解压之后如下目录
- ActiveMQ从入门到精通(二)
接上一篇<ActiveMQ从入门到精通(一)>,本篇主要讨论的话题是:消息的顺序消费.JMS Selectors.消息的同步/异步接受方式.Message.P2P/PubSub.持久化订阅 ...
- Velocity魔法堂系列一:入门示例
一.前言 Velocity作为历史悠久的模板引擎不单单可以替代JSP作为Java Web的服务端网页模板引擎,而且可以作为普通文本的模板引擎来增强服务端程序文本处理能力.而且Velocity被移植到不 ...
- ActiveMQ的P2P示例
ActiveMQ的P2P示例(点对点通信) (1)下载安装activemq,启动activeMQ. 详细步骤参考博客:http://www.cnblogs.com/DFX339/p/9050878.h ...
- Velocity魔法堂系列一:入门示例(转)
Velocity魔法堂系列一:入门示例 一.前言 Velocity作为历史悠久的模板引擎不单单可以替代JSP作为Java Web的服务端网页模板引擎,而且可以作为普通文本的模板引擎来增强服务端程序文本 ...
随机推荐
- unity-----------------------使用BuildAssetBundle打包
我发现很多美工兄弟都爱问程序Unity3d为什么总丢材质? 我不排除U3d有BUG的情况下会丢材质?但是其实很多时候是人为操作而引起的. 1.不保存就在上传 这个操作太恐怖了,切记!!在 U3D里 ...
- ZooKeeper系列
Zookeeper系列(一) ZooKeeper系列(二) ZooKeeper系列(三) ZooKeeper系列(四)
- Eclipse和MyEclipse的区别 分类: 编程工具 2015-07-18 11:12 23人阅读 评论(0) 收藏
今天,在一个Q群里有人问Eclipse和MyEclipse的区别.虽然对于知道的人来说答案很简单,但是对于不知道的人来说就很难,很多问题也都是这样的,会者不难,难者不会. 其实,网上搜搜答案就挺多的, ...
- [转] 关于QT的系统总结
出处:http://www.cnblogs.com/wangqiguo/p/4625611.html 阅读目录 编译环境与开发流程 QT项目的构成及原理 QT中的布局 QT中的通用控件 QVarian ...
- 前端可视化编程:liveReload安装,sublime 3
需要插件 sublime text3:View in Browser:LiveReload chrome:liveReload 配置方法 一:sublime text3 sublime 3下载地址: ...
- Go中error类型的nil值和nil
https://my.oschina.net/chai2010/blog/117923
- GDAL------加载Shapefile文件
代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; using Syst ...
- Go错误处理(二)
1,.error接口的定义 type error interface{ Error()string } 2.error的使用 func Foo(param int)(n int,err error){ ...
- CentOS 6.3 + Subversion + Usvn 搭建版本管理服务器
一. Subversion 简介 Subversion是一个自由,开源的版本控制系统.在Subversion管理下,文件和目录可以超越时空.Subversion将文件存放在中心版本库里.这个版本库很像 ...
- windows 下获取当前进程的线程数量
#include <TlHelp32.h> int get_thread_amount() { ; ]; PROCESSENTRY32 pe32; pe32.dwSize = sizeof ...