How to covert xml based spring-integration-email configuration to java based config?

我只是一个虾纸丫 提交于 2019-12-13 01:14:57

问题


I am using spring-integration-email in a spring boot application to receive emails. All example I found online use xml based config.

I am using java based config in my application and I want to use the same for the spring-integration-email.

Below is my xml based config, which works properly. How Can I convert it to java based config?

<util:properties id="javaMailProperties">
    <prop key="mail.imap.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop>
    <prop key="mail.imap.socketFactory.fallback">false</prop>
    <prop key="mail.store.protocol">imaps</prop>
    <prop key="mail.debug">${imap.debug}</prop>
</util:properties>

<mail:inbound-channel-adapter id="imapAdapter"
                              store-uri="${imap.uri}"
                              channel="recieveEmailChannel"
                              should-delete-messages="false"
                              should-mark-messages-as-read="true"
                              auto-startup="true"
                              java-mail-properties="javaMailProperties">
    <int:poller fixed-delay="${imap.poolerSecondsDelay}" time-unit="SECONDS"/>
</mail:inbound-channel-adapter>

<int:channel id="recieveEmailChannel">
    <int:interceptors>
        <int:wire-tap channel="logger"/>
    </int:interceptors>
</int:channel>

<int:logging-channel-adapter id="logger" level="DEBUG"/>

<int:service-activator input-channel="recieveEmailChannel" ref="emailReceiverService" method="receive"/>

<bean id="emailReceiverService" class="com.sts.app.email.service.EmailService">
</bean>

回答1:


The code you posted (along with some small improvements to make it more configurable) would be transformed into the following Java config:

@Configuration 
public class MailConfig {

    @Value("${email.host}")
    private String host;

    @Value("${email.from}")
    private String from;

    @Value("${email.subject}")
    private String subject;

    @Bean
    public JavaMailSender javaMailService() {
        JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
        javaMailSender.setHost(host);
        return javaMailSender;
    }

    @Bean
    public SimpleMailMessage simpleMailMessage() {
       SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
       simpleMailMessage.setFrom(from);
       simpleMailMessage.setSubject(subject);
       return simpleMailMessage;
    }
}

also quick look on this link




回答2:


@Bean
@InboundChannelAdapter(value = "testReceiveEmailChannel", poller = @Poller(fixedDelay = "200000", taskExecutor = "asyncTaskExecutor"))
public MessageSource<javax.mail.Message> mailMessageSource(MailReceiver mailReceiver) {
    MailReceivingMessageSource mailReceivingMessageSource = new MailReceivingMessageSource(mailReceiver);
    // other setters here
    return mailReceivingMessageSource;
}

@Bean
public MailReceiver imapMailReceiver(@Value("imaps://${login}:${pass}@${host}:993/inbox") storeUrl) {
     ImapMailReceiver imapMailReceiver = new ImapMailReceiver(storeUrl);
        // other setters here
     return imapMailReceiver;
}

With the Spring Integration Java DSL that may look like:

    @Bean
    public IntegrationFlow imapMailFlow() {
        return IntegrationFlows
                .from(s -> s.imap("imap://user:pw@localhost:" + imapPort + "/INBOX")
                                .searchTermStrategy(this::fromAndNotSeenTerm)
                                .javaMailProperties(p -> p.put("mail.debug", "false")),
                        e -> e.autoStartup(true)
                                .poller(p -> p.fixedDelay(1000)))
                .channel(MessageChannels.queue("imapChannel"))
                .get();
    }

Also see the question and its discussion: spring receive emails without xml (using annotations only)



来源:https://stackoverflow.com/questions/36169903/how-to-covert-xml-based-spring-integration-email-configuration-to-java-based-con

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!