2014-11-07 2 views
0

저는 Spring과 Spring의 통합에 익숙하지 않으며, 수행해야 할 간단한 작업이 있습니다. 그것을 필터링하여 일부 전자 메일의 주제는 정규 표현식을 사용하고 db에 일부 정보를 등록합니다.스프링 통합 메일 인바운드 채널

JavaMailProperties를 설정했는데 테스트에서 읽은 전자 메일의 출력을 제공하지만 실제로는 service-activator으로 설정하는 메서드는 호출되지 않으며 이것이 실제로 두통이 심해집니다.

<?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:[email protected]: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"); 
    } 

테스트 클래스 :

다음

XML 구성 파일

@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

+1

왜 호출해야합니까? 내가 맞으면 응용 프로그램이 시작되고 그 후에 중지됩니다. 또한 왜 두 컨텍스트를로드하고 다른 컨텍스트에서 두 콩을 혼합하는 것이 더 나빠지는지 궁금합니다. 첫눈에 테스트에 결함이 있다고 말할 수 있습니다. –

+0

잘이 (http://blog.solidcraft.eu/2011/04/read-emails-from-imap-with-spring.html) 자습서에 따라 메서드는 기본적으로 호출되어야합니다. 왜 그런지 모르겠지만 이메일을 읽은 후에 "메소드"를 호출하는 해결책을 물어볼 수 있습니까? – softwareplay

+0

나는 간단히 "사이에"절차를 이해할 수 없다 .. – softwareplay

답변

1

을 이 전체 테스트의 경우에이 켜지지보다, 당신은 main 스레드를 방지하기 위해 일부 barrier 일찍 중단 할 몇 가지 async 물건을 테스트하려고합니다.

가장 단순한 방법은 시험 방법이 끝나기 전에 Thread.sleep()을 추가 할 수 있습니다.

그러나 더 좋은 솔루션은 과 같은 일부 synchonizer을 기반으로합니다.

다른 쪽에서는 봄 통합에 QueueChannel이 있습니다. <service-activator>의 경우 output-channel으로 사용할 수 있습니다. 테스트 클래스에 삽입하고 output.receive(10000)에서 기다리십시오. 그리고 assert... 당신의 테스트를 진짜 단위 테스트로 만들기위한 결과 메시지.

관련 문제