簡體   English   中英

Spring集成郵件入站通道

[英]Spring integration mail inbound channel

我是Spring和Spring集成的新手,我要完成一個簡單的任務。 通過正則表達式來過濾一些電子郵件,並在數據庫中注冊一些信息。

我已經設置了JavaMailProperties,並且測試為我提供了已讀電子郵件的輸出,但是從未調用我使用service-activator設置的方法,這實際上使我非常頭疼。

以下是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"
       xmlns:mail="http://www.springframework.org/schema/integration/mail"
       xmlns:int="http://www.springframework.org/schema/integration"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
              http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
              http://www.springframework.org/schema/integration/mail  
              http://www.springframework.org/schema/integration/mail/spring-integration-mail-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">  
<util:properties id="javaMailProperties">
        <prop key="mail.store.protocol">pop3</prop>
        <prop key="mail.debug">true</prop>
    </util:properties>
    <mail:inbound-channel-adapter id="pop3Adapter" 
                                      store-uri="pop3://username:password@mail..example.com:110/INBOX"                                     
                                      channel="recieveEmailChannel"                                          
                                      should-delete-messages="false"                                   
                                      auto-startup="true"
                                      java-mail-properties="javaMailProperties"
                                      mail-filter-expression="subject matches '(^Presente+\\s([1-9]{1})+(\\s[-]\\s)+([A-Z]{4,})+(\\s[A-Z]{6,})$)'">
        <int:poller max-messages-per-poll="10" fixed-delay="10000"/>
    </mail:inbound-channel-adapter>
    <int:channel id="recieveEmailChannel">        
        <int:interceptors>
            <int:wire-tap channel="logger"/>
        </int:interceptors>
    </int:channel>
    <int:logging-channel-adapter id="logger" level="DEBUG"/>
    <int:service-activator input-channel="recieveEmailChannel" ref="leggiMail" method="processa_mail"/>
    <bean id="leggiMail" class="it.jenia.ac.mail.rapportini.LeggiMail">
    </bean>
</beans>

LeggiMail帶班processa_mail方法很簡單:

import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.stereotype.Service;

@Service
public class LeggiMail {
    private static Logger logger = Logger.getLogger(LeggiMail.class);
    public static int count_get = 0;
    @ServiceActivator
    public void processa_mail(MimeMessage mimeMessage) {
        count_get++;
        logger.debug("porcessa_mail working");
    }

我在以下位置使用此應用程序的測試類:

@ContextConfiguration(locations = { "classpath*:/test-spring-configuration.xml" })
@RunWith(SpringJUnit4ClassRunner.class)
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, TransactionalTestExecutionListener.class })
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = false)
public class LeggiMailTest {
    private static Logger logger = Logger.getLogger(LeggiMailTest.class);
    @Autowired
    LeggiMail lm;
    @Test
    @Transactional(value = "transactionManager", propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = Exception.class)
    public void test_processa_mail(){
        logger.debug("Test started");
    }
}

Test started ”日志在控制台中正確顯示,但是從未顯示日志porcessa_mail working

我在該主題上找到的第一篇教程只是談到了默認情況下上下文會調用的一種方法。 http://blog.solidcraft.eu/2011/04/read-emails-from-imap-with-spring.html (而且它說,加載上下文時,默認情況下應調用“ processa_mail”方法,因為service-activator

閱讀有關服務激活器的本教程並不能提供足夠的幫助: http : //docs.spring.io/spring-integration/reference/html/messaging-endpoints-chapter.html

當您嘗試測試一些async內容時,您會遇到一些barrier以防止main線程過早停止,這是整個測試用例所必需的。

最簡單的方法是在測試方法結束之前添加Thread.sleep()

但是更好的解決方案基於某些synchonizer ,例如CountDonwLatch

QueueChannel ,Spring Integration中有QueueChannel 您可以將其用作<service-activator>output-channel 將其注入測試類,然后等待output.receive(10000) assert...結果消息,使您的測試真正成為單元測試。

暫無
暫無

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

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