package mq;
import com.rabbitmq.client.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;
@Slf4j
@Component
public class MqConnectionUtil implements ApplicationRunner {
private static Map<String, Connection> connectionMap = new HashMap<>();
private static Map<String, Channel> channelMap = new HashMap<>();
public static Connection getConnection(String host, int port, String vhost, String username,String password) throws Exception {
//定义连接工厂
ConnectionFactory factory = new ConnectionFactory();
//设置服务地址
factory.setHost(host);
//端口
factory.setPort(port);
//设置账号信息,用户名、密码、vhost
factory.setVirtualHost(vhost);//设置虚拟机,一个mq服务可以设置多个虚拟机,每个虚拟机就相当于一个独立的mq
factory.setUsername(username);
factory.setPassword(password);
// 通过工厂获取连接
Connection connection = factory.newConnection();
log.info("连接MQ成功..");
return connection;
}
public static void runConsumer(String host, int port, String vhost, String username,String password , String queue){
try {
Connection connection = getConnection(host, port, vhost, username, password);
Channel channel = connection.createChannel();
channel.queueDeclare(queue, true, false, false, null);
//channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "");
DefaultConsumer consumer = new DefaultConsumer(channel) {
// 获取消息,并且处理,这个方法类似事件监听,如果有消息的时候,会被自动调用
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body){
try {
// body 即消息体
String msg = new String(body);
System.out.println(" [x] received1 : " + msg + "!");
channel.basicAck(envelope.getDeliveryTag(), false);
}catch (Exception e){
e.printStackTrace();
try {
//第三个参数true,表示这个消费会重新进入队列
channel.basicNack(envelope.getDeliveryTag(), false, true);
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
}
};
// 监听队列,第二个参数false,手动进行ACK
channel.basicConsume(queue, false, consumer);
//channel.close();
//connection.close();
}catch (Exception e){
e.printStackTrace();
}
}
public static void runConsumer2(String host, int port, String vhost, String username,String password, String queue){
try {
Connection connection = getConnection(host, port, vhost, username, password);
Channel channel = connection.createChannel();
channel.queueDeclare(queue, true, false, false, null);
//channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "");
DefaultConsumer consumer = new DefaultConsumer(channel) {
// 获取消息,并且处理,这个方法类似事件监听,如果有消息的时候,会被自动调用
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body){
try {
// body 即消息体
String msg = new String(body);
System.out.println(" [x] received2 : " + msg + "!");
channel.basicAck(envelope.getDeliveryTag(), false);
}catch (Exception e){
e.printStackTrace();
try {
channel.basicNack(envelope.getDeliveryTag(), false, true);
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
}
};
// 监听队列,第二个参数false,手动进行ACK
channel.basicConsume(queue, false, consumer);
//channel.close();
//connection.close();
}catch (Exception e){
e.printStackTrace();
}
}
@Override
public void run(ApplicationArguments args) throws Exception {
runConsumer("1.1.1.1", 5672, "/", "admin", "admin" ,"queue");
runConsumer2("1.1.1.1", 5672, "/", "admin", "admin" ,"queue");
}
}
rabbitmq client模式
于 2024-11-12 14:55:51 首次发布