zl程序教程

您现在的位置是:首页 >  其他

当前栏目

ActiveMQ之点对点使用详解编程语言

编程语言 使用 详解 activemq 点对点
2023-06-13 09:20:46 时间
public static void main(String[] args) throws JMSException { // 先创建连接工厂 密码默认采用admin admin ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(url); // 创建连接 Connection connection = activeMQConnectionFactory.createConnection(); // 启动连接 connection.start(); // 创建会话 Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); // 是否需要事务方式提交 消费方式默认自动签收 // 拿到session 创建目标 创建队列 Queue queue = session.createQueue(queueName); // 创建生产者 MessageProducer producer = session.createProducer(queue); // 生产者生产的消息 是放在这个queue里面的 for (int i = 1; i i++) { // 拿到队列 创建消息 TextMessage textMessage = session.createTextMessage("消息内容" + i); // 发送消息 producer.send(textMessage); // 关闭连接 connection.close(); System.out.println("消息发送完毕"); }

依赖:

 dependencies 

 dependency 

 groupId org.apache.activemq /groupId 

 artifactId activemq-core /artifactId 

 version 5.7.0 /version 

 /dependency 

 /dependencies 

查看结果:

ActiveMQ之点对点使用详解编程语言

 创建完了 producer 下面 comsumer:

 

package com.toov5.producer; 

import javax.jms.Connection; 

import javax.jms.JMSException; 

import javax.jms.Message; 

import javax.jms.MessageConsumer; 

import javax.jms.MessageListener; 

import javax.jms.Queue; 

import javax.jms.Session; 

import javax.jms.TextMessage; 

import org.apache.activemq.ActiveMQConnectionFactory; 


public static void main(String[] args) throws JMSException { // 先创建连接工厂 密码默认采用admin admin ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(url); // 创建连接 Connection connection = activeMQConnectionFactory.createConnection(); // 启动连接 connection.start(); // 创建会话 Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); // 是否需要事务方式提交 消费方式默认自动签收 // 拿到session 创建目标 创建队列 Queue queue = session.createQueue(queueName);