依赖
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
</dependency>
工具类
public class RabbitmqUtils {
private static final ConnectionFactory connectionFactory;
static {
//创建连接mq的连接工厂对象,重量级对象,类加载时创建一次即可
connectionFactory = new ConnectionFactory();
//设置连接rabbitmq的主机
connectionFactory.setHost("192.168.232.134");
//设置端口号
connectionFactory.setPort(5672);
//设置连接的虚拟主机
connectionFactory.setVirtualHost("/ems");
//设置访问虚拟主机的用户名和密码
connectionFactory.setUsername("alice");
connectionFactory.setPassword("123");
}
//获取连接对象
public static Connection getConnection(){
try {
return connectionFactory.newConnection();
} catch (IOException | TimeoutException e) {
e.printStackTrace();
}
return null;
}
//关闭通道和连接
public static void close(Channel channel, Connection connection){
if(channel != null){
try {
channel.close();
} catch (IOException | TimeoutException e) {
e.printStackTrace();
}
}
if(connection != null){
try {
connection.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
直连模型
##开发生产者
public class Provider {
public static void main(String[] args) {
Connection connection = null;
Channel channel = null;
try {
//获取连接对象
connection = RabbitmqUtils.getConnection();
//获取通道
if (connection != null) {
channel = connection.createChannel();
//通道绑定对应消息队列
/*
* 参数1 queue:队列名称(不存在自动创建)
* 参数2 durable:用来定义队列特性是否需要持久化(为true该队列将在服务器重启后保留下来,持久化到硬盘中)
* 参数3 exclusive:是否独占队列(为true仅限此连接)
* 参数4 autoDelete:是否在消费完成后自动删除队列
* 参数5 arguments:队列的其他属性(构造参数)
* */
channel.queueDeclare("hello",false,false,false,null);
//发布消息
/*
* 参数1 exchange:要将消息发布到的交换机
* 餐数2 routingKey:路由键,指定队列
* 参数3 props:消息的其他属性
* 参数4 body:消息具体内容
* */
String message = "hello rabbitmq";
channel.basicPublish("","hello",null,message.getBytes());
}
} catch (IOException e) {
e.printStackTrace();
} finally {
System.out.println("生产者发布消息完成......");
RabbitmqUtils.close(channel,connection);
}
}
}
##消费者
public class Consumer {
public static void main(String[] args) {
Connection connection = null;
Channel channel = null;
try {
//获取连接对象
connection = RabbitmqUtils.getConnection();
//获取通道
if (connection != null) {
channel = connection.createChannel();
//通道绑定对应消息队列
/*
* 参数1 queue:队列名称(不存在自动创建)
* 参数2 durable:用来定义队列特性是否需要持久化(为true该队列将在服务器重启后保留下来)
* 参数3 exclusive:是否独占队列(为true仅限此连接)
* 参数4 autoDelete:是否在消费完成不再使用后自动删除队列
* 参数5 arguments:队列的其他属性(构造参数)
* */
channel.queueDeclare("hello",false,false,false,null);
//消费消息
DefaultConsumer defaultConsumer = new DefaultConsumer(channel){
//获取消息并且处理。此方法类似于事件监听,有消息时会被自动调用
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
System.out.println("message:"+new String(body)); //body即消息体
}
};
/*
* 参数1 queue:队列名称
* 参数2 autoAck:开启消息的自动确认机制
* 参数3 Consumer callback:消费时的回调接口
* */
channel.basicConsume("hello",true, defaultConsumer);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
System.out.println("消费者消费消息完成......");
//RabbitmqUtils.close(channel,connection);
}
}
}
##参数
//对队列和队列中消息进行持久化设置
//第二个参数改为true,该队列将在服务器重启后保留下来
channel.queueDeclare("hello",true,false,false,null);
//发布消息时添加消息的属性设置,第三个参数改为MessageProperties.PERSISTENT_TEXT_PLAIN
channel.basicPublish("","hello", MessageProperties.PERSISTENT_TEXT_PLAIN,message.getBytes());
//消费者获取消息时,保证绑定的消息队列参数一致
channel.queueDeclare("hello",true,false,false,null);
模型工作队列
##生产者
public class Provider {
public static void main(String[] args) {
Connection connection = null;
Channel channel = null;
try {
//获取连接对象
connection = RabbitmqUtils.getConnection();
if (connection != null) {
//获取通道对象
channel = connection.createChannel();
//声明队列
channel.queueDeclare("work",true,false,false,null);
//生产消息
String message = "";
for (int i = 1; i <= 20; i++) {
message = "work queues,id:"+i;
channel.basicPublish("","work",null,message.getBytes());
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
RabbitmqUtils.close(channel,connection);
}
}
}
##消费者
public class Consumer1 {
public static void main(String[] args) {
Connection connection = null;
Channel channel = null;
try {
//获取连接
connection = RabbitmqUtils.getConnection();
if (connection != null) {
//获取通道
channel = connection.createChannel();
//声明队列
channel.queueDeclare("work",true,false,false,null);
//消费消息
channel.basicConsume("work",true,new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
System.out.println("Consumer-1:"+new String(body));//打印消息
}
});
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class Consumer2 {
public static void main(String[] args) {
Connection connection = null;
Channel channel = null;
try {
//获取连接
connection = RabbitmqUtils.getConnection();
if (connection != null) {
//获取通道
channel = connection.createChannel();
//声明队列
channel.queueDeclare("work",true,false,false,null);
//消费消息
channel.basicConsume("work",true,new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
System.out.println("Consumer-2:"+new String(body));//打印消息
}
});
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
默认情况下,RabbitMQ将按顺序将每个消息发送给下一个使用者。平均而言,每个消费者都会收到相同数量的消息。这种分发消息的机制称为轮询
测试2(消费者不能及时处理消息)
模拟consumer1消费消息耗时较长
//消费消息
channel.basicConsume("work",true,new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
try {
Thread.sleep(1000); //假设consumer1处理消息耗时较长
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Consumer-1:"+new String(body));//打印消息
}
});
consumer1和consumer2依旧消费了同样的消息。此时consumer1已经从队列中拿到所有消息,但由于处理能力较差,不能及时处理,导致处理消息很慢。
我们希望处理消息更快的消费者能处理更多的消息,处理能力差的少消费
"能者多劳"的实现 :处理消息能力好的消费者处理更多任务,处理能力差的分发较少任务
修改两个消费者
- 关闭消息自动确认
- 设置同一时刻服务器只发送一条消息给同一消费者
- 开启消息的手动确认
//消费消息
channel.basicQos(1);//每次只发送一条消息给同一消费者
channel.basicConsume("work",false,new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
try {
Thread.sleep(1000); //假设consumer1执行时间较长
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Consumer-1:"+new String(body));//打印消息
//开启消息手动确认 参数1:队列中消息确认标识 参数2:是否开启多个消息同时确认
channel.basicAck(envelope.getDeliveryTag(),false);
}
});
订阅模型之fanout
##生产者
public class Provider {
public static void main(String[] args) {
Connection connection = null;
Channel channel = null;
//获取连接对象
connection = RabbitmqUtils.getConnection();
try {
if (connection != null) {
//获取通道对象
channel = connection.createChannel();
//将通道声明指定交换机 参数1:交换机名称 参数2:交换机类型
channel.exchangeDeclare("logs","fanout");
//发布消息(指定交换机)
String message = "fanout message";
channel.basicPublish("logs","",null,message.getBytes());
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//释放资源
RabbitmqUtils.close(channel,connection);
}
}
}
##消费者
public class Consumer1 {
public static void main(String[] args) {
try {
//获取连接
Connection connection = RabbitmqUtils.getConnection();
if (connection != null) {
//获取通道
Channel channel = connection.createChannel();
//声明交换机
channel.exchangeDeclare("logs","fanout");
//临时队列
String queue = channel.queueDeclare().getQueue();
//绑定交换机和队列 队列名称 交换机名称 路由键
channel.queueBind(queue,"logs","");
//消费消息
channel.basicConsume(queue,true,new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
System.out.println("Consumer1: "+new String(body)); //打印消息
}
});
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
订阅模型之routing
##生产者
public class Provider {
public static void main(String[] args) {
Connection connection = null;
Channel channel = null;
try {
//获取连接
connection = RabbitmqUtils.getConnection();
if (connection != null) {
//获取通道
channel = connection.createChannel();
//通过通道声明交换机 交换机类型为direct
channel.exchangeDeclare("logs_direct","direct");
//发送消息
String routingKey = "info";
String message = "direct--routingKey,routingKey:"+routingKey;
//交换机名称 路由键 消息其他属性 消息具体内容
channel.basicPublish("logs_direct",routingKey,null,message.getBytes());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
//释放资源
RabbitmqUtils.close(channel,connection);
}
}
}
##消费者
public class Consumer1 {
public static void main(String[] args) {
try {
//获取连接
Connection connection = RabbitmqUtils.getConnection();
if (connection != null) {
//获取通道
Channel channel = connection.createChannel();
//通道声明交换机与交换机类型
channel.exchangeDeclare("logs_direct","direct");
//临时队列
String queue = channel.queueDeclare().getQueue();
//基于route key绑定队列和交换机
channel.queueBind(queue,"logs_direct","error");
//消费消息
channel.basicConsume(queue,true,new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
System.out.println("Consumer1:"+new String(body));
}
});
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class Consumer2 {
public static void main(String[] args) {
try {
//获取连接
Connection connection = RabbitmqUtils.getConnection();
if (connection != null) {
//获取通道
Channel channel = connection.createChannel();
//声明交换机与交换机类型
channel.exchangeDeclare("logs_direct","direct");
//获取临时队列
String queue = channel.queueDeclare().getQueue();
//基于route key绑定队列与交换机
channel.queueBind(queue,"logs_direct","info");
channel.queueBind(queue,"logs_direct","error");
channel.queueBind(queue,"logs_direct","warning");
//消费消息
DefaultConsumer defaultConsumer = new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
System.out.println("Consumer2:"+new String(body));
}
};
channel.basicConsume(queue,true,defaultConsumer);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
交换机将消息发送到指定routingkey为 info 的队列
Consumer1 订阅routingkey为error的队列
Consumer2 订阅routingkey为error/info/warning的队列
所以Consumer1未收到消息,Consumer2收到消息
订阅模型之topics
##生产者
public class Provider {
public static void main(String[] args) {
Connection connection = null;
Channel channel = null;
try {
//获取连接
connection = RabbitmqUtils.getConnection();
if (connection != null) {
//获取通道
channel = connection.createChannel();
//声明交换机及交换机类型
channel.exchangeDeclare("topics","topic");
//发布消息
String routingKey = "user.save";
String message = "hello topic,routingKey:"+routingKey;
channel.basicPublish("topics",routingKey,null,message.getBytes());
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//释放资源
RabbitmqUtils.close(channel,connection);
}
}
}
##消费者
public class Consumer1 {
public static void main(String[] args) {
try {
//获取连接
Connection connection = RabbitmqUtils.getConnection();
//获取通道
if (connection != null) {
Channel channel = connection.createChannel();
//声明交换机与交换机类型
channel.exchangeDeclare("topics","topic");
//生成临时队列
String queue = channel.queueDeclare().getQueue();
//绑定交换机与队列 使用通配符形式routingKey
channel.queueBind(queue,"topics","user.*");
//消费消息
DefaultConsumer defaultConsumer = new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
System.out.println("Consumer1:"+new String(body)); //打印消息
}
};
channel.basicConsume(queue,true,defaultConsumer);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class Consumer2 {
public static void main(String[] args) {
try {
//获取连接
Connection connection = RabbitmqUtils.getConnection();
//获取通道
if (connection != null) {
Channel channel = connection.createChannel();
//声明交换机与交换机类型
channel.exchangeDeclare("topics","topic");
//生成临时队列
String queue = channel.queueDeclare().getQueue();
//绑定交换机与队列 使用通配符形式routingKey
channel.queueBind(queue,"topics","user.#");
//消费消息
DefaultConsumer defaultConsumer = new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
System.out.println("Consumer2:"+new String(body)); //打印消息
}
};
channel.basicConsume(queue,true,defaultConsumer);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Provider 发布消息提供的routingKey为 user.save
Consumer1 接收队列的消息匹配routingKey为 user.*
Consumer2 接收队列的消息匹配routingKey为 user.#
两者都能接收到消息
使用 订阅模型之routing 结合代码
//交换机名称
private final static String EXCHANGE_NAME = "send.notice";
//队列名称
private static final String QUEUE_NAME = "send.notice :";
//队列类型
private static final String QUEUE_TYPE = "direct";
//死信交换机的名字
public static final String MY_DLX_DIRECT_EXCHANGE_NAME = "my_dlx_direct_exchange_name";
//死信队列的名字
public static final String MY_DLX_DIRECT_QUEUE_NAME_01 = "my_dlx_direct_queue_name_01";
//死信队列的key
public static final String MY_DLX_DIRECT_KEY = "my_dlx_direct_key";
/**
* 发送消息
* @param sendNoticeDto
* @return
*/
public Result sendNoticeQueueOld(SendNoticeDto sendNoticeDto) {
Connection connection = null;
Channel channel = null;
try {
//获取连接
connection = RabbitmqUtils.getConnection();
if (connection != null) {
//获取通道
channel = connection.createChannel();
//通过通道声明交换机 交换机类型为direct
channel.exchangeDeclare(EXCHANGE_NAME,QUEUE_TYPE);
//key 和 消息
String routingKey = sendNoticeDto.getUserId();
String message = sendNoticeDto.getNoticeQuantity();
//发送消息
System.out.println("routingKey:"+ sendNoticeDto.getUserId());
System.out.println("message:"+ sendNoticeDto.getNoticeQuantity());
//交换机名称 路由键 消息其他属性 消息具体内容
channel.basicPublish(EXCHANGE_NAME,routingKey,null,message.getBytes());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
//释放资源
RabbitmqUtils.close(channel,connection);
}
return new Result();
}
/**
* 发送消息
* @param sendNoticeDto
* @return
*/
@Override
public Result sendNoticeQueue(SendNoticeDto sendNoticeDto) {
Connection connection = null;
Channel channel = null;
try {
//获取连接
connection = RabbitmqUtils.getConnection();
if (connection != null) {
//获取通道
channel = connection.createChannel();
//通过通道声明交换机 交换机类型为direct
channel.exchangeDeclare(EXCHANGE_NAME,QUEUE_TYPE);
//key 和 消息
String routingKey = sendNoticeDto.getUserId();
String message = sendNoticeDto.getNoticeQuantity();
//发送消息
System.out.println("routingKey:"+ sendNoticeDto.getUserId());
System.out.println("message:"+ sendNoticeDto.getNoticeQuantity());
//交换机名称 路由键 消息其他属性 消息具体内容
channel.basicPublish(EXCHANGE_NAME,routingKey,null,message.getBytes());
//用户创建WebSocket链接 就调用消费方法
int count = WebSocket.findToUserId(routingKey);
if(count>0){
consumptionClaimQueue(routingKey);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
//释放资源
RabbitmqUtils.close(channel,connection);
}
return new Result();
}
/**
* 创建队列
* @param sendNoticeDto
* @return
*/
public Result ClaimQueueOld(SendNoticeDto sendNoticeDto) {
try {
//获取连接
Connection connection = RabbitmqUtils.getConnection();
if (connection != null) {
//获取通道
Channel channel = connection.createChannel();
//通过通道声明交换机 交换机类型为direct
channel.exchangeDeclare(EXCHANGE_NAME,QUEUE_TYPE);
String key = String.valueOf(SecurityUser.getUserId());
String queueName = QUEUE_NAME + String.valueOf(SecurityUser.getUserId());
System.out.println("key:"+ key);
//定义一个ttl队列
//定义一个map集合存放参数
Map<String,Object> arguments = new HashMap<>();
// 这里的key参数需要从web图形化界面中获得,后面一定要是整形,单位是毫秒
//设置消息有效期,消息到期未被消费,就胡进入到死信交换机,并由死信交换机路由到死信队列
// arguments.put("x-message-ttl", 10000);
/**
* 设置消息发送到队列中在被丢弃之前可以存活的时间,单位:毫秒
*/
// arguments.put("x-message-ttl", 10000);
/**
* 设置一个队列多长时间未被使用将会被删除,单位:毫秒
*/
arguments.put("x-expires", 10000);
/**
* queue中可以存储处于ready状态的消息数量
*/
// arguments.put("x-max-length", 6);
/**
* queue中可以存储处于ready状态的消息占用的内存空间
*/
// arguments.put("x-max-length-bytes", 1024);
/**
* queue溢出行为,这将决定当队列达到设置的最大长度或者最大的存储空间时发送到消息队列的消息的处理方式;
* 有效的值是:drop-head(删除queue头部的消息)、reject-publish(拒绝发送来的消息)、reject-publish-dlx(拒绝发送消息到死信交换器)
* 类型为quorum 的queue只支持drop-head;
*/
arguments.put("x-overflow", "reject-publish");
/**
* 死信交换器,消息被拒绝或过期时将会重新发送到的交换器
*/
arguments.put("x-dead-letter-exchange", MY_DLX_DIRECT_EXCHANGE_NAME);
/**
* 当消息是死信时使用的可选替换路由
*/
arguments.put("x-dead-letter-routing-key", MY_DLX_DIRECT_KEY);
// arguments.put(queueName, 10000);
//声明队列
channel.queueDeclare(queueName, true, false, false, arguments);
// channel.queueDeclare(queueName, true, false, false, null);
//基于route key绑定队列和交换机
channel.queueBind(queueName, EXCHANGE_NAME, key);
}
} catch (IOException e) {
e.printStackTrace();
}
return new Result();
}
/**
* 创建队列
*/
@Override
public void claimQueue (String userId) {
try {
//获取连接
Connection connection = RabbitmqUtils.getConnection();
if (connection != null) {
//获取通道
Channel channel = connection.createChannel();
//通过通道声明交换机 交换机类型为direct
channel.exchangeDeclare(EXCHANGE_NAME,QUEUE_TYPE);
String key = userId;
System.out.println("key:"+ key);
//声明队列
String queueName = QUEUE_NAME + key;
channel.queueDeclare(queueName, true, false, false, null);
// //基于route key绑定队列和交换机
channel.queueBind(queueName,EXCHANGE_NAME, key);
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 删除队列
* @param userId
*/
@Override
public void deleteClaimQueue(String userId) {
try {
//获取连接
Connection connection = RabbitmqUtils.getConnection();
if (connection != null) {
//获取通道
Channel channel = connection.createChannel();
String queueName = QUEUE_NAME + userId;
channel.queueDelete(queueName);
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 消费队列
* @param userId
*/
public void consumptionClaimQueue(String userId) {
try {
//获取连接
Connection connection = RabbitmqUtils.getConnection();
if (connection != null) {
//获取通道
Channel channel = connection.createChannel();
//通过通道声明交换机 交换机类型为direct
channel.exchangeDeclare(EXCHANGE_NAME,QUEUE_TYPE);
String key = userId;
String queueName = QUEUE_NAME+userId;
//基于route key绑定队列和交换机
channel.queueBind(queueName,EXCHANGE_NAME, key);
// //消息到达回调函数
// DeliverCallback deliverCallback = (consumerTag, message)->{
// WebSocket.sendMessageToUserId(new String(message.getBody(), StandardCharsets.UTF_8),key);
// System.out.println("消息已经接收到ClaimQueue:"+new String(message.getBody(), StandardCharsets.UTF_8));
// };
// CancelCallback cancelCallback = (consumerTag)->{
// System.out.println("消息发送被中断!");
// };
// channel.basicConsume(queueName,true,deliverCallback,cancelCallback);
//消费消息
channel.basicConsume(queueName,true,new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
WebSocket.sendMessageToUserId(new String(body),key);
System.out.println("消费消息Consumer1:"+new String(body));
}
});
}
} catch (IOException e) {
e.printStackTrace();
}
}
vue 监听消息队列 参考例子
//1、首先安装stomp
yarn add stompjs
yarn add sockjs-client
//2、引入
import Stomp from 'stompjs';
let client;
//mpData为传过来的参数。里面包含基本的用户名,密码,主机,超时时间等等
export function MqMessage(mpData) {
client = Stomp.client(mpData.url);
let value = null;
var headers = {
login: mpData.username,
passcode: mpData.userpwd,
//虚拟主机,默认“/”
host: mpData.vhost,
// 'accept-version': '1.1,1.0',
// 'heart-beat': '100000,10000',
// 'client-id': 'my-client-id'
};
//创建连接,放入连接成功和失败回调函数
client.connect(headers, onConnected, onFailed);
function onConnected(frame) {
// console.log('Connected: ' + frame);
//绑定交换机exchange_pushmsg是交换机的名字rk_pushmsg是绑定的路由key
//如果不用通过交换机发则如下直接写队列名称就行
var exchange = mpData.queue;
//创建随机队列用上面的路由key绑定交换机,放入收到消息后的回调函数和失败的回调函数
//是交换机把下面/queue/改为/exchange/
client.subscribe('/queue/' + exchange, responseCallback, {
ack: 'client',
'x-message-ttl': mpData.args['x-message-ttl'], //这个为我的超时时间
durable: true,
});
// console.log(frame);
}
function onFailed(frame) {
// console.log('Failed: ' + frame);
if (client.connected) {
client.disconnect(function () {
client.connect(headers, onConnected, onFailed);
});
}
else {
client.connect(headers, onConnected, onFailed);
}
}
function responseCallback(frame) {
value = frame.body;
// console.log('得到的消息 msg=>' + frame.body);
// console.log(frame);
//接收到服务器推送消息,向服务器定义的接收消息routekey路由rk_recivemsg发送确认消息
frame.ack();
}
// return value;
}
// 断开连接
export function DisMqMessage() {
try {
if (client.connected) {
client.disconnect();
}
} catch (e) {}
}
##vue测试链接,上面代码队列创建时配置多种类型可以用来参考
const adress = "ws://ip:端口/ws";
const client = Stomp.client(adress);
const failed = (err: any) => {
console.log(err);
};
const getMessage = (data: any) => {
console.log(data);
data.ack();
};
client.connect(
"应户名",
"密码",
(res: any) => {
const topic = "/queue/队列名称";
client.subscribe(topic, getMessage, { ack: "client" });
console.log(res, "success");
},
failed
);