于紫祥_1901
2020-12-01 4650388e8954ab6291fa0effb439b6ed3eb6eaa7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package com.moral.util;
 
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
 
public class RabbitMQUtils {
    private  static ConnectionFactory factory ;
 
    static{
        factory = new ConnectionFactory();
        factory.setHost("47.96.15.25");//设置连接地址
        factory.setPort(5672);//设置端口
        factory.setUsername("guest");
        factory.setPassword("guest_pass");
        factory.setVirtualHost("/");//设置虚拟主机
    }
 
    public static Connection getConnection(){
        try{
            return factory.newConnection();
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
 
    public static void closeConnectionChannel(Connection connection, Channel channel){
        try{
            if(channel!=null)
                channel.close();
            if(connection!=null)
                connection.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
 
    public static void closeChannel( Channel channel){
        try{
            if(channel!=null)
                channel.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}