一、RabbitMQProducer
1 package com.miaoshaProject.mq; 2 3 import com.rabbitmq.client.Channel; 4 import com.rabbitmq.client.Connection; 5 6 import java.io.IOException; 7 import java.util.concurrent.TimeoutException; 8 9 /** 10 * @Author wangshuo 11 * @Date 2022/5/6, 16:57 12 * Please add a comment 13 */ 14 public class RabbitMQProducer extends Thread { 15 16 private static final String QUEUE_NAME = "queue1"; 17 18 public static void main(String[] args) throws IOException, TimeoutException, InterruptedException { 19 20 //demo1 21 //test1(); 22 23 //使用消息确认机制 24 //test2(); 25 26 //使用事务机制 27 //test3(); 28 29 //开启五个线程分别生产100条消息 30 test4(); 31 } 32 33 //继承Thread类重写run方法 34 @Override 35 public synchronized void run() { 36 37 try { 38 //创建连接 39 Connection connection = RabbitMQConnection.getConnection(); 40 //创建通道 41 Channel channel = connection.createChannel(); 42 channel.txSelect(); 43 try { 44 //发送一百条消息 45 for (int i = 0; i < 100; i++) { 46 47 String msg = Thread.currentThread().getName() + "发送消息:消息测试" + i; 48 System.out.println(msg); 49 channel.basicPublish("", QUEUE_NAME, null, msg.getBytes()); 50 } 51 channel.txCommit(); 52 } catch (Exception e) { 53 //打印错误信息,回滚 54 System.out.println(e); 55 channel.txRollback(); 56 } finally { 57 //关闭通道和连接 58 channel.close(); 59 connection.close(); 60 } 61 } catch (IOException | TimeoutException e) { 62 e.printStackTrace(); 63 } 64 } 65 66 private static void test4() throws IOException, TimeoutException { 67 68 RabbitMQProducer rmqp = new RabbitMQProducer(); 69 new Thread(rmqp,"线程1").start(); 70 new Thread(rmqp,"线程2").start(); 71 new Thread(rmqp,"线程3").start(); 72 new Thread(rmqp,"线程4").start(); 73 new Thread(rmqp,"线程5").start(); 74 } 75 76 private static void test3() throws IOException, TimeoutException { 77 78 //创建连接 79 Connection connection = RabbitMQConnection.getConnection(); 80 //创建通道 81 Channel channel = connection.createChannel(); 82 channel.txSelect(); 83 try { 84 //发送消息 85 String msg = "消息测试1"; 86 channel.basicPublish("", QUEUE_NAME, null, msg.getBytes()); 87 channel.txCommit(); 88 } catch (Exception e) { 89 //打印错误信息,回滚 90 System.out.println(e); 91 channel.txRollback(); 92 } finally { 93 //关闭通道和连接 94 channel.close(); 95 connection.close(); 96 } 97 } 98 99 private static void test2() throws IOException, TimeoutException, InterruptedException { 100 101 //创建连接 102 Connection connection = RabbitMQConnection.getConnection(); 103 //创建通道 104 Channel channel = connection.createChannel(); 105 String msg = "消息测试2"; 106 //Select Confirms 107 channel.confirmSelect(); 108 channel.basicPublish("", QUEUE_NAME, null, msg.getBytes()); 109 //消息确认 110 boolean b = channel.waitForConfirms(); 111 if (b) 112 System.out.println("投递成功"); 113 else 114 System.out.println("投递失败");//抛出错误 115 //关闭通道和连接 116 channel.close(); 117 connection.close(); 118 } 119 120 private static void test1() throws IOException, TimeoutException { 121 //创建连接 122 Connection connection = RabbitMQConnection.getConnection(); 123 //创建通道 124 Channel channel = connection.createChannel(); 125 String msg = "消息测试1"; 126 channel.basicPublish("", QUEUE_NAME, null, msg.getBytes()); 127 //关闭通道和连接 128 channel.close(); 129 connection.close(); 130 } 131 }
二、RabbitMQConsumer
1 package com.miaoshaProject.mq; 2 3 import com.rabbitmq.client.*; 4 5 import java.io.IOException; 6 import java.util.concurrent.TimeoutException; 7 8 /** 9 * @Author wangshuo 10 * @Date 2022/5/6, 17:05 11 * Please add a comment 12 */ 13 public class RabbitMQConsumer { 14 15 private static final String QUEUE_NAME = "queue1"; 16 17 public static void main(String[] args) throws IOException, TimeoutException { 18 19 Thread thread1 = new Thread(new Runnable(){ 20 21 @Override 22 public synchronized void run() { 23 try { 24 //开启消费者 25 consumer("线程一"); 26 } catch (IOException | TimeoutException e) { 27 e.printStackTrace(); 28 } 29 } 30 }); 31 Thread thread2 = new Thread(new Runnable(){ 32 33 @Override 34 public synchronized void run() { 35 try { 36 //开启消费者 37 consumer("线程二"); 38 } catch (IOException | TimeoutException e) { 39 e.printStackTrace(); 40 } 41 } 42 }); 43 thread1.start(); 44 thread2.start(); 45 } 46 47 private static void consumer(String a) throws IOException, TimeoutException { 48 //获取连接 49 Connection connection = RabbitMQConnection.getConnection(); 50 //创建通道 51 Channel channel = connection.createChannel(); 52 //自定义一个Consumer 53 DefaultConsumer defaultConsumer = new DefaultConsumer(channel) { 54 55 //重写获取后动作 56 @Override 57 public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { 58 59 String s = new String(body, "UTF-8"); 60 System.out.println(a + "消费者收到消息: " + s); 61 //消费完成,通知MQ删除消息 62 channel.basicAck(envelope.getDeliveryTag(), false); 63 } 64 }; 65 //监听队列 第二个参数autoAck-true:自动签收消息 一般我们会设置为false 66 //channel.basicConsume(QUEUE_NAME, true, defaultConsumer); 67 68 channel.basicConsume(QUEUE_NAME, false, defaultConsumer); 69 } 70 }
三、RabbitMQConsumer_Gzdl
1 package com.miaoshaProject.mq.demo_rumen; 2 3 import com.miaoshaProject.mq.util.RabbitMQConnection; 4 import com.rabbitmq.client.*; 5 6 import java.io.IOException; 7 import java.util.concurrent.TimeoutException; 8 9 /** 10 * @Author wangshuo 11 * @Date 2022/5/6, 17:05 12 * 消费者实现工作队列(一个线程每次消费三个,另一个线程每次消费一个) 13 */ 14 public class RabbitMQConsumer_Gzdl { 15 16 private static final String QUEUE_NAME = "queue1"; 17 18 public static void main(String[] args) throws IOException, TimeoutException { 19 20 Thread thread1 = new Thread(new Runnable() { 21 22 @Override 23 public synchronized void run() { 24 try { 25 //开启消费者 26 consumer("线程一", 3); 27 } catch (IOException | TimeoutException e) { 28 e.printStackTrace(); 29 } 30 } 31 }); 32 Thread thread2 = new Thread(new Runnable() { 33 34 @Override 35 public synchronized void run() { 36 try { 37 //开启消费者 38 consumer("线程二", 1); 39 } catch (IOException | TimeoutException e) { 40 e.printStackTrace(); 41 } 42 } 43 }); 44 thread1.start(); 45 thread2.start(); 46 } 47 48 private static void consumer(String a, Integer b) throws IOException, TimeoutException { 49 //获取连接 50 Connection connection = RabbitMQConnection.getConnection(); 51 //创建通道 52 Channel channel = connection.createChannel(); 53 //设置队列每次获取几条消息 54 channel.basicQos(b); 55 //自定义一个Consumer 56 DefaultConsumer defaultConsumer = new DefaultConsumer(channel) { 57 58 //重写获取后动作 59 @Override 60 public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { 61 62 String s = new String(body, "UTF-8"); 63 System.out.println(a + "消费者收到消息: " + s); 64 //消费完成,通知MQ删除消息 65 channel.basicAck(envelope.getDeliveryTag(), false); 66 } 67 }; 68 //监听队列 第二个参数autoAck-true:自动签收消息 一般我们会设置为false 69 //channel.basicConsume(QUEUE_NAME, true, defaultConsumer); 70 71 channel.basicConsume(QUEUE_NAME, false, defaultConsumer); 72 } 73 }
本文来自博客园,作者:荣慕平,转载请注明原文链接:https://www.cnblogs.com/rongmuping/articles/16241495.html
浙公网安备 33010602011771号