Spring Integration Mail - 将XML转换为Java配置

时间:2016-08-10 17:52:55

标签: java xml spring spring-integration spring-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);
    }
}

4 个答案:

答案 0 :(得分:2)

  

我不知道应该如何替换这一行:

<int:channel id="emails"/>

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

请阅读Reference Manual了解详情,并查看samples项目。

是的,不要忘记@EnableIntegration某些@Configuration课程Column1 | Column2 ICDS-1 | 01 ICDS-1A | 1A ICDS-2 | 02 ICDS-3 | 3 ICDS-4A | 04 OTHER | ... http://docs.spring.io/spring-integration/reference/html/overview.html#programming-tips

答案 1 :(得分:2)

这取决于您想要的频道,但基本上这适用

Messaging Channels

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

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

答案 2 :(得分:0)

完全取决于您使用的消息渠道类型

如果您使用点对点频道,则可以选择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();
        }

    }

答案 3 :(得分:0)

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都将正确初始化。