小编给大家分享一下RabbitMQ有哪些工作模式,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!
RabbitMQ五种工作模式在SpringBoot环境下做的代码测试,RabbitMQ的包是用SpringBoot的starter-amqp包引入的。
1、简单队列
一个生产者对应一个消费者!!!
1、pom文件
SpringBoot导入rabbitmq 启动包
2、工具类
/** * 〈简述〉* 〈连接RabbitMQ的工具类〉 * * @create 2020/7/1 * @since 1.0.0 */public class ConnectionUtil { public static Connection getConnection() throws Exception { return getConnection(new Properties()); } private static Connection getConnection(Properties properties) throws Exception { return getConnection(properties.getHost(), properties.getPort(), properties.getvHost(), properties.getUserName(), properties.getPassWord()); } public static Connection getConnection(String host, int port, String vHost, String userName, String passWord) throws Exception { //1、定义连接工厂 ConnectionFactory factory = new ConnectionFactory(); //2、设置服务器地址 factory.setHost(host); //3、设置端口 factory.setPort(port); //4、设置虚拟主机、用户名、密码 factory.setVirtualHost(vHost); factory.setUsername(userName); factory.setPassword(passWord); //5、通过连接工厂获取连接 Connection connection = factory.newConnection(); return connection; } public static class Properties implements Serializable { String host = "192.168.1.103"; int port = 5672; String vHost = "/"; String userName = "guest"; String passWord = "guest"; public Properties() { } public Properties(String host, int port, String vHost, String userName, String passWord) { this.host = host; this.port = port; this.vHost = vHost; this.userName = userName; this.passWord = passWord; } public String getHost() { return host; } public Properties setHost(String host) { this.host = host; return self(); } public int getPort() { return port; } public Properties setPort(int port) { this.port = port; return self(); } public String getvHost() { return vHost; } public Properties setvHost(String vHost) { this.vHost = vHost; return self(); } public String getUserName() { return userName; } public Properties setUserName(String userName) { this.userName = userName; return self(); } public String getPassWord() { return passWord; } public Properties setPassWord(String passWord) { this.passWord = passWord; return self(); } private Properties self(){ return this; } } }
郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。