java - Spring集成邮件入站 channel

标签 java spring spring-integration

我是 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>

具有 processa_mail 方法的 LeggiMail 类非常简单:

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

日志测试已开始在控制台中正确显示,但日志porcessa_mailworking从未显示。

我发现的关于这个主题的第一个教程只是谈到了上下文默认调用的方法。 http://blog.solidcraft.eu/2011/04/read-emails-from-imap-with-spring.html (它表示加载上下文时应默认调用方法“processa_mail”,因为它是一个服务激活器

阅读本关于服务激活器的教程并没有提供足够的帮助:http://docs.spring.io/spring-integration/reference/html/messaging-endpoints-chapter.html

最佳答案

当您尝试测试一些async时给你一些东西barrier防止main线程提前停止,而不是整个测试用例所必需的。

最简单的方法是添加 Thread.sleep()在测试方法结束之前。

但更好的解决方案是基于一些synchonizer ,例如CountDonwLatch .

从另一边有QueueChannel在Spring集成中。您可以将其用作 output-channel对于 <service-activator> 。将其注入(inject)到测试类中并等待 output.receive(10000) 。和assert...结果消息使您的测试真正成为单元测试。

关于java - Spring集成邮件入站 channel ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26798261/

相关文章:

使用预增量递归时的 Java 堆栈溢出

java - Rest url 中查询参数的日期时间格式

java - 包含外部 jar 依赖项 spring boot 测试

spring - 测试 JMS 和 Spring 集成

java - 处理 ServletException - Java Servlet API 3.+

java - 这个程序已经是解析器了吗?

Spring 属性(property-placeholder) Autowiring

Spring Integration 作为独立 ESB 的嵌入式替代方案

java - Spring 集成和响应式(Reactive) WebSockets

java - 引用了未解析的外部符号 __imp__JNI_CreateJavaVM@12