本文介绍了在一个新的线程问题发送电子邮件春暖花开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个应用程序,我开发的功能之一是,将发送电子邮件在我们的系统中注册,每次用户那里得到的他的发票。从发送的Java应用程序,特别是如果使用Spring框架轻松的电子邮件。我使用 JavaMailSenderImpl SimpleMailMessage从Spring框架和它的作品没关系。

One of the functionalities of app that I'm developing is that an email is sent every time user get's his invoice registered in our system. Sending an email from Java app easy especially if using Spring framework. I use JavaMailSenderImpl and SimpleMailMessage from Spring framework and it works okay.

但我需要发送电子邮件在一个新的线程,以便与SMTP服务器通信不会减慢应用程序的其余部分。问题是,当我打电话

But I need to send email in a new thread so that communication with SMTP server does not slow down the rest of apps processes. Problem is that when I call

MailSender.send()

这一个新的线程的方法,不发送电子邮件消息,如在同一线程发送时反对。
我试着用春天的 @Async注释,春天执行人和普通的旧的java.lang.Thread ,但它不工作。

method from a new thread, email message is not sent, as opposed when sending in a same thread.I tried with spring's @Async annotation, spring Executor and plain old java.lang.Thread but it doesn't work.

可以通过电子邮件进行异步Java中与春天派?没有任何人有类似的问题与此?
如果需要,我可以张贴一些code样品。

Can email be send asynchronously in java with spring? Had anyone a similar issue with this?I can post some code samples if needed.

TNX

推荐答案

1)添加任务命名空间到春天上下文。以下XSD是Spring 3.0的发布。
的xmlns:任务=htt​​p://www.springframework.org/schema/task

1) Add task namespace into spring context. The following xsd is for Spring 3.0 release.xmlns:task="http://www.springframework.org/schema/task"

结果

2)在声明中执行人Spring上下文文件。

2) Declare the executor in your spring context file.

<!--  Executor for the methods marked wiht @async annotations --> 
<task:executor id="asyncExecutor" pool-size="25" /> 

3)配置这春天的任务

3) Configure this to Spring task

<!--  Configuration for the Runtime --> 
<task:annotation-driven executor="asyncExecutor" /> 

这些都是你在Spring上下文文件所需要的配置。

These are all the configuration you need in the Spring context file.

该方法需要执行异步地@Async annotaion注解吧。

The method you need to perform asynchronously annotate it with @Async annotaion.

现在,所有@Async注释的方法将是春天任务执行异步处理。

Now, all the methods annotated with @async will be handled be spring task executor asynchronously.

这篇关于在一个新的线程问题发送电子邮件春暖花开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 11:04