<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      異步處理與消息隊列總結

      在Spring Boot Web項目中,異步處理和消息隊列是提高系統性能和可擴展性的重要技術。異步處理可以讓應用在處理耗時操作時不會阻塞主線程,而消息隊列則可以實現系統間的解耦和流量削峰。

      首先,我們來看看Spring Boot中的異步處理。Spring提供了@Async注解來支持方法的異步執行。要使用異步處理,需要在主應用類上添加@EnableAsync注解來啟用異步支持。

      以下是一個簡單的異步處理示例:package com.example.demo.service.impl;

      import com.example.demo.service.NotificationService;
      import org.slf4j.Logger;
      import org.slf4j.LoggerFactory;
      import org.springframework.scheduling.annotation.Async;
      import org.springframework.stereotype.Service;

      import java.util.concurrent.CompletableFuture;

      @Service
      public class NotificationServiceImpl implements NotificationService {

      private static final Logger logger = LoggerFactory.getLogger(NotificationServiceImpl.class);
      
      @Override
      @Async("asyncExecutor")
      public CompletableFuture<String> sendEmail(String to, String subject, String content) {
          logger.info("Sending email to: {}", to);
          
          // 模擬耗時的郵件發送操作
          try {
              Thread.sleep(2000);
          } catch (InterruptedException e) {
              Thread.currentThread().interrupt();
          }
          
          logger.info("Email sent to: {}", to);
          return CompletableFuture.completedFuture("Email sent successfully to " + to);
      }
      

      }
      需要配置一個線程池來執行異步任務:package com.example.demo.config;

      import org.springframework.context.annotation.Bean;
      import org.springframework.context.annotation.Configuration;
      import org.springframework.scheduling.annotation.EnableAsync;
      import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

      import java.util.concurrent.Executor;

      @Configuration
      @EnableAsync
      public class AsyncConfig {

      @Bean(name = "asyncExecutor")
      public Executor asyncExecutor() {
          ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
          executor.setCorePoolSize(5);
          executor.setMaxPoolSize(10);
          executor.setQueueCapacity(25);
          executor.setThreadNamePrefix("AsyncThread-");
          executor.initialize();
          return executor;
      }
      

      }
      在控制器中調用異步方法:package com.example.demo.controller;

      import com.example.demo.service.NotificationService;
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.web.bind.annotation.GetMapping;
      import org.springframework.web.bind.annotation.PathVariable;
      import org.springframework.web.bind.annotation.RestController;

      import java.util.concurrent.CompletableFuture;

      @RestController
      @RequestMapping("/api/notifications")
      public class NotificationController {

      @Autowired
      private NotificationService notificationService;
      
      @GetMapping("/email/{to}")
      public CompletableFuture<String> sendEmail(@PathVariable String to) {
          return notificationService.sendEmail(to, "Test Subject", "Test Content");
      }
      

      }
      接下來,我們看看消息隊列的使用。RabbitMQ是一個流行的消息隊列中間件,Spring Boot提供了spring-boot-starter-amqp來簡化與RabbitMQ的集成。

      首先,添加RabbitMQ的依賴:
      org.springframework.boot
      spring-boot-starter-amqp

      配置RabbitMQ連接信息:spring:
      rabbitmq:
      host: localhost
      port: 5672
      username: guest
      password: guest
      創建消息隊列配置類:package com.example.demo.config;

      import org.springframework.amqp.core.Queue;
      import org.springframework.context.annotation.Bean;
      import org.springframework.context.annotation.Configuration;

      @Configuration
      public class RabbitMQConfig {

      @Bean
      public Queue notificationQueue() {
          return new Queue("notification.queue", true);
      }
      

      }
      創建消息生產者:package com.example.demo.service.impl;

      import com.example.demo.service.NotificationService;
      import org.springframework.amqp.rabbit.core.RabbitTemplate;
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.stereotype.Service;

      @Service
      public class NotificationServiceImpl implements NotificationService {

      private static final String QUEUE_NAME = "notification.queue";
      
      @Autowired
      private RabbitTemplate rabbitTemplate;
      
      @Override
      public void sendNotification(String message) {
          rabbitTemplate.convertAndSend(QUEUE_NAME, message);
          System.out.println("Message sent to the queue: " + message);
      }
      

      }
      創建消息消費者:package com.example.demo.service.impl;

      import org.springframework.amqp.rabbit.annotation.RabbitListener;
      import org.springframework.stereotype.Service;

      @Service
      public class NotificationConsumer {

      @RabbitListener(queues = "notification.queue")
      public void receiveMessage(String message) {
          System.out.println("Received message: " + message);
          // 處理消息
          processMessage(message);
      }
      
      private void processMessage(String message) {
          // 模擬處理消息的耗時操作
          try {
              Thread.sleep(1000);
          } catch (InterruptedException e) {
              Thread.currentThread().interrupt();
          }
          System.out.println("Message processed: " + message);
      }
      

      }
      在控制器中調用消息生產者:package com.example.demo.controller;

      import com.example.demo.service.NotificationService;
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.web.bind.annotation.GetMapping;
      import org.springframework.web.bind.annotation.PathVariable;
      import org.springframework.web.bind.annotation.RestController;

      @RestController
      @RequestMapping("/api/notifications")
      public class NotificationController {

      @Autowired
      private NotificationService notificationService;
      
      @GetMapping("/send/{message}")
      public String sendNotification(@PathVariable String message) {
          notificationService.sendNotification(message);
          return "Notification sent successfully";
      }
      

      }
      除了RabbitMQ,Spring Boot還支持其他消息隊列,如Kafka、ActiveMQ等。對于Kafka,需要添加以下依賴:
      org.springframework.boot
      spring-boot-starter-kafka

      配置Kafka連接信息:spring:
      kafka:
      bootstrap-servers: localhost:9092
      consumer:
      group-id: my-group
      auto-offset-reset: earliest
      創建Kafka生產者:package com.example.demo.service.impl;

      import com.example.demo.service.NotificationService;
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.kafka.core.KafkaTemplate;
      import org.springframework.stereotype.Service;

      @Service
      public class NotificationServiceImpl implements NotificationService {

      private static final String TOPIC = "notification.topic";
      
      @Autowired
      private KafkaTemplate<String, String> kafkaTemplate;
      
      @Override
      public void sendNotification(String message) {
          kafkaTemplate.send(TOPIC, message);
          System.out.println("Message sent to Kafka: " + message);
      }
      

      }
      創建Kafka消費者:package com.example.demo.service.impl;

      import org.springframework.kafka.annotation.KafkaListener;
      import org.springframework.stereotype.Service;

      @Service
      public class NotificationConsumer {

      @KafkaListener(topics = "notification.topic", groupId = "my-group")
      public void listen(String message) {
          System.out.println("Received message from Kafka: " + message);
          // 處理消息
          processMessage(message);
      }
      
      private void processMessage(String message) {
          // 處理消息的邏輯
      }
      

      }
      異步處理和消息隊列是構建高性能、高可擴展性系統的重要技術。在Spring Boot中,通過@Async注解和簡單的配置,我們可以輕松實現方法的異步執行;通過集成RabbitMQ或Kafka等消息隊列中間件,我們可以實現系統間的解耦和流量削峰。在實際項目中,應根據具體需求選擇合適的異步處理方式和消息隊列中間件。

      posted @ 2025-05-28 14:40  霸王雞  閱讀(10)  評論(0)    收藏  舉報
      主站蜘蛛池模板: 少妇高潮喷水正在播放| 国精品午夜福利视频| 人妻少妇久久中文字幕| 翘臀少妇被扒开屁股日出水爆乳| 国产高清吹潮免费视频| 国产不卡精品视频男人的天堂| 好姑娘6电影在线观看| 国产中文字幕精品视频| 国产精品一在线观看| 蒲城县| 99www久久综合久久爱com| 欧洲精品码一区二区三区| 在线免费观看亚洲天堂av| 人妻丝袜AV中文系列先锋影音 | 久久99精品久久久久久9| 99久久综合精品五月天| 午夜欧美精品久久久久久久| 在线看片免费人成视频久网| 亚州AV无码乱码精品国产| 樱花草视频www日本韩国| 免费人妻无码不卡中文18禁| 少妇粗大进出白浆嘿嘿视频| 精品人妻码一区二区三区| 国产高颜值不卡一区二区| 久久99精品久久久大学生| 成在线人永久免费视频播放 | 无码伊人66久久大杳蕉网站谷歌| 国产成人精品一区二三区| 香蕉在线精品一区二区| 国产小受被做到哭咬床单GV| 人妻无码中文专区久久app| 男人进女人下部全黄大色视频| 深夜视频国产在线观看| AV无码不卡一区二区三区| 在国产线视频A在线视频| 女人被狂c躁到高潮视频| 蜜臀av一区二区三区日韩| 99riav国产精品视频| 国产精品成人无码久久久| 亚洲热线99精品视频| a级国产乱理伦片在线观看al |