package pfs.y2017.m11.mq.activemq.demo02;

import java.util.concurrent.atomic.AtomicInteger;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage; import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory; public class Comsumer { 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<>();
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 = null; 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 pfs.y2017.m11.mq.activemq.demo02;

import java.util.concurrent.atomic.AtomicInteger;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage; import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory; public class Producter { // 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<>(); 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:" + num);
System.out.println(Thread.currentThread().getName() + "productor:我是大帅哥,我现在正在生产东西!,count:" + num);
// 发送消息
messageProducer.send(msg);
// 提交事务
session.commit();
}
} catch (JMSException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

  

package pfs.y2017.m11.mq.activemq.demo02;

public class TestConsumer {
public static void main(String[] args){
Comsumer comsumer = new Comsumer();
comsumer.init();
TestConsumer testConsumer = new TestConsumer();
new Thread(testConsumer.new ConsumerMq(comsumer)).start();
new Thread(testConsumer.new ConsumerMq(comsumer)).start();
new Thread(testConsumer.new ConsumerMq(comsumer)).start();
new Thread(testConsumer.new ConsumerMq(comsumer)).start();
} private class ConsumerMq implements Runnable{
Comsumer comsumer;
public ConsumerMq(Comsumer comsumer){
this.comsumer = comsumer;
} @Override
public void run() {
while(true){
try {
comsumer.getMessage("Jaycekon-MQ2");
// Thread.sleep(10000);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}

  

package pfs.y2017.m11.mq.activemq.demo02;

public class TestMq {
public static void main(String[] args){
Producter producter = new Producter();
producter.init();
TestMq testMq = new TestMq();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//Thread 1
new Thread(testMq.new ProductorMq(producter)).start();
//Thread 2
new Thread(testMq.new ProductorMq(producter)).start();
//Thread 3
new Thread(testMq.new ProductorMq(producter)).start();
//Thread 4
new Thread(testMq.new ProductorMq(producter)).start();
//Thread 5
new Thread(testMq.new ProductorMq(producter)).start();
} private class ProductorMq implements Runnable{
Producter producter;
public ProductorMq(Producter producter){
this.producter = producter;
} @Override
public void run() {
while(true){
try {
producter.sendMessage("Jaycekon-MQ2");
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}

  

ActiveMQ(二) 转的更多相关文章

  1. 学习ActiveMQ(二):点对点(队列)模式消息演示

    一:介绍 点对点的消息发送方式主要建立在 消息(Message ),队列(Queue),发送者(Sender),消费者(receiver)上,Queue 存贮消息,Sender 发送消息,receiv ...

  2. ActiveMQ (二) 常用配置简介

    ActiveMQ的主要配置文件 ActiveMQ的一些常用的属性很多可以在对应的配置文件中进行配置的.比如访问web console的管理端的端口,用户名密码,连接MQ时的用户名和密码,持久化设置,是 ...

  3. ActiveMQ (二)—发布订阅模式

    ActiveMQ的另一种模式就SUB/HUB即发布订阅模式,是SUB/hub就是一拖N的USB分线器的意思.意思就是一个来源分到N个出口.还是上节的例子,当一个订单产生后,后台N个系统需要联动,但有一 ...

  4. ActiveMQ (二):JMS

    1.前言 由于ActiveMQ是一种完全符合JMS规范的一种通信工具,所以在使用ActiveMQ前认识JMS规范就变的十分必要了. 认识JMS主要从以下方面: a. JMS 模型 b. JMS 对象模 ...

  5. ActiveMQ (二) JMS入门

    JMS入门 前提:安装好了ActiveMQ  ActiveMQ安装 Demo结构: 首先pom.xml引入依赖: <dependency> <groupId>org.apach ...

  6. springboot整合activemq(二),消费均匀分析

    问题分析:当如果多个消费者是什么情况呢 topic消费是友多个消费者的,是支持的,但是queue是支持,但是不能保证多个消费均匀消费,在分布式环境下怎么操作呢: 看案例: 在前面整合代码执行: 浏览器 ...

  7. C#实现ActiveMQ消息队列

    本文使用C#实现ActiveMQ消息队列功能. 一.首先需要导入两个包,分别是:Apache.NMS 和 Apache.NMS.ActiveMQ 二.创建Winform程序实现生产者功能. 三.Pro ...

  8. JMQ

    [京东技术]京东的MQ经历了JQ->AMQ->JMQ的发展,其中JQ的基于关系数据库,严格意义上讲称不上消息中间件,JMQ的存储是JFS和HBase,AMQ即ActiveMQ,本文说说JM ...

  9. 京东消息中间件JMQ

    http://blog.csdn.net/javahongxi/article/details/54411464 [京东技术]京东的MQ经历了JQ->AMQ->JMQ的发展,其中JQ的基于 ...

随机推荐

  1. jquery 元素文本取值/赋值

    select元素选中option值 jq $("#select1 option:selected").text(); $("select id或class option: ...

  2. vue.js 利用SocketCluster实现动态添加数据及排序

    直接上代码 html: <!DOCTYPE html> <html lang="en"> <head> <meta charset=&qu ...

  3. CoCoS 2D-JS的开发环境搭建

    CoCoS 2D-JS的开发环境搭建 在Hbuilder中新建web项目,将cocos2d-js-v3.9.js复制到在js文件夹下,将project.json复制到工程的根目录下 在index.ht ...

  4. [BZOJ1590] [Usaco2008 Dec]Secret Message 秘密信息(字典树)

    传送门 看到前缀就要想到字典树! 看到前缀就要想到字典树! 看到前缀就要想到字典树! #include <cstdio> #include <iostream> #define ...

  5. FZU 2020 :组合 【lucas】

    Problem Description 给出组合数C(n,m), 表示从n个元素中选出m个元素的方案数.例如C(5,2) = 10, C(4,2) = 6.可是当n,m比较大的时候,C(n,m)很大! ...

  6. ElasticSearch聚合入门(续)

    主要理解聚合中的terms. 参考:http://www.cnblogs.com/xing901022/p/4947436.html Terms聚合 记录有多少F,多少M { "size&q ...

  7. J2ME开发入门

    原文发布时间为:2008-07-31 -- 来源于本人的百度文章 [由搬家工具导入] J2ME开发入门J2ME方面开发的资料,确实是少之又少,一般给新手推荐的都是王森先生的《PDA与手机开发入门》一书 ...

  8. ftrace笔记

    mount -t debugfs nodev /sys/kernel/debug 在mount后,可以在debug目录下看到tracing目录,该目录包含了ftrace的控制与输出文件. (1) en ...

  9. mysql合并和时间函数

    sql:利用group_concat()方法,参数为需要合并的字段,合并的字段分隔符默认为逗号,可通过参数separator指定,该方法往往配合group by 一起使用.利用group_concat ...

  10. Spring Batch 文档(中文)

    http://blog.csdn.net/shorn/article/category/1186181