如何使用java程序操作ActiveMQ
来源:互联网 发布:python编辑脚本 编辑:程序博客网 时间:2024/06/01 07:45
第一步:开发activeMQ要先导入activemq-all-5.14.0.jar包,因为我创建的是maven 项目,因此需要使用maven坐标导入。如果创建的是普通java项目,则应该在lib目录下导入jar包。
<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.0</modelVersion> <groupId>com.niwotaxuexiba.maven</groupId> <artifactId>activeMQ_helloworld</artifactId> <version>0.0.1-SNAPSHOT</version> <name>activeMQ_helloworld</name> <dependencies> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-all</artifactId> <version>5.14.0</version> </dependency> </dependencies></project>
第二步:因为要使用JUnit测试,所以也需要将JUnit的jar包也导入pom文件。
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version></dependency>
第三步:编写MQ消息生产者。分为几个小的步骤:
①连接工厂。
②选择使用默认的用户名/密码/路径。
③new一个ActiveMQConnectionFactory对象,也就是实例化一个工厂,这是使用ActiveMQ的入口。
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
④获取一个连接
Connection connection = connectionFactory.createConnection();
⑤建立会话
Session session = connection.createSession(true,Session.AUTO_ACKNOWLEDGE);
⑥创建队列或者话题对象
Queue queue = session.createQueue("HelloWorld");
⑦创建生产者或者消费者
MessageProducer producer = session.createProducer(queue);
⑧发送消息
for(int i = 0; i < 10; i++){
producer.send(session.createTextMessage("你好,activeMQ:"+i));
}
⑨提交操作
session.commit();
public class ActiveMQProducer {@Testpublic void testProduceMQ() throws Exception {// 连接工厂// 使用默认用户名、密码、路径// 路径 tcp://host:61616ConnectionFactory connectionFactory = new ActiveMQConnectionFactory();// 获取一个连接Connection connection = connectionFactory.createConnection();// 建立会话Session session = connection.createSession(true,Session.AUTO_ACKNOWLEDGE);// 创建队列或者话题对象Queue queue = session.createQueue("HelloWorld");// 创建生产者 或者 消费者MessageProducer producer = session.createProducer(queue);// 发送消息for (int i = 0; i < 10; i++) {producer.send(session.createTextMessage("你好,activeMQ:" + i));}// 提交操作session.commit();}}
查看控制台,消息已经被生产
①使用MessageConsumer完成消费:
public class ActiveMQConsumer {@Test// 直接消费public void testCosumeMQ() throws Exception {// 连接工厂// 使用默认用户名、密码、路径// 路径 tcp://host:61616ConnectionFactory connectionFactory = new ActiveMQConnectionFactory();// 获取一个连接Connection connection = connectionFactory.createConnection();// 开启连接connection.start();// 建立会话// 第一个参数,是否使用事务,如果设置true,操作消息队列后,必须使用 session.commit();Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);// 创建队列或者话题对象Queue queue = session.createQueue("HelloWorld");// 创建消费者MessageConsumer messageConsumer = session.createConsumer(queue);while (true) {TextMessage message = (TextMessage) messageConsumer.receive(10000);if (message != null) {System.out.println(message.getText());} else {break;}}}}
查看控制台,发现信息已经被消费
②使用监听器,监听消息的内容,进行消费。
@Test// 使用监听器消费public void testCosumeMQ2() throws Exception {// 连接工厂// 使用默认用户名、密码、路径// 路径 tcp://host:61616ConnectionFactory connectionFactory = new ActiveMQConnectionFactory();// 获取一个连接Connection connection = connectionFactory.createConnection();// 开启连接connection.start();// 建立会话// 第一个参数,是否使用事务,如果设置true,操作消息队列后,必须使用 session.commit();Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);// 创建队列或者话题对象Queue queue = session.createQueue("HelloWorld");// 创建消费者MessageConsumer messageConsumer = session.createConsumer(queue);messageConsumer.setMessageListener(new MessageListener() {// 每次接收消息,自动调用 onMessagepublic void onMessage(Message message) {TextMessage textMessage = (TextMessage) message;try {System.out.println(textMessage.getText());} catch (JMSException e) {e.printStackTrace();}}});while (true) {// 不能让junit线程死掉}}
以上就是activeMQ消息的生产及效果的代码编写。
- 如何使用java程序操作ActiveMQ
- ActiveMQ的java操作
- Java ActiveMQ连接池使用
- Java操作 ActiveMQ 远程监控JMX设置
- Java操作 ActiveMQ 远程监控JMX设置
- 使用Java程序动态操作Tomcat
- 使用Java操作ElasticSearch1.7程序
- 使用JAVA程序操作FTP服务器
- 通过 ActiveMQ 演示如何使用 JMS API
- 通过 ActiveMQ 演示如何使用 JMS API
- Java ActiveMQ 理解JMS 和 ActiveMQ基本使用
- Java ActiveMQ 理解JMS 和 ActiveMQ基本使用
- Java ActiveMQ 理解JMS 和 ActiveMQ基本使用
- Java ActiveMQ 理解JMS 和 ActiveMQ基本使用
- 如何使用Java编写多线程程序
- 如何使用Java读取串口的程序
- 如何使用Java编写多线程程序
- 如何使用java程序实现windows锁屏
- python读取文本中的内容
- 大道至简,职场上做人做事做管理
- Python机器学习实践指南-中文-PDF
- Facebook登录-前端网页以及在angular中的使用方法
- Coursera deeplearning.ai 深度学习习题3-1-Structuring Machine Learning Projects(1)
- 如何使用java程序操作ActiveMQ
- Python Day2 字符串
- 数据结构实验之排序四:寻找大富翁
- unity详细解说Mecanim使用 动作 Animator
- spring中关于不能 new ClassPathXmlApplicationContext("");的问题
- ansible安装遇到"AttributeError: 'module' object has no attribute 'PROTOCOL_SSLv3'"的问题
- git基本使用
- php生成 文字水印 图片水印
- 阻塞与非阻塞的区别