Spring Boot Messaging with JmsTemplate and EmbeddedActiveMQBroker
1. Pom.xml
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-camel</artifactId>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-broker</artifactId>
<scope>test</scope>
</dependency>
2. Application yml / properties
amq:
endpoint:
in: some.queue.route.in
broker:
url: vm://localhost?broker.persistent=false
3. Test Class
@SpringBootTest(classes = {Application.class}, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
@ActiveProfiles({"your-app-yml"})
public class TestClass {
@Autowired
private JmsTemplate jmsTemplate;
@Rule
public EmbeddedActiveMQBroker broker = new EmbeddedActiveMQBroker();
@Value("${amq.endpoint.in}")
private String amqEndpointIn;
/**
* Make sure all queues are initialized before starting tests
*/
@Before
public void init() {
this.jmsTemplate.browse("some.queue.route2.in", (session, browser) -> 0);
this.jmsTemplate.browse("some.queue.route3.in", (session, browser) -> 0);
this.jmsTemplate.browse("some.queue.route.in", (session, browser) -> 0);
}
@Test
public void testQueues() throws Exception {
// assert again all queues are empty before starting original test
assertTrue(broker.getMessageCount("some.queue.route2.in") < 0);
assertTrue(broker.getMessageCount("some.queue.route3.in") < 0);
assertTrue(broker.getMessageCount("some.queue.route.in") == 0);
// trigger
jmsTemplate.send(amqEndpointIn, mc -> mc.createTextMessage(message));
jmsTemplate.send("some.queue.route2.in", mc -> mc.createTextMessage(message));
// assert copy-queue not empty, dlq-queue empty
assertTrue(broker.getMessageCount("some.queue.route2.in") > 0);
assertTrue(broker.getMessageCount("some.queue.route3.in") < 0);
}
}
Δεν υπάρχουν σχόλια:
Δημοσίευση σχολίου
What may be missing, or could get better?