从seda队列中获取null

时间:2014-11-13 20:14:04

标签: apache-camel junit4

我有一条看起来像这样的骆驼路线:

 <camel:camelContext id="context" xmlns="http://camel.apache.org/schema/spring">
        <propertyPlaceholder id="camelContextProperties" location="ref:lefrogen"/>
        <contextScan/>
        <template id="producerTemplate"/>
        <template id="consumerTemplate"/>
        <route id="chibzo">
            <from uri="activemq:queueName"/>
            <to uri="seda:internal?multipleConsumers=true"/>
        </route>
        <route id="chon">
            <from uri="seda:internal?multipleConsumers=true"/>
            <bean ref="beanName"/>
            <to uri="seda:external?multipleConsumers=true"/>
        </route>
    </camel:camelContext>

bean类是这样的:

@Component
public class BeanName {
    private final static Logger LOGGER = Logger.getLogger(BeanName.class.getName());
    public static String message = null;

    public static String map(String custom) {    
        message = custom;
        return custom;
    }       

}

我编写了一个单元测试,它应该从bean中获取数据,然后将其与seda:external中的数据进行比较。问题是当我从seda队列中获取数据时,我变为null。这是单元测试:

public class Test{
  @EndpointInject
  ProducerTemplate producerTemplate;

  @EndpointInject
  ConsumerTemplate consumerTemplate;

  @Autowired
  BeanName beanName; 

  @Test
  public void testName() throws Exception {   
    producerTemplate.sendBody("seda:internal","Good bye");

    Thread.sleep(10000); //needs to sleep so that the bean gets executed before accessing 
                         //beanName.message;

    String message = beanName.message;
    LOGGER.info("Printing" + message); //prints (Good bye)

    String body = consumerTemplate.receiveBodyNoWait("seda:external", String.class);
    LOGGER.info("Body is" + body); //prints null 
  }

队列提供​​null的原因是什么?

1 个答案:

答案 0 :(得分:0)

它的并发,你正在使用recieveBodyNoWait进行快速检查。而是使用receiveBody并提供一个超时值,以避免在队列中没有消息时等待。

如果您还想使用receiveBodyNoWait,请在代码中添加sleep。