簡體   English   中英

Spring Integration Mail-將XML轉換為Java Config

[英]Spring Integration Mail - converting XML to Java Config

我是Spring Framework的新手,在將* .xml轉換為Java Config時遇到了一些麻煩。 我不知道該如何替換此行:

<int:channel id="emails"/>

您可以在下面看到我的文件

xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
        http://www.springframework.org/schema/integration/mail http://www.springframework.org/schema/integration/mail/spring-integration-mail.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"
       xmlns:int="http://www.springframework.org/schema/integration"
       xmlns:int-mail="http://www.springframework.org/schema/integration/mail"
       xmlns:util="http://www.springframework.org/schema/util">

    <int:channel id="emails"/>

    <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">true</prop>
    </util:properties>

    <int-mail:imap-idle-channel-adapter id="mailAdapter"
                                  store-uri="imaps://login:pass@imap-server:993/INBOX"
                                  java-mail-properties="javaMailProperties"
                                  channel="emails"
                                  should-delete-messages="false"
                                  should-mark-messages-as-read="true">
    </int-mail:imap-idle-channel-adapter>
</beans>

我已經創建了Java Config:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.integration.mail.ImapIdleChannelAdapter;
import org.springframework.integration.mail.ImapMailReceiver;

import java.util.Properties;

@Configuration
class ImapConfiguration {

    private Properties javaMailProperties() {
        Properties javaMailProperties = new Properties();

        javaMailProperties.setProperty("mail.imap.socketFactory.class","javax.net.ssl.SSLSocketFactory");
        javaMailProperties.setProperty("mail.imap.socketFactory.fallback","false");
        javaMailProperties.setProperty("mail.store.protocol","imaps");
        javaMailProperties.setProperty("mail.debug","true");

        return javaMailProperties;
    }

    @Bean
    ImapIdleChannelAdapter mailAdapter() {
        ImapMailReceiver mailReceiver = new ImapMailReceiver("imaps://login:pass@imap-server:993/INBOX");

        mailReceiver.setJavaMailProperties(javaMailProperties());
        mailReceiver.setShouldDeleteMessages(false);
        mailReceiver.setShouldMarkMessagesAsRead(true);

        return new ImapIdleChannelAdapter(mailReceiver);
    }
}

我不知道該如何替換此行:

 <int:channel id="emails"/> 

剛到

@Bean
public MessageChannel emails() {
    return new DirectChannel();
}

請閱讀參考手冊以獲取更多信息,並查看示例項目。

是的,不要忘記對某些@Configuration類使用@EnableIntegrationhttp : @EnableIntegration

這取決於您想要的頻道,但是基本上適用

訊息通道

@Bean
public PollableChannel defaultChannel() {
    return new QueueChannel(10);
}

@Bean
public SubscribableChannel subscribeChannel() {
    return new PublishSubscribeChannel();
}

它完全取決於您使用的消息通道類型

如果使用點對點指向通道,則DirectChannel和NullChannel是適合您的選項。 對於發布訂閱者通道,請使用PublishSubscribeChannel,QueueChannel,PriorityChannel,RendezvousChannel,ExecutorChannel和ScopedChannel。

我建議你回去看看你做得如何

applicationcontext.getBean("emails",DirectChannel.class)

然后加

@Bean
    public DirectChannel defaultChannel() {
        return new DirectChannel();
    }

這是整個java配置類。

    import java.util.Properties;

    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.integration.channel.DirectChannel;
    import org.springframework.integration.mail.ImapIdleChannelAdapter;
    import org.springframework.integration.mail.ImapMailReceiver;

    @Configuration
    public class IMAPIdleConfiguration {

        @Value("${imap.mailReceiverURL}")
        private String imapMailReceiverURL;

        private Properties javaMailProperties() {
            Properties javaMailProperties = new Properties();
            /*
             * <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">false</prop> <prop
             * key="mail.smtp.timeout">10000</prop>
             */
            javaMailProperties.setProperty("mail.imap.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
            javaMailProperties.setProperty("mail.imap.socketFactory.fallback", "false");
            javaMailProperties.setProperty("mail.store.protocol", "imaps");
            javaMailProperties.setProperty("mail.debug", "true");
            javaMailProperties.setProperty("mail.smtp.timeout", "10000");

            return javaMailProperties;
        }

        @Bean
        ImapIdleChannelAdapter mailAdapter() {

            ImapMailReceiver mailReceiver = new ImapMailReceiver(this.imapMailReceiverURL);
            mailReceiver.setJavaMailProperties(javaMailProperties());
            mailReceiver.setShouldDeleteMessages(false);
            mailReceiver.setShouldMarkMessagesAsRead(true);

            ImapIdleChannelAdapter imapIdleChannelAdapter = new 
 ImapIdleChannelAdapter(mailReceiver);
            imapIdleChannelAdapter.setAutoStartup(true);
            imapIdleChannelAdapter.setOutputChannel(directChannel());
            return imapIdleChannelAdapter;

        }

        @Bean
        public DirectChannel directChannel() {
            return new DirectChannel();
        }

    }

Rahul Tokase的代碼幾乎是正確的,除了一件重要的事情。 代替:

@Bean
ImapIdleChannelAdapter mailAdapter() {    
            ImapMailReceiver mailReceiver = new ImapMailReceiver(this.imapMailReceiverURL);
            mailReceiver.setJavaMailProperties(javaMailProperties());
            mailReceiver.setShouldDeleteMessages(false);
            mailReceiver.setShouldMarkMessagesAsRead(true);

            ImapIdleChannelAdapter imapIdleChannelAdapter = new ImapIdleChannelAdapter(mailReceiver);
            imapIdleChannelAdapter.setAutoStartup(true);
            imapIdleChannelAdapter.setOutputChannel(directChannel());
            return imapIdleChannelAdapter;    
        }

我們必須執行以下操作:

    @Bean
    ImapIdleChannelAdapter mailAdapter() {        
                ImapIdleChannelAdapter imapIdleChannelAdapter = new ImapIdleChannelAdapter(mailReceiver());           
                imapIdleChannelAdapter.setAutoStartup(true);
                imapIdleChannelAdapter.setOutputChannel(directChannel());
                return imapIdleChannelAdapter;        
            }

    @Bean
    ImapMailReceiver mailReceiver() {        
                ImapMailReceiver mailReceiver = new ImapMailReceiver(this.imapMailReceiverURL);
                mailReceiver.setJavaMailProperties(javaMailProperties());
                mailReceiver.setShouldDeleteMessages(false);
                mailReceiver.setShouldMarkMessagesAsRead(true);
                return mailReceiver;
}

在這種情況下,所有bean將正確初始化。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM