如何在spring mvc

时间:2015-10-27 10:16:52

标签: java spring spring-mvc javamail

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

@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";
    }

这是我的applicationcontext

 </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秒。在添加此方法之前只需1秒。任何人都可以指导我减少程序的速度或调用sendEmail()之后完成第一笔交易。谢谢。

1 个答案:

答案 0 :(得分:0)

您必须创建异步服务并配置应用程序以进行异步使用,此处您有spring tutorial。在异步服务中,您必须放置用于发送电子邮件的代码。下面你可以看到2个类的示例代码,第一个是服务第二个是应用程序类,



        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);
        }
    }