RabbitMQ 几种工作模式---通用工具类,配置类
转自图灵-白起
几种工作模式供用到两个jar:
<dependency> <groupId>com.rabbitmq</groupId> <artifactId>amqp-client</artifactId> <version>5.3.0</version> </dependency> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.5</version> </dependency>
获取RabbitMQ连接类:
package com..utils; import com.rabbitmq.client.Connection; import com.rabbitmq.client.ConnectionFactory; public class RabbitUtils { private static ConnectionFactory connectionFactory = new ConnectionFactory(); static { connectionFactory.setHost("127.0.0.1"); connectionFactory.setPort(5672);//5672是RabbitMQ的默认端口号 // connectionFactory.setUsername("guest"); // connectionFactory.setPassword("guest"); connectionFactory.setUsername("admin"); connectionFactory.setPassword("admin"); // connectionFactory.setVirtualHost("/admin"); } public static Connection getConnection(){ Connection conn = null; try { conn = connectionFactory.newConnection(); return conn; } catch (Exception e) { throw new RuntimeException(e); } } }
demo中用到的 队列 和 交换机 声明类:
package com.wondersgroup.utils;
public class RabbitConstant {
//队列
public static final String QUEUE_SMS = "sms";
public static final String QUEUE_SINA = "sina";
public static final String QUEUE_BAIDU = "baidu";
public static final String EXCHANGE_WEATHER = "weather";
public static final String QUEUE_HELLOWORLD = "helloworld";
//交换机
public static final String EXCHANGE_WEATHER_TOPIC = "weather_topic";
public static final String EXCHANGE_WEATHER_ROUTING = "weather_routing";
}