max3d
max3d

Reputation: 1507

ServiceActivator does not receive message from ImapIdleChannelAdapter

ServiceActivator does not receive messages from ImapIdleChannelAdapter...

JavaMail logs successful FETCH, but MIME messages do not get delivered to SA endpoint... I want to understand what is wrong in my code.

A7 FETCH 1:35 (ENVELOPE INTERNALDATE RFC822.SIZE FLAGS BODYSTRUCTURE) * 1 FETCH (ENVELOPE ("Fri....

Code snippet below: ` @Autowired EmailConfig emailCfg;

@Bean
public SubscribableChannel mailChannel() {
    return MessageChannels.direct().get();
}

@Bean
public ImapIdleChannelAdapter getMailAdapter() {
    ImapMailReceiver mailReceiver = new ImapMailReceiver(emailCfg.getImapUrl());
    mailReceiver.setJavaMailProperties(javaMailProperties());
    mailReceiver.setShouldDeleteMessages(false);
    mailReceiver.setShouldMarkMessagesAsRead(true);
    ImapIdleChannelAdapter imapIdleChannelAdapter = new ImapIdleChannelAdapter(mailReceiver);
    imapIdleChannelAdapter.setOutputChannel(mailChannel());
    imapIdleChannelAdapter.setAutoStartup(true);
    imapIdleChannelAdapter.afterPropertiesSet();
    return imapIdleChannelAdapter;
}

@ServiceActivator(inputChannel = "mailChannel")
public void receive(String mail) {
    log.warn(mail);
}

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");
    javaMailProperties.setProperty("mail.imap.ssl", "true");
    return javaMailProperties;
}

`

The problem was due to wrong bean initialization. Full version that works OK:

@Slf4j
@Configuration
@EnableIntegration
public class MyMailAdapter {

@Autowired
EmailConfig emailCfg;

@Bean
public SubscribableChannel mailChannel() {
    log.info("Channel ready");
    return MessageChannels.direct().get();
}

@Bean
public ImapMailReceiver receiver() {
    ImapMailReceiver mailReceiver = new ImapMailReceiver(emailCfg.getImapUrl());
    mailReceiver.setJavaMailProperties(javaMailProperties());
    mailReceiver.setShouldDeleteMessages(false);
    mailReceiver.setShouldMarkMessagesAsRead(true);
    return mailReceiver;
}

@Bean
public ImapIdleChannelAdapter adapter() {
    ImapIdleChannelAdapter imapIdleChannelAdapter = new ImapIdleChannelAdapter(receiver());
    imapIdleChannelAdapter.setOutputChannel(mailChannel());
    imapIdleChannelAdapter.afterPropertiesSet();
    return imapIdleChannelAdapter;
}

@ServiceActivator(inputChannel = "mailChannel")
public void receive(Message<MimeMessage> mail) throws MessagingException {
    log.info(mail.getPayload().toString());
}

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");
    javaMailProperties.setProperty("mail.imap.ssl", "true");
    return javaMailProperties;
}
}

Upvotes: 0

Views: 795

Answers (1)

B.Ohara
B.Ohara

Reputation: 251

I don't know what's exactly wrong with your code but I will suggest you few approches that could help you.

  1. Firstly I suggest you to use java DSL in java based configuration. It will provide you nice way to directly specific flow of your integration application (and avoid simply mistakes). For example for spliiter and service activator:

    @Bean
    public IntegrationFlow yourFlow(AbstractMessageSplitter splitter,  
    MessageHandler handler) {
    return 
    IntegrationFlows
        .from(CHANNEL)
        .split(splitter)
        .handle(handler).get();
    

    }

  2. Secondly it's generally bad idea to directly specify message type to String. Try something like this (why String?):

    @ServiceActivator(inputChannel = "mailChannel")
    public void receive(Message<?> message) {
    /* (String) message.getPayload() */
    }
    

Maybe it's not a case but let's check it.

Upvotes: 1

Related Questions