2017-11-02 6 views
0

아래 콘솔 출력에서 ​​내 내장 ActiveMQ가 올바르게 초기화되었으며 61616 포트에서 응답하고 있습니다.유닛 테스트 중에 onMessage() (JMS Listener)가 호출되지 않는 이유

아래의 테스트를 실행하면 줄별로 디버깅하고 simpleSend() 및 sendMessage()가 실행 된 것을 볼 수 있기 때문에 메시지가 성공적으로 전송되었다고 가정합니다.

onMessage가 두 번 트리거 될 것으로 예상했지만 실제로 발생하지 않았습니다. 그 브로커와 내장의 ActiveMQ가 시작 - 2 -위한 MyListener의 인스턴스가 org.springframework.jms.listener.DefaultMessageListenerContainer에 전달 1 : 내가 단위 테스트를 실행하면 아래의 코드에서

는, 내가 이해

그래서 onMessage()를 테스트하기 위해 여기에없는 부분이 있습니까?

콘솔

INFO | Using Persistence Adapter: MemoryPersistenceAdapter 
INFO | Apache ActiveMQ 5.14.1 (localhost, ID:win10-cha-55866-1509636415848-0:1) is starting 
INFO | Listening for connections at: tcp://127.0.0.1:61616 
INFO | Connector tcp://localhost:61616 started 
INFO | Apache ActiveMQ 5.14.1 (localhost, ID:win10-cha-55866-1509636415848-0:1) started 
INFO | For help or more information please see: http://activemq.apache.org 

보낸 사람

import javax.jms.Queue; 

import org.springframework.jms.core.JmsTemplate; 

public class SampleJmsMessageSender { 

    private JmsTemplate jmsTemplate; 
    private Queue queue; 

    public void setJmsTemplate(JmsTemplate jmsTemplate) { 
     this.jmsTemplate = jmsTemplate; 
    } 

    public void setQueue(Queue queue) { 
     this.queue = queue; 
    } 

    public void simpleSend() { 
     jmsTemplate.send(queue, s -> s.createTextMessage("hello queue world")); 
    } 

    public void sendMessage(final MyPojo mp) { 
     this.jmsTemplate.convertAndSend(mp); 
    } 
} 

EmbeddedActiveMQ.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:p="http://www.springframework.org/schema/p" 
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:util="http://www.springframework.org/schema/util" xmlns:task="http://www.springframework.org/schema/task" 
    xmlns:amq="http://activemq.apache.org/schema/core" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://activemq.apache.org/schema/core 
         http://activemq.apache.org/schema/core/activemq-core-5.2.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> 

    <!-- Embedded ActiveMQ Broker --> 
    <amq:broker id="broker" useJmx="false" persistent="false" 
     useShutdownHook="false"> 
     <amq:transportConnectors> 
      <amq:transportConnector uri="tcp://localhost:61616" /> 
     </amq:transportConnectors> 
    </amq:broker> 
</beans> 

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:amq="http://activemq.apache.org/schema/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd 
    http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd"> 

    <!-- JmsTemplate Definition --> 
    <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"> 
     <property name="connectionFactory" ref="connectionFactory" /> 
     <property name="defaultDestination" ref="destinationQueue" /> 
     <property name="messageConverter" ref="myMessageConverter" /> 
    </bean> 
    <bean id="amqConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"> 
     <constructor-arg index="0" value="tcp://localhost:61616" /> 
    </bean> 

    <!-- ConnectionFactory Definition --> 
    <bean id="connectionFactory" 
     class="org.springframework.jms.connection.SingleConnectionFactory"> 
     <constructor-arg ref="amqConnectionFactory" /> 

    </bean> 


    <bean id="destinationQueue" class="org.apache.activemq.command.ActiveMQQueue"> 
     <constructor-arg index="0" value="IN_QUEUE" /> 
    </bean> 


    <bean id="SampleJmsMessageSender" class="com.mypackage.spring.jms.SampleJmsMessageSender"> 
     <property name="queue" ref="destinationQueue" /> 
     <property name="jmsTemplate" ref="jmsTemplate" /> 
    </bean> 

    <bean id="myMessageConverter" class="com.mypackage.spring.jms.SampleMessageConverter" /> 

    <!-- this is the Message-Driven POJO (MDP) --> 
    <bean id="messageListener" class="com.mypackage.spring.jms.MyListener"> 
     <property name="jmsTemplate" ref="jmsTemplate" /> 
     <property name="queue" ref="destinationQueue" /> 
    </bean> 


    <!-- and this is the message listener container --> 
    <bean id="jmsContainer" 
     class="org.springframework.jms.listener.DefaultMessageListenerContainer"> 
     <property name="connectionFactory" ref="connectionFactory" /> 
     <property name="destinationName" value="IN_QUEUE" /> 
     <property name="messageListener" ref="messageListener" /> 
    </bean> 
</beans> 

단위 테스트

import org.junit.BeforeClass; 
import org.junit.Ignore; 
import org.junit.Test; 
import org.springframework.context.ApplicationContext; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 

public class DefaultTextMessageSenderIntegrationTest { 

    private static SampleJmsMessageSender messageProducer; 

    @SuppressWarnings("resource") 
    @BeforeClass 
    public static void setUp() { 
     ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:EmbeddedActiveMQ.xml", "classpath:applicationContext.xml"); 
     messageProducer = (SampleJmsMessageSender) applicationContext.getBean("SampleJmsMessageSender"); 
    } 

    @Test 
    public void test1() { 
     messageProducer.simpleSend(); 
    } 

    @Test 
    public void test2() { 
     messageProducer.sendMessage(new MyPojo("name", 1)); 
    } 
} 

리스너

import org.springframework.jms.core.JmsTemplate; 

import javax.jms.JMSException; 
import javax.jms.Message; 
import javax.jms.MessageListener; 
import javax.jms.Queue; 
import javax.jms.TextMessage; 
import java.util.Map; 

public class MyListener implements MessageListener { 

    private JmsTemplate jmsTemplate; 
    private Queue queue; 

    public void setJmsTemplate(JmsTemplate jmsTemplate) { 
     this.jmsTemplate = jmsTemplate; 
    } 

    public void setQueue(Queue queue) { 
     this.queue = queue; 
    } 
    @Override 
    public void onMessage(Message message) { 
     if (message instanceof TextMessage) { 
      try { 
       String msg = ((TextMessage) message).getText(); 
       System.out.println("Received message: " + msg); 
      } catch (JMSException ex) { 
       throw new RuntimeException(ex); 
      } 
     } 
    } 

    public MyPojo receiveMessage() throws JMSException { 
     Map map = (Map) this.jmsTemplate.receiveAndConvert(); 
     return new MyPojo((String) map.get("name"), (Integer) map.get("age")); 
    } 
} 

의 pom.xml

<properties> 
     <springframework.version>4.3.4.RELEASE</springframework.version> 
     <activemq.version>5.14.1</activemq.version> 
     <maven-war-plugin.version>2.6</maven-war-plugin.version> 
     <maven-compiler-plugin.version>3.6.0</maven-compiler-plugin.version> 
     <junit.version>4.12</junit.version> 
    </properties> 

    <dependencies> 

     <!-- Spring JMS --> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-jms</artifactId> 
      <version>${springframework.version}</version> 
      <exclusions> 
       <exclusion> 
        <artifactId>commons-logging</artifactId> 
        <groupId>commons-logging</groupId> 
       </exclusion> 
      </exclusions> 
     </dependency> 

     <!-- ActiveMQ --> 
     <dependency> 
      <groupId>org.apache.activemq</groupId> 
      <artifactId>activemq-all</artifactId> 
      <version>${activemq.version}</version> 
     </dependency> 

     <!-- test --> 
     <dependency> 
      <groupId>junit</groupId> 
      <artifactId>junit</artifactId> 
      <version>${junit.version}</version> 
      <scope>test</scope> 
     </dependency> 
    </dependencies> 

    <build> 
     <pluginManagement> 
      <plugins> 
       <plugin> 
        <groupId>org.apache.maven.plugins</groupId> 
        <artifactId>maven-war-plugin</artifactId> 
        <version>${maven-war-plugin.version}</version> 
        <configuration> 
         <warSourceDirectory>src/main/webapp</warSourceDirectory> 
         <warName>spring-jms</warName> 
         <failOnMissingWebXml>false</failOnMissingWebXml> 
        </configuration> 
       </plugin> 

       <plugin> 
        <groupId>org.apache.maven.plugins</groupId> 
        <artifactId>maven-compiler-plugin</artifactId> 
        <version>${maven-compiler-plugin.version}</version> 
        <configuration> 
         <source>1.8</source> 
         <target>1.8</target> 
        </configuration> 
       </plugin> 
      </plugins> 
     </pluginManagement> 
     <finalName>spring-jms</finalName> 
    </build> 

답변

0

당신의 단위 테스트는 (비동기)가 메시지를 소비 리스너 전에 중지, 당신은 것을 포함해야 테스트로 지연 Thread.sleep(2000);을 추가하여 테스트 중지 지연

또는 당신은 MyListener.receiveMessage()을 추가하고 DMLC를 제거 ...

또는이 값이 null로 시험에 인스턴스 변수로 String msg = null; of MyListener을 넣고 기다리고 있습니다 ... 실제 코드와

에서 디버그 모드에서 메시지가 사용되는지 확인하기 위해 MyListener.onMessage에 중단 점을 넣을 수 있습니다.