gpt4 book ai didi

spring - 如何在 spring 中实现异步电子邮件服务

转载 作者:行者123 更新时间:2023-12-01 06:33:31 26 4
gpt4 key购买 nike

这是我的电子邮件服务

@Service("mailService")
public class EmailService
{

@Autowired
private MailSender mailSender;

@Async
public void sendMail(String to, String subject, String body) {
SimpleMailMessage message = new SimpleMailMessage();
message.setTo(to);
message.setSubject(subject);
message.setText(body);
mailSender.send(message);

}

@Async
public void sendPreConfiguredMail(String to,SimpleMailMessage configuredMessage) {
SimpleMailMessage mailMessage = new SimpleMailMessage(configuredMessage);
mailMessage.setTo(to);
mailSender.send(mailMessage);
}

}

我使用 JavaMailSenderImpl 来实现 MailSender
 <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="smtp.gmail.com"/>
<property name="port" value="25"/>
<property name="username" value="xxx"/>
<property name="password" value="xxx"/>
<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>
<prop key="mail.debug">true</prop>
</props>
</property>
</bean>

让spring认出 @Async
<mvc:annotation-driven />

<bean id="executorService" class="java.util.concurrent.Executors"
factory-method="newFixedThreadPool">
<constructor-arg value="10" />
</bean>

<task:executor id="threadPoolTaskExecutor" pool-size="10" />

<task:annotation-driven executor="executorService" />

<context:component-scan base-package="com.bistyle.lifelog" />

但是据说@Async在服务方法中不起作用,对吗?

我应该怎么做才能实现它?

最佳答案

尝试更换:

<bean id="executorService" class="java.util.concurrent.Executors"
factory-method="newFixedThreadPool">
<constructor-arg value="10" />
</bean>

<task:executor id="threadPoolTaskExecutor" pool-size="10" />

<task:annotation-driven executor="executorService" />


<task:executor id="executorService" pool-size="10" />

<task:annotation-driven executor="executorService" />

它对我很有效

关于spring - 如何在 spring 中实现异步电子邮件服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17786132/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com