一、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。

最新文章

  1. 深入理解java内存模型系列文章
  2. ajax与HTML5 history pushState/replaceState实例
  3. openfire配置MSSQL说明(数据库设置)
  4. Kafka的配置文件详细描述
  5. SignalR的简单实现
  6. Android通过tcpdump抓包(wifi, 2g, 3g都可以)
  7. ASP.NET Web API 使用记录
  8. Linux命令之 文件归档管理
  9. Python第八天——Json
  10. 打包zip下载
  11. UVA732【DFS+栈】
  12. qt 打包发布 获取dll
  13. Bootstrap-datepicker3官方文档中文翻译---概述(原文链接 http://bootstrap-datepicker.readthedocs.io/en/latest/index.html)
  14. bootstrap之navbar
  15. WebApi中关于base64和jwt的联合验证
  16. 写出优质Java代码的4个技巧(转)
  17. python带参数装饰器使用
  18. 手机端API接口验证及参数签名验证
  19. SSL虚拟主机安全方案
  20. 同步IO与同步非阻塞IO的理解

热门文章

  1. 08 Javascript的函数
  2. 为什么很多IT公司不喜欢进过培训机构的人呢
  3. gRPC入坑记
  4. MySQL 全文索引实现简单版搜索引擎
  5. iOS中 分类(category)与扩展(Extension)的区别?
  6. PAT L3-016:二叉搜索树的结构(暴力)
  7. visual studio code emmet 插件不提示?解决方案
  8. 开源FTP/SFTP客户端 FileZilla v3.31.0 绿色便携版
  9. 1. Django每日一码 之原生View源码
  10. Windows下必备的开发神器之Cmder使用说明