当前位置: 首页 > wzjs >正文

博客网站哪个权重高政务信息化建设网站

博客网站哪个权重高,政务信息化建设网站,微网站模板在线制作,开发公司经营范围惰性队列工作原理 惰性队列通过尽可能多地将消息存储到磁盘上来减少内存的使用。与传统队列相比,惰性队列不会主动将消息加载到内存中,而是尽量让消息停留在磁盘上,从而降低内存占用。尽管如此,它并不保证所有操作都是同步写入磁…

惰性队列工作原理

惰性队列通过尽可能多地将消息存储到磁盘上来减少内存的使用。与传统队列相比,惰性队列不会主动将消息加载到内存中,而是尽量让消息停留在磁盘上,从而降低内存占用。尽管如此,它并不保证所有操作都是同步写入磁盘的。这意味着消息可能会先被缓存到操作系统的缓冲区中,然后由操作系统决定何时将其真正写入磁盘。

  • 优点:适合处理大量消息且对内存压力敏感的场景。
  • 缺点:由于频繁的磁盘I/O操作,性能可能不如传统队列。

同步刷盘的概念

同步刷盘意味着每次写入操作都会等待数据完全写入磁盘后才返回确认信息。虽然这种方式提供了更强的数据持久性保证,但它也显著增加了写入操作的延迟。对于RabbitMQ而言,可以通过设置消息为持久化来增加数据的安全性,但对于极端情况下的数据安全性要求,还需要结合其他策略如调整操作系统参数或使用文件系统级别的同步写入配置。

延迟插件的工作原理

RabbitMQ本身没有内置的延迟队列功能,但可以通过安装rabbitmq_delayed_message_exchange插件实现这一功能。该插件允许创建一个自定义交换机类型,该交换机能够根据消息头中的延迟时间属性来延迟消息的传递。

在Spring Boot中集成RabbitMQ惰性队列和延迟消息

1. 项目初始化

首先,确保你的Spring Boot项目中包含必要的依赖:

<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-amqp</artifactId></dependency>
</dependencies>
2. 配置RabbitMQ连接

application.yml中配置RabbitMQ连接信息:

spring:rabbitmq:host: localhostport: 5672username: guestpassword: guest
3. 定义惰性队列

创建一个配置类来定义惰性队列:

import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import java.util.HashMap;
import java.util.Map;@Configuration
public class RabbitMqConfig {/*** 定义惰性模式的队列* @return 返回惰性队列实例*/@Beanpublic Queue lazyQueue() {Map<String, Object> args = new HashMap<>();// 设置队列为惰性模式args.put("x-queue-mode", "lazy");return new Queue("my_lazy_queue", true, false, false, args); // durable=true for queue durability}
}
4. 发送持久化消息

创建一个服务类用于发送消息,并确保消息是持久化的:

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;@Service
public class MessageSender {@Autowiredprivate RabbitTemplate rabbitTemplate;/*** 发送一条持久化消息到惰性队列* @param message 要发送的消息内容*/public void sendMessage(String message) {rabbitTemplate.convertAndSend("my_lazy_queue", message);System.out.println(" [x] Sent '" + message + "'");}
}

确保消息持久化可以在application.yml中设置如下:

spring:rabbitmq:template:exchange: ''routing-key: 'my_lazy_queue'mandatory: truepublisher-confirms: truepublisher-returns: true
5. 接收消息

创建一个监听器来接收消息:

import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;@Component
public class MessageReceiver {/*** 监听并接收来自惰性队列的消息* @param message 接收到的消息内容*/@RabbitListener(queues = "my_lazy_queue")public void receiveMessage(String message) {System.out.println(" [x] Received '" + message + "'");}
}
6. 使用延迟插件发送延迟消息

首先,在RabbitMqConfig中定义延迟交换机:

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.CustomExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import java.util.Collections;
import java.util.HashMap;
import java.util.Map;@Configuration
public class RabbitMqConfig {/*** 定义延迟交换机* @return 返回延迟交换机实例*/@Beanpublic CustomExchange delayExchange() {Map<String, Object> args = new HashMap<>();args.put("x-delayed-type", "direct");return new CustomExchange("delayed_exchange", "x-delayed-message", true, false, args);}/*** 绑定延迟队列到延迟交换机* @param delayedQueue 延迟队列* @param delayExchange 延迟交换机* @return 返回绑定实例*/@Beanpublic Binding binding(Queue delayedQueue, CustomExchange delayExchange) {return new Binding("delayed_queue", Binding.DestinationType.QUEUE, "delayed_exchange", "routing.key", Collections.emptyMap());}/*** 定义延迟队列* @return 返回延迟队列实例*/@Beanpublic Queue delayedQueue() {return new Queue("delayed_queue");}
}

然后,创建一个服务类来发送延迟消息:

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;@Service
public class DelayedMessageSender {@Autowiredprivate RabbitTemplate rabbitTemplate;/*** 发送带有延迟的消息* @param message 要发送的消息内容* @param delayTime 延迟时间(毫秒)*/public void sendDelayedMessage(String message, int delayTime) {MessagePostProcessor messagePostProcessor = message -> {message.getMessageProperties().setHeader("x-delay", delayTime);return message;};rabbitTemplate.convertAndSend("delayed_exchange", "routing.key", message, messagePostProcessor);System.out.println(" [x] Sent '" + message + "' with delay.");}
}

最后,创建一个监听器来接收延迟消息:

import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;@Component
public class DelayedMessageReceiver {/*** 监听并接收来自延迟队列的消息* @param message 接收到的消息内容*/@RabbitListener(queues = "delayed_queue")public void receiveDelayedMessage(String message) {System.out.println(" [x] Received delayed message '" + message + "'");}
}

高级特性和最佳实践

  • 发布确认机制:为了提高可靠性,可以开启发布确认机制,以确保消息确实被RabbitMQ服务器接受。

rabbitTemplate.setConfirmCallback((correlationData, ack, cause) -> {if (ack) {System.out.println("Message acknowledged");} else {System.err.println("Message not acknowledged due to: " + cause);}
});
  • 预取计数(Prefetch Count):通过设置预取计数限制每个消费者同时处理的消息数量,有助于防止消费者被过多未处理的消息压垮。

import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class RabbitMqConnectionConfig {@Beanpublic CachingConnectionFactory cachingConnectionFactory() {CachingConnectionFactory connectionFactory = new CachingConnectionFactory();connectionFactory.setHost("localhost");connectionFactory.setPort(5672);connectionFactory.setUsername("guest");connectionFactory.setPassword("guest");connectionFactory.setChannelCacheSize(25);connectionFactory.getRabbitConnectionFactory().setRequestedChannelMax(200);return connectionFactory;}
}

可以在application.yml中设置:

spring:rabbitmq:listener:simple:prefetch: 10

文章转载自:

http://LmO7p42j.gzgwn.cn
http://7DmivIp9.gzgwn.cn
http://VlKaOLDx.gzgwn.cn
http://e0QivCYC.gzgwn.cn
http://4wcCbHxv.gzgwn.cn
http://hnA89DDS.gzgwn.cn
http://e7u0goOX.gzgwn.cn
http://3u36Wwo0.gzgwn.cn
http://mBBkQfl2.gzgwn.cn
http://mjFisPNr.gzgwn.cn
http://DKTY05pY.gzgwn.cn
http://QdrDt2pW.gzgwn.cn
http://3rbUxder.gzgwn.cn
http://hgFVMpeK.gzgwn.cn
http://7ByJsBxx.gzgwn.cn
http://eGy0IrTN.gzgwn.cn
http://qfuqqqbg.gzgwn.cn
http://TQBheJvP.gzgwn.cn
http://8iW7M7ny.gzgwn.cn
http://iRgm7eGg.gzgwn.cn
http://YWN7FtVW.gzgwn.cn
http://2L25aRBd.gzgwn.cn
http://8Wc83uqy.gzgwn.cn
http://lWMiHYUG.gzgwn.cn
http://INOpd5rH.gzgwn.cn
http://UbFmVghj.gzgwn.cn
http://x5mYpzTq.gzgwn.cn
http://364Dhusm.gzgwn.cn
http://urdWob2f.gzgwn.cn
http://gkQTfPtC.gzgwn.cn
http://www.dtcms.com/wzjs/773708.html

相关文章:

  • 运城推广型网站建设网站建设undefined
  • 佛山本地网站建设精美 企业网站模板
  • 长春一大网站属于公司的网站怎么做
  • 响应式网站源码网页制作技术有哪些
  • 网页设计与网站建设完全教程上海注册公司扶持政策
  • 南京做网站牛社交模板wordpress
  • 深圳网站建设-龙华信科软件开发培训
  • 企业电子商务网站建设和一般商城网站建设经验
  • 五合一网站建设免费的网站认证
  • 网站建设属于淘宝哪种类目十大小程序开发公司
  • 网站推广的岗位要求3d在线设计网站
  • 优化大师官方网站地方农产品网站建设
  • 怎么做黑客攻击网站优质网站建设哪家好
  • 设计对网站的重要性城阳网站建设电话
  • 购买网站域名怎么做会计分录江宁区建设工程质量监督站网站
  • 成都网站建设sntuu大城 网站
  • 天津网站建设网站推广产品设计包括哪些方面
  • 绿色的医疗资讯手机网站wap模板html源码下载三站合一网站营销
  • 网站快速建设wordpress umeditor
  • 深圳 网站公司四川企业seo推广
  • 武昌做网站公司网页开发项目
  • 周口市规划建设局网站创新的成都网站建设
  • 东营人事考试信息网春秋网络优化技术团队介绍
  • 社区网站怎么做微信小程序广告投放
  • 做百度快照要先有网站吗手机上如何开发软件
  • 网站做百度口碑设计构建网站
  • 南京网站建设 小程序大连承揽营销型网站公司
  • 网站推广洛阳app开发需要用到哪些工具
  • 电商网站建设价格低手机移动端网站是什么
  • 淄博外贸网站制作东莞保安