I'm using ProducerTemplate and ConsumerTemplate to handle sending and receiving messages in simple route test. Below there is a setup:
@CamelSpringBootTest
@EnableAutoConfiguration
class PushRouterIT extends BaseIT implements AmqContainer {
@Autowired
ProducerTemplate producer;
@Autowired
ConsumerTemplate consumer;
Test method:
//given //when
producer.sendBody("activemq:queue:test", json);
//then
var result = consumer.receiveBody(topic, TIMEOUT_IN_MILLIS);
assertThat(result).isEqualTo(json);
Routing is declared via a bean created with configuration annotation by extending class RouteBuilder, e.g.:
from("activemq:queue:test")
.routeId(CAMEL_MAIN_ROUTE_ID)
.process(PUSH_PROCESSOR_BEAN_NAME)
.choice()
.when()
.jsonpath(CONDITION_1_CHOICE_DEFINITION)
.to("activemq:topic:test1")
.when()
.jsonpath(CONDITION_2_CHOICE_DEFINITION)
.to("activemq:topic:test2")
.otherwise()
.to("activemq:queue:dlq")
.end();
Versions used:
Spring Boot 3.2.2 +
<camel-spring-boot-starter.version>4.3.0</camel-spring-boot-starter.version>
<camel-spring-boot-starter-activemq.version>4.3.0</camel-spring-boot-starter-activemq.version>
<camel-test-junit5.version>4.3.0</camel-test-junit5.version>
<camel-test-spring-junit5.version>4.3.0</camel-test-spring-junit5.version>
<testcontainers.version>1.19.5</testcontainers.version>
So the problem which I'm facing is fact that SOMETIMES my consumer is saying that he didn't receive any message. Increasing timeout is not working. Can it be related with the fact that I'm consuming messages from topic that can be not handled well by ConsumerTemplate construction?
I believe you're experiencing a race condition between sending and consuming.
If you're consuming messages from a JMS topic then you need to create your topic consumer before you send messages to the topic. The semantics of a JMS topic dictate that when no consumers exist on the topic then messages sent to that topic are discarded.