熟悉activemq的初步试用
1.在服务器(阿里云ubuntu16.04)上安装activemq,我是直接下载activemq:
wget http://archive.apache.org/dist/activemq/apache-activemq/5.6.0/apache-activemq-5.6.0-bin.tar.gz
2.解压及安装,可以通过activemq --help 查看一些命令及参数信息;
3.启动activemq;
4.编写简单的java demo:
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory; import javax.jms.*;
import java.util.concurrent.atomic.AtomicInteger; /**
* Created by IntelliJ IDEA.
*
* @author
* Description:
* Date: 2017/11/26
* Time: 12:07
*/
public class Producter {
/**
* ActiveMq 的默认用户名
*/
private static final String USERNAME = "xx";
/**
* ActiveMq 的默认登录密码
*/
private static final String PASSWORD = "xx";
/**
* ActiveMQ 的链接地址
*/
private static final String BROKEN_URL = "xx"; 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);
connectionFactory = new ActiveMQConnectionFactory(
ActiveMQConnection.DEFAULT_USER,
ActiveMQConnection.DEFAULT_PASSWORD,
"tcp://xx");
//从工厂中创建一个链接
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();
}
}
}
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory; import javax.jms.*;
import java.util.concurrent.atomic.AtomicInteger; /**
* Created by IntelliJ IDEA.
*
* @author
* Description:
* Date: 2017/11/26
* Time: 12:08
*/
public class Comsumer { /**
* ActiveMq 的默认用户名
*/
private static final String USERNAME = "xx";
/**
* ActiveMq 的默认登录密码
*/
private static final String PASSWORD = "xx";
/**
* ActiveMQ 的链接地址
*/
private static final String BROKEN_URL = "xx"; ConnectionFactory connectionFactory; Connection connection; Session session; ThreadLocal<MessageConsumer> threadLocal = new ThreadLocal<>();
AtomicInteger count = new AtomicInteger(); public void init(){
try {
connectionFactory = new ActiveMQConnectionFactory(
ActiveMQConnection.DEFAULT_USER,
ActiveMQConnection.DEFAULT_PASSWORD,
"tcp://xx");
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();
}
}
}
test:
/**
* Created by IntelliJ IDEA.
*
* @author
* Description:
* Date: 2017/11/26
* Time: 12:48
*/
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("zq-MQ");
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
/**
* Created by IntelliJ IDEA.
*
* @author
* Description:
* Date: 2017/11/26
* Time: 12:17
*/
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("zq-MQ");
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
以上仅仅是一个简单的demo,实际生产环境种使用,须要考虑使用场景,配置activemq的配置文件,以及与项目的整合问题。
熟悉activemq的初步试用的更多相关文章
- [dpdk] 熟悉SDK与初步使用 (四)(L3 Forwarding源码分析)
接续前节:[dpdk] 熟悉SDK与初步使用 (三)(IP Fragmentation源码分析) 前文中的最后一个问题,搁置,并没有找到答案.所以继续阅读其他例子的代码,想必定能在其他位置看到答案. ...
- [dpdk] 熟悉SDK与初步使用 (二)(skeleton源码分析)
接续前节:[dpdk] 熟悉SDK与初步使用 (一)(qemu搭建实验环境) 程序逻辑: 运行参数: 关键API: 入口函数: int rte_eal_init(int argc, char **ar ...
- Mysql 官方Memcached 插件初步试用感受 - schweigen - ITeye技术网站
Mysql 官方Memcached 插件初步试用感受 - schweigen - ITeye技术网站 Mysql 官方Memcached 插件初步试用感受
- [dpdk] 熟悉SDK与初步使用 (三)(IP Fragmentation源码分析)
对例子IP Fragmentation的熟悉,使用,以及源码分析. 功能: 该例子的功能有二: 一: 将IP分片? 二: 根据路由表,做包转发. 路由表如下: IP_FRAG: Socket : ad ...
- vs2015安装及初步试用
Vs2015一直都听说好用,便捷.之前用vc++6.0,总感觉界面很灰,让人编程兴趣不高,恰巧借此机会,安装一下vs2015,从编译器上体验下编程的舒心,方便.希望我不会变得太懒... 首先,我下的是 ...
- 在WINDOWS下初步试用OMNET++ 4
闲扯: 最近实习公司要做ZIGBEE,我是对这个兴趣不大,但工作还是要做的,目前帮着找找合适的仿真软件,什么NS-2啊,OPNET啊. 正文: 这个软件软件直接去官网下载就好了,免费开源. 安装也比较 ...
- [dpdk] 熟悉SDK与初步使用 (一)(qemu搭建实验环境)
搭建实验环境: troubleshoot 第一步加载驱动 第二步切换驱动 使用了所有qemu支持的卡 [tong@T7:~/VM/dpdk] % cat start.sh sudo qemu-syst ...
- 浅谈react的初步试用
现在最热门的前端框架,毫无疑问是 React . 上周,基于 React 的 React Native 发布,结果一天之内,就获得了 5000 颗星,受瞩目程度可见一斑. React 起源于 Face ...
- JAVA JDK的安装及初步试用
1.进入浏览器输入下图网址进入相关页面 2.网站主界面如图 3.单击箭头所指功能块 4.选择如下图的对应选项 5.进入如下页面后单击下图红色框 6.进入如下页面后单击如下红色框进行下载 7.下载好之 ...
随机推荐
- UI对象库-定位元素与程序分离
1.前言 这几天有人问我,UI自动化测试中使用到的页面定位元素应该存放在哪里比较合适?我想说的是如果你使用的是PO设计模式设计测试用例的话,可以把定位元素存在每一个page页面,一个page存放对应的 ...
- Tomcat调试404错误
开篇附上我找到的部分解决方法摘自:https://blog.csdn.net/psp0001060/article/details/51879232 如不想跳转查看,链接内容如下: 问题一: ...
- python接口自动化-post请求3
一.SSL 证书 https 的请求相对于http安全级别高,需要验证SSL证书import urllib3 使用这个方法就可以了urllib3.disable_warnings() 可忽略警告 二. ...
- Unity3D介绍
Unity3D介绍:Unity3D是一个游戏开发引擎 由Unity Technologies开发的一个让玩家轻松创建诸如三维视频游戏.建筑可视化.实时三维动画等类型互动内容的多平台的综合型游戏开发工具 ...
- Effective C++ 第0章 copy constructor和copy assignment operator
拷贝构造函数(copy constructor)被用来以一个对象来初始化同类型的另一个对象,拷贝赋值运算符(copy assignment operator)被用来将一个对象中的值拷贝到同类型的另一个 ...
- open-vm-tools与VMware Tools
安装VMware Tools经常会出现兼容性不好,系统之间复制文件失灵,并且安装时提示建议使用open-vm-tools,于是放弃vmware-tools的安装,尝试使用open-vm-tools o ...
- Cnario Player 接入视频采集卡采集外部音视频信号测试
测试产品 型号: TC-D56N1-30P采集卡 参数: 1* HDMI 1.4输入, PCIe 接口为PCI-Express x4(Gen2), 最高支持4096x2160@30Hz, 支持1920 ...
- CodeForces Round #554 Div.2
A. Neko Finds Grapes 代码: #include <bits/stdc++.h> using namespace std; ; int N, M; int a[maxn] ...
- Centos6.5-DHCPServer安装
1.查询dhcp有没有被安装,如下没有被安装 [zfp@localhost ~]$ rpm -q dhcppackage dhcp is not installed[zfp@localhost ~]$ ...
- c提高第四次作业
1. 简述指针数组和数组指针的区别?答: 指针数组:是一个数组,每个元素都是指针 数组指针:是一个指针,指向数组的指针 2. 如何定义一个指向 int a[10] 类型的指针变量(数组指针)(使用3种 ...