本地配置及使用

参考博客https://www.cnblogs.com/hushaojun/p/6016709.html

1.安装

下载地址:http://activemq.apache.org/download.html 

2.配置(conf目录下)

1)开启jms监控,activemq.xml中进行如下修改(注:这里的配置不是必须,根据需要自行配置 

这里写图片描述

3、启动 

直接运行bin目录下:activemq.bat 


4、可视化界面 

浏览器中:http://localhost:8161/admin/index.jsp 

用户名,密码在:jetty-realm.properties中设置


备注: 第一个为用户名  第二个是密码 第三个是角色

5.与项目整合

5.1实战版


重启一下服务或往任意队列发送一下消息(两者都做了,不知道哪个发挥了作用)

就可以看到  项目中的队列出现在本地mq中(如下图)



5.2测试版

消息生产者

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination; import javax.jms.JMSException; import javax.jms.MessageProducer; import javax.jms.Session; import javax.jms.TextMessage; import org.apache.activemq.ActiveMQConnectionFactory; public class QueueProducer { public static void main(String[] args) { //连接信息设置 String username = "system"; String password = "manager"; String brokerURL = "failover://tcp://localhost:61616"; //连接工厂 ConnectionFactory connectionFactory = null; //连接 Connection connection = null; //会话 接受或者发送消息的线程 Session session = null; //消息的目的地 Destination destination = null; //消息生产者 MessageProducer messageProducer = null; //实例化连接工厂 connectionFactory = new ActiveMQConnectionFactory(username, password, brokerURL); try { //通过连接工厂获取连接 connection = connectionFactory.createConnection(); //启动连接 connection.start(); //创建session session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE); //创建一个名称为QueueTest的消息队列 destination = session.createQueue("QueueTest"); //创建消息生产者 messageProducer = session.createProducer(destination); //发送消息 TextMessage message = null; for (int i=0; i<10; i++) { //创建要发送的文本信息 message = session.createTextMessage("Queue消息测试" +(i+1)); //通过消息生产者发出消息 messageProducer.send(message); System.out.println("发送成功:" + message.getText()); } session.commit(); } catch (Exception e) { e.printStackTrace(); }finally{ if(null != connection){ try { connection.close(); } catch (JMSException e) { e.printStackTrace(); } } } } } 

 

 

消息消费者

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination; import javax.jms.JMSException; import javax.jms.MessageConsumer; import javax.jms.Session; import javax.jms.TextMessage; import org.apache.activemq.ActiveMQConnectionFactory; public class QueueConsumer { public static void main(String[] args) { //连接信息设置 String username = "system"; String password = "manager"; String brokerURL = "failover://tcp://localhost:61616"; //连接工厂 ConnectionFactory connectionFactory = null; //连接 Connection connection = null; //会话 接受或者发送消息的线程 Session session = null; //消息的目的地 Destination destination = null; //消息消费者 MessageConsumer messageConsumer = null; //实例化连接工厂 connectionFactory = new ActiveMQConnectionFactory(username, password, brokerURL); try { //通过连接工厂获取连接 connection = connectionFactory.createConnection(); //启动连接 connection.start(); //创建session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); //创建一个连接QueueTest的消息队列 destination = session.createQueue("QueueTest"); //创建消息消费者 messageConsumer = session.createConsumer(destination); while (true) { TextMessage textMessage = (TextMessage) messageConsumer.receive(100000); if(textMessage != null){ System.out.println("成功接收消息:" + textMessage.getText()); }else { break; } } } catch (JMSException e) { e.printStackTrace(); } }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值