我是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
答案 0 :(得分:1)
当您尝试测试某些async
内容时,您需要barrier
来阻止main
线程提前停止,而不是整个测试用例。
简单方法是在测试方法结束之前添加Thread.sleep()
。
但更好的解决方案基于某些synchonizer
,例如CountDonwLatch
。
另一方面,Spring Integration中有QueueChannel
。您可以将其用作output-channel
的{{1}}。将它注入测试类并等待<service-activator>
。并output.receive(10000)
结果消息使您的测试真正进行单元测试。