BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Spring for Apache Kafka 3.0 and Spring for RabbitMQ 3.0 Released

Spring for Apache Kafka 3.0 and Spring for RabbitMQ 3.0 Released

Bookmarks

VMware has released Spring for Apache Kafka 3.0 and Spring for RabbitMQ 3.0, requiring Java 17 and Spring Framework 6.0. The projects now support the creation of native GraalVM applications and observation for timers and tracing by using the Micrometer metrics facade. Both projects now provide a Bill of Materials (BOM) in the pom.xml file to assist with dependency management.

Micrometer is used to instrument JVM code without vendor lock-in to allow observation for timers and tracing for the KafkaTemplate, the RabbitTemplate and listener containers. Micrometer is comparable with SLF4J, a logging facade, but for metrics.

Spring AOT native hints are now available in order to create native images for Spring applications with Spring for Apache Kafka or Spring for RabbitMQ. Examples are available in the spring-aot-smoke-tests project on GitHub. The EmbeddedKafkaBroker class from the spring-kafka-test module is not supported in native images.

Spring for Apache Kafka 3.0 requires Kafka clients version 3.3.1 and a minimum broker (Kafka server) version of 2.5 is required when using transactions.

A single global EmbeddedKafkaBroker may now be used across multiple test classes by replacing the bootstrap servers property with something like:

public final class EmbeddedKafkaContainer {
    private static EmbeddedKafkaBroker embeddedKafkaBroker = 
        new EmbeddedKafkaBroker(1, false)
            .brokerListProperty("spring.kafka.bootstrap-servers");
    private static boolean started;

    public static EmbeddedKafkaBroker getEmbeddedKafkaBroker() {
        if (!started) {
            try {
                embeddedKafkaBroker.afterPropertiesSet();
            }
            catch (Exception exception) {
                throw new KafkaException("Error starting 
                    EmbeddedKafkaBroker", exception);
            }
            started = true;
        }
        return embeddedKafkaBroker;
    }

    private EmbeddedKafkaContainer() {
        super();
    }
}

After configuring the EmbeddedKafkaBroker, it may be used in each test class:

static {
    EmbeddedKafkaHolder.getEmbeddedKafkaBroker()
        .addTopics("students", "teacher");
}

private static final EmbeddedKafkaBroker broker =
    EmbeddedKafkaContainer.getEmbeddedKafkaBroker();

With the previous example, the broker keeps running after completing the tests, which may lead to a potential problem. For example, when using a Gradle daemon, the destroy() method on EmbeddedKafkaBroker should be called after all tests are executed.

The GlobalEmbeddedKafkaTestExecutionListener starts a global EmbeddedKafkaBroker for the test plan and stops it when the test plan is executed. The listener is disabled by default and may be enabled via the spring.kafka.global.embedded.enabled property on JUnit Platform 1.8 or newer.

The @RetryableTopic annotation for non-blocking retries is no longer experimental. The annotation was further improved in this release, and this may now be used as a meta-annotation on custom annotations. Multiple @RetryableTopic listeners may be configured on the same topic in the same application context. The container now publishes the ConsumerRetryAuthEvent and ConsumerRetryAuthSuccessfulEvent events.

The various send methods defined in the KafkaTemplate and ReplyingKafkaTemplate classes now return a CompletableFuture instead of the deprecated ListenableFuture.

Spring for RabbitMQ now supports super streams with single active consumers. Super streams are created by binding several stream queues to an exchange with the argument x-super-stream: true. The bean of type SuperStream can be used to create the exchange test.exchange and two queues or partitions:

@Bean
SuperStream superStream() {
	return new SuperStream("test.exchange", 2);
}

Listener methods annotated with the @RabbitListener annotation may now consume a batch of messages of type Collection or List.

The send methods of the classes AsyncRabbitTemplate, RabbitStreamTemplate and the interface RabbitStreamOperations now return CompletableFuture instead of the deprecated ListenableFuture.

Support for remote method invocation (RMI) in Spring for RabbitMQ has been removed.

More information is available on the "What's New" pages for Kafka and RabbitMQ.

About the Author

Rate this Article

Adoption
Style

BT