Desde una aplicación web con spring quiero enviar correos electrónicos, y lo quiero hacer de forma asíncrona.
Para ello quiero utilizar la anotación @Async de spring.
Esta es la configuración que tengo: servlet-context.xml
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<context:component-scan base-package="com.erodrigo.confianza" />
<!-- Anotaciones en tareas programadas -->
<task:annotation-driven/>
<!-- Enables the Spring MVC @Controller programming model -->
<context:annotation-config />
<annotation-driven>
<message-converters>
<beans:bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
<beans:bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
</message-converters>
</annotation-driven>
Luego tengo una interface donde declaro los metodos.
@Service
public interface EmailTaskService {
/**
*
* @param usuario
* @throws Exception
*/
@Async
public void sendMailNewUser(UsuarioDto usuario) throws Exception;
/**
*
* @param contratista
* @param tipoDocumento
* @param nombreDocumento
* @throws Exception
*/
@Async
public void sendMailDocumentoCaducado(EmpresasDto contratista, String nombreDocumento, String fechaCaducidad) throws Exception;
/**
*
* @param usuario
* @throws Exception
*/
@Async
public void sendMailResetPass(UsuarioDto usuario) throws Exception;
/**
*
* @throws Exception
*/
@Async
public void sendMailTest() throws Exception;
/**
*
* @return
* @throws Exception
*/
public MimeMessage createMimeMessage() throws Exception;
/**
*
* @param message
* @throws Exception
*/
public void send(MimeMessagePreparator message) throws Exception;
/**
*
* @param message
* @throws Exception
*/
public void send(MimeMessage message) throws Exception;
/**
*
* @param message
* @throws Exception
*/
public void send(SimpleMailMessage message) throws Exception;
}
Y la clase que los implementa.
@Service
public class EmailTaskServiceImpl implements EmailTaskService {
private PropertiesConfiguration properties;
private MailSender mailSender;
private JavaMailSender javaMailSender;
private VelocityEngine velocityEngine;
@Override
@Async
public void sendMailNewUser(final UsuarioDto usuario) throws Exception {
MimeMessagePreparator preparator = new MimeMessagePreparator() {
public void prepare(MimeMessage mimeMessage) throws Exception {
MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true);
message.setTo(usuario.getEmail());
message.setFrom(new InternetAddress(EmailsConstants.CUENTA_EMAIL, EmailsConstants.APP_NAME));
Map<String, Object> map = new HashMap<String, Object>();
System.out.println(usuario.getNombreCompleto());
map.put("nombreUsuario", usuario.getNombreCompleto());
map.put("user", usuario.getEmail());
map.put("userPass", usuario.getPass());
map.put("enlaceApp", properties.getString(EmailsConstants.URL_APP));
@SuppressWarnings("deprecation")
String text = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine,
EmailsConstants.VM_NUEVO_USUARIO, map);
message.setSubject(EmailsConstants.SUBJECT_NEW_USER);
message.setText(text, true);
}
};
send(preparator);
}
@Override
@Async
public void sendMailDocumentoCaducado(final EmpresasDto contratista, final String nombreDocumento, final String fechaCaducidad) throws Exception {
MimeMessagePreparator preparator = new MimeMessagePreparator() {
public void prepare(MimeMessage mimeMessage) throws Exception {
MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true);
message.setTo(contratista.getEmail());
message.setFrom(new InternetAddress(EmailsConstants.CUENTA_EMAIL, EmailsConstants.APP_NAME));
Map<String, Object> map = new HashMap<String, Object>();
map.put("nombreContratista", contratista.getRazonSocial());
map.put("nombreDocumento", nombreDocumento);
map.put("fechaCaducidad", fechaCaducidad);
map.put("enlaceApp", properties.getString(EmailsConstants.URL_APP));
@SuppressWarnings("deprecation")
String text = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine,
EmailsConstants.VM_DOCUMENTO_CADUCADO, map);
message.setSubject(EmailsConstants.SUBJECT_DOCUMENTO_CADUCADO);
message.setText(text, true);
}
};
send(preparator);
}
@Override
@Async
public void sendMailResetPass(final UsuarioDto usuario) throws Exception {
MimeMessagePreparator preparator = new MimeMessagePreparator() {
public void prepare(MimeMessage mimeMessage) throws Exception {
MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true);
message.setTo(usuario.getEmail());
message.setFrom(new InternetAddress(EmailsConstants.CUENTA_EMAIL, EmailsConstants.APP_NAME));
Map<String, Object> map = new HashMap<String, Object>();
System.out.println(usuario.getNombreCompleto());
map.put("nombreUsuario", usuario.getNombreCompleto());
map.put("user", usuario.getEmail());
map.put("userPass", usuario.getPass());
map.put("enlaceApp", properties.getString(EmailsConstants.URL_APP));
@SuppressWarnings("deprecation")
String text = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine,
EmailsConstants.VM_RESET_PASS, map);
message.setSubject(EmailsConstants.SUBJECT_RESET_PASS);
message.setText(text, true);
}
};
send(preparator);
}
@Override
@Async
public void sendMailTest() throws Exception {
System.out.println("Entro en sendMailTest()");
MimeMessagePreparator preparator = new MimeMessagePreparator() {
public void prepare(MimeMessage mimeMessage) throws Exception {
System.out.println("Entro prepare()");
MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true);
message.setTo("[email protected]");
message.setFrom(new InternetAddress(EmailsConstants.CUENTA_EMAIL, EmailsConstants.APP_NAME));
Map<String, Object> map = new HashMap<String, Object>();
@SuppressWarnings("deprecation")
String text = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine,
"/velocity/test.vm", map);
message.setSubject("Test");
message.setText(text, true);
}
};
System.out.println("Envio e correo");
send(preparator);
System.out.println("FIN envio correo");
}
@Override
public MimeMessage createMimeMessage() throws Exception {
return javaMailSender.createMimeMessage();
}
@Override
public void send(MimeMessagePreparator message) throws Exception {
javaMailSender.send(message);
}
@Override
public void send(MimeMessage message) throws Exception {
javaMailSender.send(message);
}
@Override
public void send(SimpleMailMessage message) throws Exception {
mailSender.send(message);
}
// GETTERS and SETTERS
public PropertiesConfiguration getProperties() {
return properties;
}
public void setProperties(PropertiesConfiguration properties) {
this.properties = properties;
}
public VelocityEngine getVelocityEngine() {
return velocityEngine;
}
public void setVelocityEngine(VelocityEngine velocityEngine) {
this.velocityEngine = velocityEngine;
}
public MailSender getMailSender() {
return mailSender;
}
public void setMailSender(MailSender mailSender) {
this.mailSender = mailSender;
}
public JavaMailSender getJavaMailSender() {
return javaMailSender;
}
public void setJavaMailSender(JavaMailSender javaMailSender) {
this.javaMailSender = javaMailSender;
}
}
Pues no se que tengo mal configurado pero no ejecuta los métodos de enviar emails de forma asíncrona.
¿Que estoy haciendo mal?
Muchas gracias por adelantado.