ホームページ  >  記事  >  Java  >  Springboot での非同期タスクの分析例

Springboot での非同期タスクの分析例

WBOY
WBOY転載
2023-05-10 23:31:041107ブラウズ

非同期タスク

スタートアップ クラス

@MapperScan("com.topcheer.*.*.dao")
@SpringBootApplication
@EnableCaching
@EnableRabbit
@EnableAsync
public class Oss6Application {
  public static void main(String[] args) {
    SpringApplication.run(Oss6Application.class, args);
  }
}

コントローラー層

/**
 * @author WGR
 * @create 2019/10/12 -- 21:53
 */
@RestController
public class AsynController {

  @Autowired
  AsynService asyncService;

  @GetMapping("/hello")
  public String hello(){
    asyncService.hello();
    return "success";
  }
}

サービス層

/**
 * @author WGR
 * @create 2019/10/12 -- 21:52
 */
@Service
public class AsynService {

  //告诉Spring这是一个异步方法
  @Async
  public void hello() {
    try {
      Thread.sleep(3000);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    System.out.println("处理数据中...");
  }

}

テスト結果:

ページには成功が直接表示され、コンソールには 3 秒後に処理中のデータが表示されます...

以上がSpringboot での非同期タスクの分析例の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事はyisu.comで複製されています。侵害がある場合は、admin@php.cn までご連絡ください。