rabbitmq client模式

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");
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值