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(一)的更多相关文章
随机推荐
- 02-MySQL的安装和管理
# mysql的安装和基本管理 # 01 数据库管理软件分类 ''' 分两大类: 关系型:如sqllite,db2,oracle,access,sql server,MySQL,注意:sql语句通用 ...
- Codeforces Round #563 (Div. 2)C
C. Ehab and a Special Coloring Problem 题目链接:http://codeforces.com/contest/1174/problem/C 题目 You're g ...
- bugku welcome to bugkuctf
题目地址:http://123.206.87.240:8006/test1/ 这道题主要用到了俩个知识点:php伪协议和序列化 点进题目看到:you are not the number of bug ...
- 5分钟快速部署DataDraw数字绘
经常有小伙伴问,有没有一款好用又免费的画图软件,画画流程图.UML.思维导图?今天就介绍一款开源的在线画图软件,满足各方面人的需求. DataDraw数字绘是一个在线线框图.流程图.网络图.组织结构图 ...
- ZOJ 3981:Balloon Robot(思维+递推)
题目链接 题意 有n支队在m个位置上做题,有一个机器人位置1到位置m再到位置1循环走派发气球,当队伍a在时间b做完了一道题目的时候,假如机器人走到队伍a的位置的时间为c,那么这个队伍的不开心值就是c- ...
- git简单使用-GitHub
本文描述window下如何使用git工具,操作GitHub远程代码库 一,准备工作: 1,安装git工具,一路默认next安装即可,下载地址 2,注册账号或者创建厂库(已有忽略) 注册账号后,创建仓库 ...
- ConnectionPool实现redis在python中的连接
这篇文章主要介绍了Python与Redis的连接教程,Redis是一个高性能的基于内存的数据库,需要的朋友可以参考下 今天在写zabbix storm job监控脚本的时候用到了python的re ...
- django基础知识之自连接:
自连接 对于地区信息,属于一对多关系,使用一张表,存储所有的信息 类似的表结构还应用于分类信息,可以实现无限级分类 新建模型AreaInfo,生成迁移 class AreaInfo(models.Mo ...
- Spring 核心技术(2)
接上篇:Spring 核心技术(1) version 5.1.8.RELEASE 1.3 Bean概述 Spring IoC 容器管理一个或多个bean,他们都是根据提供的配置元数据(例如 XML 中 ...
- mongon库加上权限认证后,java程序连接异常
现象: linux库中mongo库,用超级管理员添加了一个超级管理员,并加了认证. 通过spring等相关的配置文件获取Mongo数据库连接,抛出异常,如下: Caused by: org.sprin ...