synchronized method or use spring @transactional?

后端 未结 6 612
你的背包
你的背包 2021-01-15 08:00

I have a method that will be used to send out email. i want to lock this method so only one thread can accses it per time and the rest pool up concurrently. should i synchro

6条回答
  •  情歌与酒
    2021-01-15 08:30

    in Sping 3.0 you can use @Async annotation to do task execution, so your method will be executed later and the method is returned directly without waiting for email to be sent.

    @Async
    public  void sendThroughSMTP(List emails,String subject,String content){
    //Send emails here, you can directly send lots of email
    }
    

    then in application context you specify and don't forget to add xmlns for task schema.

    If you want to delay the execution for certain amount of time, you may use @Scheduled annotation to your method.

    Further tutorial about @Async and @Scheduled can be found here :

    http://blog.springsource.com/2010/01/05/task-scheduling-simplifications-in-spring-3-0/

提交回复
热议问题