package com.elane.springcloud.rabbit; import com.elane.springcloud.init.BlockQueue; import com.elane.springcloud.listener.ContextListener; import com.rabbitmq.client.Channel; import com.rabbitmq.client.GetResponse; import org.springframework.amqp.core.AmqpAdmin; import org.springframework.amqp.rabbit.core.ChannelCallback; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.annotation.Autowired; import java.io.IOException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import static com.elane.springcloud.init.BlockQueue.QUEUE; public class RecvMsgThread { // @Resource // private ThreadNum threadNum; @Autowired AmqpAdmin rabbitAdmin; @Autowired RabbitTemplate rabbitTemplate; public void startThread() { startRecvThread(); } void startRecvThread() { System.out.println("日志同步服务启动接收消息线程-----------------"); ExecutorService threadPool = Executors.newFixedThreadPool(10); System.out.println("日志同步服务进入消息循环提取中、、、、、"); Runnable recver = () -> { try { //此类属于启动加载类 会有线程启动调用此类 所以再取消息之前 等待项目启动OK 保证mq能够连接上 必须线程中执行方法 否则卡死等待启动 Thread.sleep(15000); } catch (InterruptedException e) { e.printStackTrace(); } while (true/*!ContextListener.isDestory*/) { if (ContextListener.isDestory) { System.out.println("服务器已经关闭了,关闭取消息线程"); threadPool.shutdown(); Thread.currentThread().interrupt(); return; } try { System.out.println("当前内存队列数量"+QUEUE.size()); if(BlockQueue.QUEUE_SIZE>QUEUE.size()){ //当默认设置的队列长度大于当前队列的数据长度 即可进行从mq取消息 否则线程持续等待 此处为变量 processQueue("springcloudsleuth"); //去指定的队列取消息 }else{ Thread.sleep(2000); } } catch (Exception e) { e.printStackTrace(); // Logs.Errorlog("resource分发线程出错", e); } } }; for (int i = 1; i <= 2; i++) { threadPool.execute(recver); } } public static class ChannelCallbackImpl implements ChannelCallback<String> { private String queueName; public ChannelCallbackImpl(String queueName){ this.queueName = queueName; } @Override public String doInRabbit(Channel channel) { GetResponse message = null; try { message = channel.basicGet(queueName, false); // System.out.println(message); if(message == null){ System.out.println("程序延时----------"); Thread.sleep(2000); }else{ //不进行判断将卡死在消息流转换出 // String response=new String(message.getBody()); // System.out.println("取到的消息串、、、、、"+response); QUEUE.put(message.getBody()); //放入本地缓存队列 队列形式为 字节流 byte [] 不做转换 到读取时直接采用jackson转jsonnode 取节点封装 channel.basicAck(message.getEnvelope().getDeliveryTag(), false); //给服务器确认消息,已经收到 } return ""; } catch(Exception e) { try { System.out.println("进入消息异常---------------"); // channel.basicNack(message.getEnvelope().getDeliveryTag(), false, true); //出现问题 此设置可以返回原消息队列 进行重置发送 channel.basicReject(message.getEnvelope().getDeliveryTag(), true); //拒绝消息 任务失败 其它情况可以拒绝此消息 } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } return null; } } public void processQueue(String queueName) { rabbitTemplate.execute(new ChannelCallbackImpl(queueName)); } }
基于Springboot到Rabbitmq实现手动PUT消息到指定大小的内存队列 队列满即暂停取消息
最新推荐文章于 2023-06-27 12:00:04 发布