Various refactoring and improvements

* Remove `MessageChannel` beans whenever their really don't need to be exposed into the target application context.
Use local property definition instead in the configuration class
* Make Twitter `Consumer` configurations conditional on their required properties
to avoid auto-configuration for those bean which are not going to be used in the target application
This commit is contained in:
Artem Bilan
2024-03-07 14:51:36 -05:00
parent d7a80b6487
commit 946b5969da
12 changed files with 69 additions and 106 deletions

View File

@@ -55,17 +55,14 @@ public class SyslogSupplierConfiguration {
@Autowired
private SyslogSupplierProperties properties;
@Bean
public FluxMessageChannel syslogInputChannel() {
return new FluxMessageChannel();
}
private final FluxMessageChannel syslogInputChannel = new FluxMessageChannel();
@Bean
public Supplier<Flux<Message<?>>> syslogSupplier(FluxMessageChannel syslogInputChannel,
public Supplier<Flux<Message<?>>> syslogSupplier(
ObjectProvider<UdpSyslogReceivingChannelAdapter> udpAdapterProvider,
ObjectProvider<TcpSyslogReceivingChannelAdapter> tcpAdapterProvider) {
return () -> Flux.from(syslogInputChannel).doOnSubscribe((subscription) -> {
return () -> Flux.from(this.syslogInputChannel).doOnSubscribe((subscription) -> {
UdpSyslogReceivingChannelAdapter udpAdapter = udpAdapterProvider.getIfAvailable();
TcpSyslogReceivingChannelAdapter tcpAdapter = tcpAdapterProvider.getIfAvailable();
if (udpAdapter != null) {
@@ -79,25 +76,19 @@ public class SyslogSupplierConfiguration {
@Bean
@ConditionalOnProperty(name = "syslog.supplier.protocol", havingValue = "udp")
public UdpSyslogReceivingChannelAdapter udpAdapter(MessageConverter syslogConverter,
FluxMessageChannel syslogInputChannel) {
return createUdpAdapter(syslogConverter, syslogInputChannel);
public UdpSyslogReceivingChannelAdapter udpAdapter(MessageConverter syslogConverter) {
return createUdpAdapter(syslogConverter);
}
@Bean
@ConditionalOnProperty(name = "syslog.supplier.protocol", havingValue = "both")
public UdpSyslogReceivingChannelAdapter udpBothAdapter(MessageConverter syslogConverter,
FluxMessageChannel syslogInputChannel) {
return createUdpAdapter(syslogConverter, syslogInputChannel);
public UdpSyslogReceivingChannelAdapter udpBothAdapter(MessageConverter syslogConverter) {
return createUdpAdapter(syslogConverter);
}
private UdpSyslogReceivingChannelAdapter createUdpAdapter(MessageConverter syslogConverter,
FluxMessageChannel syslogInputChannel) {
private UdpSyslogReceivingChannelAdapter createUdpAdapter(MessageConverter syslogConverter) {
UdpSyslogReceivingChannelAdapter adapter = new UdpSyslogReceivingChannelAdapter();
setAdapterProperties(adapter, syslogConverter, syslogInputChannel);
setAdapterProperties(adapter, syslogConverter);
return adapter;
}
@@ -105,18 +96,18 @@ public class SyslogSupplierConfiguration {
@ConditionalOnProperty(name = "syslog.supplier.protocol", havingValue = "tcp", matchIfMissing = true)
public TcpSyslogReceivingChannelAdapter tcpAdapter(
@Qualifier("syslogSupplierConnectionFactory") AbstractServerConnectionFactory connectionFactory,
MessageConverter syslogConverter, FluxMessageChannel syslogInputChannel) {
MessageConverter syslogConverter) {
return createTcpAdapter(connectionFactory, syslogConverter, syslogInputChannel);
return createTcpAdapter(connectionFactory, syslogConverter);
}
@Bean
@ConditionalOnProperty(name = "syslog.supplier.protocol", havingValue = "both")
public TcpSyslogReceivingChannelAdapter tcpBothAdapter(
@Qualifier("syslogSupplierConnectionFactory") AbstractServerConnectionFactory connectionFactory,
MessageConverter syslogConverter, FluxMessageChannel syslogInputChannel) {
MessageConverter syslogConverter) {
return createTcpAdapter(connectionFactory, syslogConverter, syslogInputChannel);
return createTcpAdapter(connectionFactory, syslogConverter);
}
@Bean
@@ -130,20 +121,19 @@ public class SyslogSupplierConfiguration {
}
private TcpSyslogReceivingChannelAdapter createTcpAdapter(AbstractServerConnectionFactory connectionFactory,
MessageConverter syslogConverter, FluxMessageChannel syslogInputChannel) {
MessageConverter syslogConverter) {
TcpSyslogReceivingChannelAdapter adapter = new TcpSyslogReceivingChannelAdapter();
adapter.setConnectionFactory(connectionFactory);
setAdapterProperties(adapter, syslogConverter, syslogInputChannel);
setAdapterProperties(adapter, syslogConverter);
return adapter;
}
private void setAdapterProperties(SyslogReceivingChannelAdapterSupport adapter, MessageConverter syslogConverter,
FluxMessageChannel syslogInputChannel) {
private void setAdapterProperties(SyslogReceivingChannelAdapterSupport adapter, MessageConverter syslogConverter) {
adapter.setPort(this.properties.getPort());
adapter.setConverter(syslogConverter);
adapter.setOutputChannel(syslogInputChannel);
adapter.setOutputChannel(this.syslogInputChannel);
adapter.setAutoStartup(false);
}

View File

@@ -53,14 +53,10 @@ public class TwitterStreamSupplierConfiguration {
private static final Log LOGGER = LogFactory.getLog(TwitterStreamSupplierConfiguration.class);
@Bean
public FluxMessageChannel twitterStatusInputChannel() {
return new FluxMessageChannel();
}
private final FluxMessageChannel twitterStatusInputChannel = new FluxMessageChannel();
@Bean
public StatusListener twitterStatusListener(FluxMessageChannel twitterStatusInputChannel,
TwitterStream twitterStream, ObjectMapper objectMapper) {
public StatusListener twitterStatusListener(TwitterStream twitterStream, ObjectMapper objectMapper) {
StatusListener statusListener = new StatusListener() {
@@ -94,7 +90,7 @@ public class TwitterStreamSupplierConfiguration {
Message<byte[]> message = MessageBuilder.withPayload(json.getBytes())
.setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_JSON_VALUE)
.build();
twitterStatusInputChannel.send(message);
TwitterStreamSupplierConfiguration.this.twitterStatusInputChannel.send(message);
}
catch (JsonProcessingException ex) {
String errorMessage = "Status to JSON conversion error!";
@@ -116,23 +112,15 @@ public class TwitterStreamSupplierConfiguration {
@Bean
public Supplier<Flux<Message<?>>> twitterStreamSupplier(TwitterStream twitterStream,
FluxMessageChannel twitterStatusInputChannel, TwitterStreamSupplierProperties streamProperties) {
TwitterStreamSupplierProperties streamProperties) {
return () -> Flux.from(twitterStatusInputChannel).doOnSubscribe((subscription) -> {
return () -> Flux.from(this.twitterStatusInputChannel).doOnSubscribe((subscription) -> {
try {
switch (streamProperties.getType()) {
case filter -> {
twitterStream.filter(streamProperties.getFilter().toFilterQuery());
}
case sample -> {
twitterStream.sample();
}
case firehose -> {
twitterStream.firehose(streamProperties.getFilter().getCount());
}
case link -> {
twitterStream.links(streamProperties.getFilter().getCount());
}
case filter -> twitterStream.filter(streamProperties.getFilter().toFilterQuery());
case sample -> twitterStream.sample();
case firehose -> twitterStream.firehose(streamProperties.getFilter().getCount());
case link -> twitterStream.links(streamProperties.getFilter().getCount());
default -> throw new IllegalArgumentException("Unknown stream type:" + streamProperties.getType());
}
}