我正在尝试将Spring Integration添加到我正在编写的REST MVC Spring应用程序中 . 我使用最新的Spring 4.2.x进行核心,集成和mvc . 我们的想法是创建单独的应用程序上下文,如动态FTP示例 . 原因是因为我可以从2个独立的帐户发送电子邮件以及从2个独立的帐户收听,因此具有单独的应用程序上下文以及环境变量来帮助为每个上下文创建bean有助于一堆 .
我为新手问题道歉,但我在手册上遇到了困难,并试图弄清楚如何在没有XML的情况下设置SMTP电子邮件配置类 .
我希望同时接收和发送集成通道 . 所有电子邮件设置都将根据环境变量进行配置,因此我注入了环境:@Autowired Environment env;
我可以定义:
-
MailSender bean
-
MailSendingMessageHandler bean
-
SMTP(出站)的MessageChannel
现在,在XML配置上,您有一个出站通道适配器,您可以在其中连接mail-sender bean以及MessageChannel
我的目标是配置:
-
发送电子邮件 .
-
收听IMAP电子邮件并进行处理 .
对于发送电子邮件,我们的想法是从休息 endpoints 获取,调用服务,该服务将消息发送到Integration SMTP出站通道以发送电子邮件 . 看起来,通过使用MailSendingMessageHandler,它将获得集成消息并转换为MailSender的邮件消息 . 我不知道如何将MailSendingMessageHandler连接到出站通道,以便可以发送电子邮件 . 另外我不知道如何从我的@Service类调用其余 endpoints 如何创建消息并通过出站SMTP通道发送它们,以便发送电子邮件 . 在一次休息电话中,我发送了我想要联系的所有电子邮件收件人 . 之前,每个电子邮件正文都已正确格式化,以便我可以创建将由MailSendingMessageHandler处理和转换的每个集成消息(作为电子邮件) . 我试图在网上找到例子但没有成功如何实现这一点 .
您可以重定向我的任何示例?提前致谢!
到目前为止,我有配置:
import java.util.Properties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.integration.annotation.InboundChannelAdapter;
import org.springframework.integration.config.EnableIntegration;
import org.springframework.integration.annotation.Poller;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.core.MessageSource;
import org.springframework.integration.mail.MailReceiver;
import org.springframework.integration.mail.MailReceivingMessageSource;
import org.springframework.integration.mail.MailSendingMessageHandler;
import org.springframework.mail.MailMessage;
import org.springframework.mail.MailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.MessagingException;
import org.springframework.core.env.Environment;
@Configuration
@EnableIntegration
public class IntegrationEmailConfig {
@Autowired
Environment env;
@Bean
public static PropertySourcesPlaceholderConfigurer pspc() {
return new PropertySourcesPlaceholderConfigurer();
}
@Bean
@InboundChannelAdapter(value = "emailInboundChannel", poller = @Poller(fixedDelay = "5000") )
public MailReceivingMessageSource mailMessageSource(MailReceiver imapMailReceiver) {
return new MailReceivingMessageSource(imapMailReceiver);
}
private Properties additionalMailProperties() {
Properties properties = new Properties();
if (env.containsProperty("mail.smtp.auth")) {
properties.setProperty("mail.smtp.auth",env.getProperty("mail.smtp.auth"));
}
if (env.containsProperty("mail.smtp.starttls.enable")) {
properties.setProperty("mail.smtp.starttls.enable",env.getProperty("mail.smtp.starttls.enable"));
}
return properties;
}
@Bean
public MailSender mailSender() throws Exception {
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
if (env.containsProperty("mail.server.host")) {
mailSender.setHost(env.getProperty("mail.server.host"));
} else {
throw new Exception("Missing mail.server.host property");
}
if (env.containsProperty("mail.server.port")) {
mailSender.setPort(Integer.parseInt(env.getProperty("mail.server.port")));
} else {
throw new Exception("Missing mail.server.port property");
}
if (env.containsProperty("mail.server.username")) {
mailSender.setUsername(env.getProperty("mail.server.username"));
} else {
throw new Exception("Missing mail.server.username property");
}
if (env.containsProperty("mail.server.password")) {
mailSender.setPassword(env.getProperty("mail.server.password"));
} else {
throw new Exception("Missing mail.server.password property");
}
mailSender.setJavaMailProperties(additionalMailProperties());
return mailSender;
}
@Bean
public MailSendingMessageHandler mailSendingMessageHandler() throws Exception {
MailSendingMessageHandler mailSendingMessageHandler = new MailSendingMessageHandler(mailSender());
//mailSendingMessageHandler.setChannelResolver(channelResolver);
return mailSendingMessageHandler;
}
/* @Bean
public DirectChannel outboundMail() {
DirectChannel outboundChannel = new DirectChannel();
return outboundChannel;
}
*/
@Bean
public MessageChannel smtpChannel() {
return new DirectChannel();
}
/* @Bean
@Value("${imap.url}")
public MailReceiver imapMailReceiver(String imapUrl) {
// ImapMailReceiver imapMailReceiver = new ImapMailReceiver(imapUrl);
// imapMailReceiver.setShouldMarkMessagesAsRead(true);
// imapMailReceiver.setShouldDeleteMessages(false);
// // other setters here
// return imapMailReceiver;
MailReceiver receiver = mock(MailReceiver.class);
MailMessage message = mock(Message.class);
when(message.toString()).thenReturn("Message from " + imapUrl);
Message[] messages = new Message[] {message};
try {
when(receiver.receive()).thenReturn(messages);
}
catch (MessagingException e) {
e.printStackTrace();
}
return receiver;
}*/
}
1 回答
只需使用
@ServiceActivator
注释MailSendingMessageHandler
bean,框架将注册ConsumerEndpointFactoryBean
来包装处理程序 . 见the documentation about "Annotations on @Beans" .