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(一)的更多相关文章
随机推荐
- 使用spring容器干掉if-else
spring容器干掉if-else 场景说明 最近新做一个项目,需要对不同店铺的商品做不同处理.例如storeA需要进行handleA操作,storeB需要进行handleB操作,如此类推 大家很容易 ...
- Mac上刚安装的WebStorm或PHPStorm遇到SVN版本太旧的问题
Mac上刚安装的WebStorm或PHPStorm遇到SVN版本太旧的问题: URL: svn: E155021: This client is too old to work with the wo ...
- 生产环境MySQL优化
a:硬件的优化: 1. 采用64位cpu,cpu至少4颗,L2缓存越大越好2. 内存要大,32-64G运行1-2个实例,96-128G运行3-4个实例3. 机械盘选用sas盘,转速15000以上,有可 ...
- spring boot 2.x 系列 —— spring boot 整合 kafka
文章目录 一.kafka的相关概念: 1.主题和分区 2.分区复制 3. 生产者 4. 消费者 5.broker和集群 二.项目说明 1.1 项目结构说明 1.2 主要依赖 二. 整合 kafka 2 ...
- lower_bound 和 upper_bound 功能和用法
以前用这两个函数的时候,简单看了几句别人的博客,记住了大概,用的时候每用一次就弄混一次,相当难受,今天对照着这两个函数的源码和自己的尝试发现:其实这两个函数只能用于 "升序" 序列 ...
- 使用SpringSecurity搭建授权认证服务(1) -- 基本demo认证原理
使用SpringSecurity搭建授权认证服务(1) -- 基本demo 登录认证是做后台开发的最基本的能力,初学就知道一个interceptor或者filter拦截所有请求,然后判断参数是否合理, ...
- django实战仿慕课网在线视频网站(完成了85%以上的功能已上传github:https://github.com/huwei86/mxonline):
1. 前台功能模块 基本模块:登录 注册 找回密码 / 全局搜索 / 个人中心, 课程功能:课程管理 / 讲师管理 / 授课机构管理 热门推荐 相关课程推荐 用户操作管理:用户收藏 / 课程评论 ...
- scrapy实战3利用fiddler对手机app进行抓包爬虫图片下载(重写ImagesPipeline):
关于fiddler的使用方法参考(http://jingyan.baidu.com/article/03b2f78c7b6bb05ea237aed2.html) 本案例爬取斗鱼 app 先利用fidd ...
- c++学习书籍推荐《C++ Primer(中文版)(第5版)》下载
百度云及其他网盘下载地址:点我 编辑推荐 <C++ Primer(中文版)(第5版)>编辑推荐:一书在手,架构无忧:三十位一线架构师真知实践:百位架构师献计献策:十万文字尽显架构精华. 媒 ...
- c语言学习书籍推荐《C语言学习路线图•C语言必须知道的300个问题》下载
下载地址:点我 <C语言学习路线图•C语言必须知道的300个问题>以基础知识为框架,介绍了c语言各部分知识所对应的常见开发疑难问题,并作了透彻地解析.<C语言学习路线图•C语言必须知 ...