简体   繁体   English

Spring Integration Mail-将XML转换为Java Config

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

Im new in Spring Framework and I have some troubles converting *.xml to Java Config. 我是Spring Framework的新手,在将* .xml转换为Java Config时遇到了一些麻烦。 I don't know how should I replace this line: 我不知道该如何替换此行:

<int:channel id="emails"/>

You can see my files below 您可以在下面看到我的文件

xml file: 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 I've aready created: 我已经创建了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);
    }
}

I don't know how should I replace this line: 我不知道该如何替换此行:

 <int:channel id="emails"/> 

Just to the 刚到

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

Please, read Reference Manual for more info and take a look into samples project. 请阅读参考手册以获取更多信息,并查看示例项目。

And yes, don't forget @EnableIntegration for some of your @Configuration classes: http://docs.spring.io/spring-integration/reference/html/overview.html#programming-tips 是的,不要忘记对某些@Configuration类使用@EnableIntegrationhttp : @EnableIntegration

It depends on the channel you want, but basically this apply 这取决于您想要的频道,但是基本上适用

Messaging Channels 讯息通道

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

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

Its totally depend upon what type of message channels you are using 它完全取决于您使用的消息通道类型

If you are using point to point to channel then DirectChannel and NullChannel is the options for you. 如果使用点对点指向通道,则DirectChannel和NullChannel是适合您的选项。 For publish-subscriber channel use PublishSubscribeChannel, QueueChannel, PriorityChannel, RendezvousChannel, ExecutorChannel and ScopedChannel. 对于发布订阅者通道,请使用PublishSubscribeChannel,QueueChannel,PriorityChannel,RendezvousChannel,ExecutorChannel和ScopedChannel。

I would suggest you go back and check how you did 我建议你回去看看你做得如何

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

then add 然后加

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

Here is the whole java configuration class. 这是整个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();
        }

    }

the code of Rahul Tokase is almost correct except one important thing. Rahul Tokase的代码几乎是正确的,除了一件重要的事情。 instead of: 代替:

@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;    
        }

we must to do following: 我们必须执行以下操作:

    @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;
}

In this case all beans will initialize correctly. 在这种情况下,所有bean将正确初始化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM