Back to Repositories

Testing Ordered ActiveMQ Message Processing in SpringBoot-Labs

A comprehensive test suite for validating ordered message processing in ActiveMQ using Spring Boot. This test class focuses on synchronized message sending with guaranteed ordering across multiple iterations.

Test Coverage Overview

The test suite examines synchronized message sending capabilities in ActiveMQ with specific focus on message ordering.

Key areas covered include:
  • Sequential message processing with multiple iterations
  • Message ordering preservation across concurrent sends
  • Integration with Spring Boot’s messaging infrastructure
  • Thread synchronization using CountDownLatch

Implementation Analysis

The testing approach utilizes Spring’s test framework with JUnit integration.

Notable implementation patterns include:
  • Spring Runner configuration for container-managed testing
  • Autowired producer injection for message sending
  • Nested loop structure for ordered message verification
  • Blocking wait pattern for consumption verification

Technical Details

Testing infrastructure includes:
  • JUnit 4 test framework
  • Spring Boot Test context
  • SLF4J logging framework
  • ActiveMQ message broker
  • CountDownLatch for thread synchronization
  • SpringRunner for test execution

Best Practices Demonstrated

The test implementation showcases several testing best practices:

  • Proper test isolation using Spring Boot test context
  • Controlled message ordering verification
  • Appropriate use of dependency injection
  • Clean separation of producer and test logic
  • Effective use of logging for debugging

yudaocode/springboot-labs

lab-32/lab-32-activemq-demo-orderly/src/test/java/cn/iocoder/springboot/lab32/activemqdemo/producer/Demo04ProducerTest.java

            
package cn.iocoder.springboot.lab32.activemqdemo.producer;

import cn.iocoder.springboot.lab32.activemqdemo.Application;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.concurrent.CountDownLatch;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class Demo04ProducerTest {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private Demo04Producer producer;

    @Test
    public void testSyncSend() throws InterruptedException {
        for (int i = 0; i < 2; i++) {
            for (int id = 0; id < 4; id++) {
                producer.syncSend(id);
//                logger.info("[testSyncSend][发送编号:[{}] 发送成功]", id);
            }
        }

        // 阻塞等待,保证消费
        new CountDownLatch(1).await();
    }

}