GH-2885: Channel used by StreamBridge missing name

Fixes https://github.com/spring-cloud/spring-cloud-stream/issues/2885

The `DirectWithAttributesChannel` used by `StreamBridge` is missing naming
information. Adding the proper application context and component name data
to the channel so that it is able to construct a name when queried.
This commit is contained in:
Soby Chacko
2024-01-19 15:45:51 -05:00
parent d63f873c30
commit e55de656eb
2 changed files with 24 additions and 2 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2020-2023 the original author or authors.
* Copyright 2020-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.
@@ -49,6 +49,7 @@ import org.springframework.cloud.stream.binder.test.TestChannelBinderConfigurati
import org.springframework.cloud.stream.binding.BindingService;
import org.springframework.cloud.stream.binding.NewDestinationBindingCallback;
import org.springframework.cloud.stream.config.BindingServiceProperties;
import org.springframework.cloud.stream.messaging.DirectWithAttributesChannel;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.integration.channel.AbstractMessageChannel;
@@ -394,6 +395,25 @@ public class StreamBridgeTests {
}
}
// See https://github.com/spring-cloud/spring-cloud-stream/issues/2885 for more context on the following test
@SuppressWarnings("unchecked")
@Test
void ensureDirectWithAttributesChannelIsPopulatedWithName() throws Exception {
try (ConfigurableApplicationContext context = new SpringApplicationBuilder(TestChannelBinderConfiguration
.getCompleteConfiguration(InterceptorConfiguration.class))
.web(WebApplicationType.NONE).run(
"--spring.jmx.enabled=false")) {
StreamBridge bridge = context.getBean(StreamBridge.class);
bridge.send("test-channel", "blah");
Field field = ReflectionUtils.findField(StreamBridge.class, "channelCache");
Objects.requireNonNull(field).setAccessible(true);
Map<String, MessageChannel> map = (Map<String, MessageChannel>) field.get(bridge);
final MessageChannel messageChannel = map.get("test-channel");
assertThat(((DirectWithAttributesChannel) messageChannel).getFullChannelName()).isEqualTo("application.test-channel");
}
}
@Test
void testInterceptorIsNotAddedMultipleTimesToTheMessageChannel() {
try (ConfigurableApplicationContext context = new SpringApplicationBuilder(TestChannelBinderConfiguration

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2020-2023 the original author or authors.
* Copyright 2020-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.
@@ -251,6 +251,8 @@ public final class StreamBridge implements StreamOperations, SmartInitializingSi
}
else {
messageChannel = new DirectWithAttributesChannel();
((DirectWithAttributesChannel) messageChannel).setApplicationContext(applicationContext);
((DirectWithAttributesChannel) messageChannel).setComponentName(destinationName);
if (this.destinationBindingCallback != null) {
Object extendedProducerProperties = this.bindingService
.getExtendedProducerProperties(messageChannel, destinationName);