spring mvc 中完成第一个事务后如何调用方法

How to call method after finishing first transaction in spring mvc

在我的 Spring mvc 应用程序中,当单击 jsp 页面中的 'Save' 按钮时调用以下方法。

@RequestMapping(value = "/add", method = RequestMethod.POST)
public String addUser(@ModelAttribute("user") @Valid User u,
        BindingResult result, @ModelAttribute("category") UserCategory uc,
        BindingResult resultCat, Model model, RedirectAttributes reDctModel) {


            this.userService.addUser(u); // adding new user to DB
            reDctModel.addFlashAttribute("msgSucess","Successfully saved..!");

            this.sendEmail(u.getUsr_email(),"RDMS","Login Details");  // For sending mail



        return "redirect:/users";
    }


public String sendEmail(String recipientAddress,String subject,String message) {
       // creates a simple e-mail object
        SimpleMailMessage email = new SimpleMailMessage();
        email.setTo(recipientAddress);
        email.setSubject(subject);
        email.setText(message);

        // sends the e-mail
        this.mailSender.send(email);

        return "Result";
    }

这是我的应用程序上下文

 </bean>
    <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
    <property name="host" value="smtp.gmail.com" />
    <property name="port" value="587" />
    <property name="username" value="myemailaddress@gmail.com" />
    <property name="password" value="********" />
    <property name="javaMailProperties">
        <props>
            <prop key="mail.transport.protocol">smtp</prop>
            <prop key="mail.smtp.auth">true</prop>
            <prop key="mail.smtp.starttls.enable">true</prop>
        </props>
    </property>
</bean>

问题是,在添加 sendEmail() 之后,大约需要 15 秒来保存新的 record.Before 添加这个方法只需要 1 second.Can 有人指导我减少程序的缓慢或提前transaction.Thanks完成后调用sendEmail()。

您必须创建异步服务并配置您的应用程序以供异步使用,这里有 spring tutorial。在异步服务中,您必须放置用于发送电子邮件的代码。您可以在下面看到 2 classes 的示例代码首先是服务,其次是应用程序 class、



        import java.util.concurrent.Future;
        import org.springframework.scheduling.annotation.Async;
        import org.springframework.scheduling.annotation.AsyncResult;
        import org.springframework.stereotype.Service;
        import org.springframework.web.client.RestTemplate;

        @Service
        public class SampleService {

            @Async
            public Future sendEmail(String email) {
                // here you can place your code for sending email
                return new AsyncResult("email sent successful");
            }

        }



    import java.util.concurrent.Future;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.CommandLineRunner;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.scheduling.annotation.EnableAsync;

    @SpringBootApplication
    @EnableAsync
    public class Application implements CommandLineRunner {

        @Autowired
        SampleService sampleService;

        @Override
        public void run(String... args) throws Exception {
            Future page1 = sampleService.sendEmail("test@gmail.com");
            while (!page1.isDone()) {
                Thread.sleep(10);
            }
            System.out.println(page1.get());
        }

        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    }