首页 技术 正文
技术 2022年11月9日
0 收藏 773 点赞 3,732 浏览 3880 个字

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

activeMQ_helloworld(一)

修改后需要让修改生效,键入/etc/init.d/iptables restart这条命令即可

2、打开管理界面http://192.168.243.128:8161/admin,输入用户名admin,密码admin,可以看到以下界面

activeMQ_helloworld(一)

二、生产者代码

 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。
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,490
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,905
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,739
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,491
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,130
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,292