如何在Spring Scheduler(@Schedule)中获取HttpServletRequest对象?

时间:2013-05-31 06:02:10

标签: spring spring-mvc thymeleaf

我正在尝试使用spring scheduler发送邮件。在那我需要HttpServletRequest对象来创建webContext,以便我可以使用百里香叶发送邮件。 任何人都知道答案。提前致谢。代码如下,

    @Async
    private void sendNotification(String toField, Users user, int currentMonth)
            throws Exception {

        // Prepare the evaluation context
        @SuppressWarnings("deprecation")

               **//here i need request object**
        final WebContext ctx = new WebContext(request, request.getSession()
                .getServletContext(), request.getLocale());

        ctx.setVariable("eagletId", user.getEagletId());
        ctx.setVariable("name", user.getFirstName());
        ctx.setVariable("setSentDate", new Date());
        ctx.setVariable("department", user.getDepartment());
        ctx.setVariable("batch", user.getBatch());

        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
        helper.setTo(user.getEmail());


        // create html body using thymeleaf
        final String htmlContent = this.templateEngine.process("email.html",
                ctx);
        helper.setText(htmlContent, true);
        mailSender.send(message);

    }

0 个答案:

没有答案