From 985a753819a9100a7a96da10fc150d2bcf66513c Mon Sep 17 00:00:00 2001 From: Artem Bilan Date: Mon, 8 Jan 2024 10:21:20 -0500 Subject: [PATCH] Make ZeroMQ modules as auto-configuration * Fix all the Checkstyle violations in these modules --- .../zeromq/ZeroMqConsumerConfiguration.java | 11 +++--- .../zeromq/ZeroMqConsumerProperties.java | 19 ++++------ .../fn/consumer/zeromq/package-info.java | 4 +++ ...ot.autoconfigure.AutoConfiguration.imports | 1 + .../ZeroMqConsumerConfigurationTests.java | 15 +++++--- .../zeromq/ZeroMqSupplierConfiguration.java | 12 +++---- .../zeromq/ZeroMqSupplierProperties.java | 36 +++++-------------- .../fn/supplier/zeromq/package-info.java | 4 +++ ...ot.autoconfigure.AutoConfiguration.imports | 1 + .../ZeroMqSupplierConfigurationTests.java | 18 ++++++---- 10 files changed, 60 insertions(+), 61 deletions(-) create mode 100644 consumer/spring-zeromq-consumer/src/main/java/org/springframework/cloud/fn/consumer/zeromq/package-info.java create mode 100644 consumer/spring-zeromq-consumer/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports create mode 100644 supplier/spring-zeromq-supplier/src/main/java/org/springframework/cloud/fn/supplier/zeromq/package-info.java create mode 100644 supplier/spring-zeromq-supplier/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports diff --git a/consumer/spring-zeromq-consumer/src/main/java/org/springframework/cloud/fn/consumer/zeromq/ZeroMqConsumerConfiguration.java b/consumer/spring-zeromq-consumer/src/main/java/org/springframework/cloud/fn/consumer/zeromq/ZeroMqConsumerConfiguration.java index 5adbd444..248c5fb9 100644 --- a/consumer/spring-zeromq-consumer/src/main/java/org/springframework/cloud/fn/consumer/zeromq/ZeroMqConsumerConfiguration.java +++ b/consumer/spring-zeromq-consumer/src/main/java/org/springframework/cloud/fn/consumer/zeromq/ZeroMqConsumerConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2016-2020 the original author or authors. + * Copyright 2016-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -25,18 +25,20 @@ import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.AutoConfiguration; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; import org.springframework.integration.mapping.OutboundMessageMapper; import org.springframework.integration.zeromq.outbound.ZeroMqMessageHandler; import org.springframework.messaging.Message; /** + * The auto-configuration for ZeroMQ consumer. + * * @author Daniel Frey * @since 3.1.0 */ -@Configuration +@AutoConfiguration @EnableConfigurationProperties(ZeroMqConsumerProperties.class) public class ZeroMqConsumerConfiguration { @@ -49,6 +51,7 @@ public class ZeroMqConsumerConfiguration { public ZeroMqMessageHandler zeromqMessageHandler(ZeroMqConsumerProperties properties, ZContext zContext, @Autowired(required = false) Consumer socketConfigurer, @Autowired(required = false) OutboundMessageMapper messageMapper) { + ZeroMqMessageHandler zeroMqMessageHandler = new ZeroMqMessageHandler(zContext, properties.getConnectUrl(), properties.getSocketType()); @@ -69,7 +72,7 @@ public class ZeroMqConsumerConfiguration { @Bean public Function>, Mono> zeromqConsumer(ZeroMqMessageHandler zeromqMessageHandler) { - return input -> input.flatMap(zeromqMessageHandler::handleMessage).ignoreElements(); + return (input) -> input.flatMap(zeromqMessageHandler::handleMessage).ignoreElements(); } } diff --git a/consumer/spring-zeromq-consumer/src/main/java/org/springframework/cloud/fn/consumer/zeromq/ZeroMqConsumerProperties.java b/consumer/spring-zeromq-consumer/src/main/java/org/springframework/cloud/fn/consumer/zeromq/ZeroMqConsumerProperties.java index 3b91d5c4..56fc7815 100644 --- a/consumer/spring-zeromq-consumer/src/main/java/org/springframework/cloud/fn/consumer/zeromq/ZeroMqConsumerProperties.java +++ b/consumer/spring-zeromq-consumer/src/main/java/org/springframework/cloud/fn/consumer/zeromq/ZeroMqConsumerProperties.java @@ -1,5 +1,5 @@ /* - * Copyright 2016-2020 the original author or authors. + * Copyright 2016-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -25,6 +25,8 @@ import org.springframework.expression.Expression; import org.springframework.validation.annotation.Validated; /** + * The configuration properties for ZeroMQ consumer. + * * @author Daniel Frey * @since 3.1.0 */ @@ -49,35 +51,26 @@ public class ZeroMqConsumerProperties { @NotNull(message = "'socketType' is required") public SocketType getSocketType() { - return socketType; + return this.socketType; } - /** - * @param socketType the {@link SocketType} to establish. - */ public void setSocketType(SocketType socketType) { this.socketType = socketType; } @NotEmpty(message = "connectUrl is required like protocol://server:port") public String getConnectUrl() { - return connectUrl; + return this.connectUrl; } - /** - * @param connectUrl The ZeroMQ socket to expose - */ public void setConnectUrl(String connectUrl) { this.connectUrl = connectUrl; } public Expression getTopic() { - return topic; + return this.topic; } - /** - * @param topic The 'topic' SpEL expression to set - */ public void setTopic(Expression topic) { this.topic = topic; } diff --git a/consumer/spring-zeromq-consumer/src/main/java/org/springframework/cloud/fn/consumer/zeromq/package-info.java b/consumer/spring-zeromq-consumer/src/main/java/org/springframework/cloud/fn/consumer/zeromq/package-info.java new file mode 100644 index 00000000..8f397a38 --- /dev/null +++ b/consumer/spring-zeromq-consumer/src/main/java/org/springframework/cloud/fn/consumer/zeromq/package-info.java @@ -0,0 +1,4 @@ +/** + * The ZeroMQ consumer auto-configuration support. + */ +package org.springframework.cloud.fn.consumer.zeromq; diff --git a/consumer/spring-zeromq-consumer/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/consumer/spring-zeromq-consumer/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports new file mode 100644 index 00000000..fb6bdb5a --- /dev/null +++ b/consumer/spring-zeromq-consumer/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -0,0 +1 @@ +org.springframework.cloud.fn.consumer.zeromq.ZeroMqConsumerConfiguration diff --git a/consumer/spring-zeromq-consumer/src/test/java/org/springframework/cloud/fn/consumer/zeromq/ZeroMqConsumerConfigurationTests.java b/consumer/spring-zeromq-consumer/src/test/java/org/springframework/cloud/fn/consumer/zeromq/ZeroMqConsumerConfigurationTests.java index dce07377..0aed1387 100644 --- a/consumer/spring-zeromq-consumer/src/test/java/org/springframework/cloud/fn/consumer/zeromq/ZeroMqConsumerConfigurationTests.java +++ b/consumer/spring-zeromq-consumer/src/test/java/org/springframework/cloud/fn/consumer/zeromq/ZeroMqConsumerConfigurationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2016-2021 the original author or authors. + * Copyright 2016-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -34,6 +34,8 @@ import org.springframework.boot.test.context.SpringBootTest; import org.springframework.messaging.Message; import org.springframework.messaging.support.MessageBuilder; import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.DynamicPropertyRegistry; +import org.springframework.test.context.DynamicPropertySource; import static org.assertj.core.api.Assertions.assertThat; import static org.awaitility.Awaitility.await; @@ -51,16 +53,21 @@ public class ZeroMqConsumerConfigurationTests { private static ZMQ.Socket socket; + private static int bindPort; + @Autowired Function>, Mono> subject; @BeforeAll static void setup() { - socket = CONTEXT.createSocket(SocketType.SUB); socket.setReceiveTimeOut(10_000); - int bindPort = socket.bindToRandomPort("tcp://*"); - System.setProperty("zeromq.consumer.connectUrl", "tcp://localhost:" + bindPort); + bindPort = socket.bindToRandomPort("tcp://*"); + } + + @DynamicPropertySource + static void zeromqConnectUrl(DynamicPropertyRegistry dynamicPropertyRegistry) { + dynamicPropertyRegistry.add("zeromq.consumer.connectUrl", () -> "tcp://localhost:" + bindPort); } @AfterAll diff --git a/supplier/spring-zeromq-supplier/src/main/java/org/springframework/cloud/fn/supplier/zeromq/ZeroMqSupplierConfiguration.java b/supplier/spring-zeromq-supplier/src/main/java/org/springframework/cloud/fn/supplier/zeromq/ZeroMqSupplierConfiguration.java index f1df85d8..e4b22e8b 100644 --- a/supplier/spring-zeromq-supplier/src/main/java/org/springframework/cloud/fn/supplier/zeromq/ZeroMqSupplierConfiguration.java +++ b/supplier/spring-zeromq-supplier/src/main/java/org/springframework/cloud/fn/supplier/zeromq/ZeroMqSupplierConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2016-2020 the original author or authors. + * Copyright 2016-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -25,21 +25,21 @@ import org.zeromq.ZMQ; import reactor.core.publisher.Flux; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.AutoConfiguration; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; import org.springframework.integration.channel.FluxMessageChannel; import org.springframework.integration.zeromq.inbound.ZeroMqMessageProducer; import org.springframework.messaging.Message; import org.springframework.messaging.support.GenericMessage; /** - * A source module that receives data from ZeroMQ. + * A supplier auto-configuration that receives data from ZeroMQ. * * @author Daniel Frey * @since 3.1.0 */ -@Configuration +@AutoConfiguration @EnableConfigurationProperties(ZeroMqSupplierProperties.class) public class ZeroMqSupplierConfiguration { @@ -70,7 +70,7 @@ public class ZeroMqSupplierConfiguration { if (socketConfigurer != null) { zeroMqMessageProducer.setSocketConfigurer(socketConfigurer); } - zeroMqMessageProducer.setOutputChannel(output); + zeroMqMessageProducer.setOutputChannel(this.output); zeroMqMessageProducer.setAutoStartup(false); return zeroMqMessageProducer; @@ -78,7 +78,7 @@ public class ZeroMqSupplierConfiguration { @Bean public Supplier>> zeromqSupplier(ZeroMqMessageProducer adapter) { - return () -> Flux.from(output).doOnSubscribe(subscription -> adapter.start()); + return () -> Flux.from(this.output).doOnSubscribe((subscription) -> adapter.start()); } } diff --git a/supplier/spring-zeromq-supplier/src/main/java/org/springframework/cloud/fn/supplier/zeromq/ZeroMqSupplierProperties.java b/supplier/spring-zeromq-supplier/src/main/java/org/springframework/cloud/fn/supplier/zeromq/ZeroMqSupplierProperties.java index 26889cdd..c71207f1 100644 --- a/supplier/spring-zeromq-supplier/src/main/java/org/springframework/cloud/fn/supplier/zeromq/ZeroMqSupplierProperties.java +++ b/supplier/spring-zeromq-supplier/src/main/java/org/springframework/cloud/fn/supplier/zeromq/ZeroMqSupplierProperties.java @@ -1,5 +1,5 @@ /* - * Copyright 2016-2020 the original author or authors. + * Copyright 2016-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -27,6 +27,8 @@ import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.validation.annotation.Validated; /** + * The ZeroMQ supplier configuration properties. + * * @author Daniel Frey * @since 3.1.0 */ @@ -59,68 +61,46 @@ public class ZeroMqSupplierProperties { */ private String[] topics = { "" }; - /** - * @param socketType the {@link SocketType} to establish. - */ public void setSocketType(SocketType socketType) { this.socketType = socketType; } @NotNull(message = "'socketType' is required") public SocketType getSocketType() { - return socketType; + return this.socketType; } @NotEmpty(message = "connectUrl is required like tcp://server:port") public String getConnectUrl() { - return connectUrl; + return this.connectUrl; } - /** - * @param connectUrl The ZeroMQ server connect url - * - * @see org.springframework.integration.zeromq.inbound.ZeroMqMessageProducer#setConnectUrl(String) - */ public void setConnectUrl(String connectUrl) { this.connectUrl = connectUrl; } @Range(min = 0, message = "'bindPort' must not be negative") public int getBindPort() { - return bindPort; + return this.bindPort; } - /** - * @param bindPort The Port to bind to on all interfaces - * - * @see org.springframework.integration.zeromq.inbound.ZeroMqMessageProducer#setBindPort(int) - */ public void setBindPort(int bindPort) { this.bindPort = bindPort; } @NotNull(message = "'consumeDelay' is required") public Duration getConsumeDelay() { - return consumeDelay; + return this.consumeDelay; } - /** - * Specify a {@link Duration} to delay consumption when no data received. - * @param consumeDelay the {@link Duration} to delay consumption when empty. - */ public void setConsumeDelay(Duration consumeDelay) { this.consumeDelay = consumeDelay; } public String[] getTopics() { - return topics; + return this.topics; } - /** - * @param topics The ZeroMQ Topics to subscribe to - * - * @see org.springframework.integration.zeromq.inbound.ZeroMqMessageProducer#setTopics(String...) - */ public void setTopics(String... topics) { this.topics = topics; } diff --git a/supplier/spring-zeromq-supplier/src/main/java/org/springframework/cloud/fn/supplier/zeromq/package-info.java b/supplier/spring-zeromq-supplier/src/main/java/org/springframework/cloud/fn/supplier/zeromq/package-info.java new file mode 100644 index 00000000..75d60909 --- /dev/null +++ b/supplier/spring-zeromq-supplier/src/main/java/org/springframework/cloud/fn/supplier/zeromq/package-info.java @@ -0,0 +1,4 @@ +/** + * The ZeroMQ supplier auto-configuration support. + */ +package org.springframework.cloud.fn.supplier.zeromq; diff --git a/supplier/spring-zeromq-supplier/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/supplier/spring-zeromq-supplier/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports new file mode 100644 index 00000000..7656ad01 --- /dev/null +++ b/supplier/spring-zeromq-supplier/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -0,0 +1 @@ +org.springframework.cloud.fn.supplier.zeromq.ZeroMqSupplierConfiguration diff --git a/supplier/spring-zeromq-supplier/src/test/java/org/springframework/cloud/fn/supplier/zeromq/ZeroMqSupplierConfigurationTests.java b/supplier/spring-zeromq-supplier/src/test/java/org/springframework/cloud/fn/supplier/zeromq/ZeroMqSupplierConfigurationTests.java index d99bc196..b87e96f5 100644 --- a/supplier/spring-zeromq-supplier/src/test/java/org/springframework/cloud/fn/supplier/zeromq/ZeroMqSupplierConfigurationTests.java +++ b/supplier/spring-zeromq-supplier/src/test/java/org/springframework/cloud/fn/supplier/zeromq/ZeroMqSupplierConfigurationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2016-2020 the original author or authors. + * Copyright 2016-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -36,11 +36,14 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.messaging.Message; import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.DynamicPropertyRegistry; +import org.springframework.test.context.DynamicPropertySource; import static org.assertj.core.api.Assertions.assertThat; /** - * @author Daniel Frey since 3.1.0 + * @author Daniel Frey + * @since 3.1.0 */ @SpringBootTest(properties = { "zeromq.supplier.topics=test-topic" }) @DirtiesContext @@ -50,6 +53,8 @@ public class ZeroMqSupplierConfigurationTests { private static ZMQ.Socket socket; + private static int bindPort; + @Autowired Supplier>> subject; @@ -58,10 +63,12 @@ public class ZeroMqSupplierConfigurationTests { String socketAddress = "tcp://*"; socket = CONTEXT.createSocket(SocketType.PUB); - int bindPort = socket.bindToRandomPort(socketAddress); - - System.setProperty("zeromq.supplier.connectUrl", "tcp://localhost:" + bindPort); + bindPort = socket.bindToRandomPort(socketAddress); + } + @DynamicPropertySource + static void zeromqConnectUrl(DynamicPropertyRegistry dynamicPropertyRegistry) { + dynamicPropertyRegistry.add("zeromq.supplier.connectUrl", () -> "tcp://localhost:" + bindPort); } @AfterAll @@ -72,7 +79,6 @@ public class ZeroMqSupplierConfigurationTests { @Test void testSubscriptionConfiguration() throws InterruptedException { - StepVerifier stepVerifier = StepVerifier.create(subject.get()) .assertNext((message) -> assertThat(message.getPayload()) .asInstanceOf(InstanceOfAssertFactories.type(byte[].class))