activeMQ_helloworld(一)
一、activeMQ下载,直接在Linux上wget http://mirror.bit.edu.cn/apache//activemq/5.14.5/apache-activemq-5.14.5-bin.tar.gz
使用tar -zxvf 解压即可,启动activeMQ很简单,直接cd到bin目录,./activemq start即可
activeMQ的默认端口是61616,后台管理界面的端口是8161,如果你的防火墙拦截了这些端口,你需要打开这些端口或者是关闭防火墙,
vim /etc/sysconfig/iptables

修改后需要让修改生效,键入/etc/init.d/iptables restart这条命令即可
2、打开管理界面http://192.168.243.128:8161/admin,输入用户名admin,密码admin,可以看到以下界面

二、生产者代码
package com.aciveMQ; 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.ActiveMQConnectionFactory; public class Producer { private static final String BROKER_URL = "tcp://192.168.243.128:61616"; public static void main(String[] args) {
ConnectionFactory connectionFactory;// 连接工厂
Connection connection = null;// 连接
Session session = null;// 会话
Queue destination;// 目标
MessageProducer messageProducer;// 消息生产者 connectionFactory = new ActiveMQConnectionFactory(BROKER_URL); try {
connection = connectionFactory.createConnection(); // 第一个参数表示是否加事物操作,第二个参数Session.AUTO_ACKNOWLEDGE表示自动确认接收,
//
session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
destination = session.createQueue("myFirstQueue");// 创建一个叫做myFirstQueue的目标队列 messageProducer = session.createProducer(destination);// 创建消息生产者
TextMessage tm = session.createTextMessage("hello world"); connection.start(); messageProducer.send(tm);
session.commit();// 启动了事物就必须提交,否则不能发消息
} catch (JMSException e) {
e.printStackTrace();
} finally {
if (connection != null) {
try {
session.close();
connection.close();
} catch (JMSException e) {
e.printStackTrace();
}
} }
} }
2、消费者
package com.aciveMQ; 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.ActiveMQConnectionFactory; /**
* @author may
*
*/
public class Consumer {
private static final String BROKER_URL = "tcp://192.168.243.128:61616"; public static void main(String[] args) { ConnectionFactory connectionFactory = null;// 连接工厂
Connection connection = null;// 连接
Session session = null;// 会话
Queue destination;// 目标
MessageConsumer messageConsumer; try {
connectionFactory = new ActiveMQConnectionFactory(BROKER_URL);
connection = connectionFactory.createConnection();
// 第一个参数表示是否加事物操作,第二个参数Session.AUTO_ACKNOWLEDGE表示自动确认接收,
// 消费消息不需要加事物
session = connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);
destination = session.createQueue("myFirstQueue");// 创建一个叫做myFirstQueue的目标主题
messageConsumer = session.createConsumer(destination); connection.start(); // receive(long argue)在取到队列中的消息后,会按每1s钟的时间再次读取
TextMessage textMessage = (TextMessage) messageConsumer.receive();
if (textMessage != null) { System.out.println(textMessage.getText()); } // System.out.println(textMessage); } catch (JMSException e) {
e.printStackTrace();
} finally {
if (connection != null) {
try {
session.close();
connection.close();
} catch (JMSException e) {
e.printStackTrace();
}
} } } }
3、pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.</modelVersion> <groupId>com.aciveMQ</groupId>
<artifactId>activeMQ_hello</artifactId>
<version>0.0.-SNAPSHOT</version>
<packaging>jar</packaging> <name>activeMQ_hello</name>
<url>http://maven.apache.org</url> <properties>
<project.build.sourceEncoding>UTF-</project.build.sourceEncoding>
</properties> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.</version>
<scope>test</scope>
</dependency> <dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.14.</version>
</dependency> <dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.</version>
<scope>test</scope>
</dependency> </dependencies>
</project>
三、测试
启动生产者,然后再启动消费者,就会输出hello world。
activeMQ_helloworld(一)的更多相关文章
随机推荐
- 15 BOM的介绍
avaScript基础分为三个部分: ECMAScript:JavaScript的语法标准.包括变量.表达式.运算符.函数.if语句.for语句等. DOM:文档对象模型,操作网页上的元素的API.比 ...
- HBase 学习之路(六)——HBase Java API 的基本使用
一.简述 截至到目前(2019.04),HBase 有两个主要的版本,分别是1.x 和 2.x ,两个版本的Java API有所不同,1.x 中某些方法在2.x中被标识为@deprecated过时.所 ...
- spring 5.x 系列第8篇 —— 整合Redis客户端 Jedis和Redisson (代码配置方式)
文章目录 一.说明 1.1 Redis 客户端说明 1.2 Redis可视化软件 1.3 项目结构说明 1.3 依赖说明 二.spring 整合 jedis 2.1 新建基本配置文件和其映射类 2.2 ...
- 【设计模式】结构型02装饰模式(Decorator Pattern)
装饰模式(Decorator Pattern) 意图:动态地给一个对象添加一些额外的职责.就增加功能来说,装饰器模式相比生成子类更为灵活. 主要解决:一般的,我们为了扩展一个类经常使用继承方式实现,由 ...
- MediatR一个.net中简单好用的中介者模式实现方案
MediatRGit地址:https://github.com/jbogard/MediatR 1.安装妞盖特包 一般来说只需要安装一个MediatR就行了,.net core程序需要再安装一个Med ...
- Programming In Lua 第六章
1, 2, 3,
- Codeforces 757B:Bash's Big Day(分解因子+Hash)
http://codeforces.com/problemset/problem/757/B 题意:给出n个数,求一个最大的集合并且这个集合中的元素gcd的结果不等于1. 思路:一开始把素数表打出来, ...
- WINDOWS 安装ZeroMQ
zmq看起来很好用,但是安装起来不是一般麻烦.原来以为java绑定会提供jar包直接可使用,但是官网没有提供已经编译好的库文件和jar.多么的不方便啊!最终还是要自己动手编译! 安装java版本的zm ...
- kuangbin专题 专题一 简单搜索 Shuffle'm Up POJ - 3087
题意:(1)有两副颜色多样的扑克牌,(A~H)表示不同颜色,给你两副牌,S1,S2和一副你需要洗出的KEY,S12由S2最底部,S1底部...一直下去,直到洗成S12,就是图片展示的那样.(2)洗好的 ...
- Git使用小技巧之免密登录
想要获取更多文章可以访问我的博客 - 代码无止境. 小代同学在使用Git的过程中发现,每次向远程仓库推送代码的时候都需要输入账号密码.做为一个程序员,多多少少都会有偷懒的思维.那么如何才能避免每次都要 ...