一、Maven依赖添加 <!-- rabbitmq相关依赖 --><dependency><groupId>com.rabbitmq</groupId><artifactId>amqp-client</artifactId><version>3.0.4</version></dependency>
二、七种工作模式的JAVA实例1、简单模式最简单的一个消费者和一个生产者模式,生产者生成消息,消费者监听消息,若是消费者监听到它所需要的消息,就会消费该消息,这种消息是次性的,被消费了就没有了 。
文章插图
1.1.1、EasyRecv.java
import com.rabbitmq.client.Channel;import com.rabbitmq.client.Connection;import com.rabbitmq.client.ConnectionFactory;import com.rabbitmq.client.QueueingConsumer;public class EasyRecv {//队列名称private final static String QUEUE_NAME = "hello world";public static void main(String[] argv) throws java.io.IOException,java.lang.InterruptedException {//打开连接和创建频道,与发送端一样ConnectionFactory factory = new ConnectionFactory();//设置RabbitMQ所在主机ip或者主机名factory.setHost("127.0.0.1");Connection connection = factory.newConnection();Channel channel = connection.createChannel();//声明队列,主要为了防止消息接收者先运行此程序,队列还不存在时创建队列 。/*** 队列名* 是否持久化*是否排外即只允许该channel访问该队列一般等于true的话用于一个队列只能有一个消费者来消费的场景*是否自动删除消费完删除*其他属性**/channel.queueDeclare(QUEUE_NAME, false, false, false, null);System.out.println("Waiting for messages. To exit press CTRL+C");//创建队列消费者QueueingConsumer consumer = new QueueingConsumer(channel);//指定消费队列/*** 队列名* 其他属性路由* 消息body*/channel.basicConsume(QUEUE_NAME, true, consumer);while (true){//nextDelivery是一个阻塞方法(内部实现其实是阻塞队列的take方法)QueueingConsumer.Delivery delivery = consumer.nextDelivery();String message = new String(delivery.getBody());System.out.println("Received '" + message + "'");}}}
1.1.2、EasySend.javaimport com.rabbitmq.client.Channel;import com.rabbitmq.client.Connection;import com.rabbitmq.client.ConnectionFactory;import java.util.Scanner;public class EasySend {//队列名称private final static String QUEUE_NAME = "hello world";public static void main(String[] argv) throws java.io.IOException{/*** 创建连接连接到MabbitMQ*/ConnectionFactory factory = new ConnectionFactory();//设置MabbitMQ所在主机ip或者主机名factory.setHost("127.0.0.1");while (true){//创建一个连接Connection connection = factory.newConnection();//创建一个频道Channel channel = connection.createChannel();//指定一个队列channel.queueDeclare(QUEUE_NAME, false, false, false, null);//发送的消息Scanner scanner = new Scanner(System.in);String ms = scanner.nextLine();//String message = "hello world!";//往队列中发出一条消息channel.basicPublish("", QUEUE_NAME, null, ms.getBytes());System.out.println("Sent '" + ms + "'");//关闭频道和连接channel.close();connection.close();}}
以上两个已经可以进行通信了,下面同样是简单的实例,但是我们可以看到在代码层面上,连接的代码都是一样的,所以我们可以创建一个连接的工具类 。1.2.1、RabbitmqConnectionUtil .java
import com.rabbitmq.client.Connection;import com.rabbitmq.client.ConnectionFactory;import java.io.IOException;publicclass RabbitmqConnectionUtil {public static Connection getConnection() throws IOException {//连接工厂ConnectionFactory factory = new ConnectionFactory();factory.setHost("localhost");//连接5672端口注意15672为工具界面端口25672为集群端口factory.setPort(5672);//factory.setVirtualHost("/xxxxx");// factory.setUsername("xxxxxx");// factory.setPassword("123456");//获取连接Connection connection = factory.newConnection();return connection;}}
1.2.2、UtilSend.javaimport com.rabbitmq.client.Channel;import com.rabbitmq.client.Connection;import top.san.RabbitMq.util.RabbitmqConnectionUtil;import java.io.IOException;public class UtilSend {private final static String QUEUE_NAME = "UtilConn";public static void main(String[] args) throws IOException {Connection connection = RabbitmqConnectionUtil.getConnection();//创建通道Channel channel = connection.createChannel();//声明队列channel.queueDeclare(QUEUE_NAME, false, false, false, null);//消息内容String message = "这里是lbw广场";channel.basicPublish("", QUEUE_NAME,null,message.getBytes());System.out.println("[x]Sent '"+message + "'");//最后关闭通关和连接channel.close();connection.close();}}
1.2.3、UtilRecv.javaimport com.rabbitmq.client.Channel;import com.rabbitmq.client.Connection;import com.rabbitmq.client.QueueingConsumer;import top.san.RabbitMq.util.RabbitmqConnectionUtil;import java.io.IOException;public class UtilRecv {private final static String QUEUE_NAME = "UtilConn";public static void main(String[] args) throws IOException, InterruptedException {Connection connection = null;connection = RabbitmqConnectionUtil.getConnection();//创建通道Channel channel = connection.createChannel();//声明队列channel.queueDeclare(QUEUE_NAME, false, false, false, null);QueueingConsumer queueingConsumer = new QueueingConsumer(channel);channel.basicConsume(QUEUE_NAME,true,queueingConsumer);while(true){//该方法会阻塞QueueingConsumer.Delivery delivery = queueingConsumer.nextDelivery();String message = new String(delivery.getBody());System.out.println("[x] Received '"+message+"'");}}}
推荐阅读
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 计算机专业学什么语言好找工作?
- 残疾人|本科没拿到学位证对以后找工作有多大影响?
- 鲜虾萝卜饼的做法
- 没有工作的一年|《没有工作的一年》赚钱还是躺平?小雨租房也乐观,却源于这一点
- 招聘|考研究生反而不好找工作,日本人这点很认真,大家觉得应该学吗?
- 教师|想上岸入编找到理想工作,该如何选择单位?随我一起来看看吧!
- 犀角化毒丸怎么样
- 戴尔Precision 5750移动工作站深度测试
- 教你快速清除Excel工作表保护密码
- excel批量拆分工作簿,用VBA一键拆分,把数据分解到N个工作簿