wojteck
wojteck

Reputation: 69

Spring Integration Mail - converting XML to Java Config

Im new in Spring Framework and I have some troubles converting *.xml to 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 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:

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

Upvotes: 1

Views: 2065

Answers (4)

the code of Rahul Tokase is almost correct except one important thing. 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.

Upvotes: 0

Rahul Tokase
Rahul Tokase

Reputation: 1218

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. For publish-subscriber channel use PublishSubscribeChannel, QueueChannel, PriorityChannel, RendezvousChannel, ExecutorChannel and 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.

    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();
        }

    }

Upvotes: 0

dgofactory
dgofactory

Reputation: 348

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();
}

Upvotes: 2

Artem Bilan
Artem Bilan

Reputation: 121427

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

Upvotes: 2

Related Questions