use of org.springframework.messaging.core.DestinationResolutionException in project spring-cloud-stream by spring-cloud.
the class BindingServiceTests method checkDynamicBinding.
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void checkDynamicBinding() {
BindingServiceProperties properties = new BindingServiceProperties();
BindingProperties bindingProperties = new BindingProperties();
bindingProperties.setProducer(new ProducerProperties());
properties.setBindings(Collections.singletonMap("foo", bindingProperties));
DefaultBinderFactory binderFactory = createMockBinderFactory();
final ExtendedPropertiesBinder binder = mock(ExtendedPropertiesBinder.class);
Properties extendedProps = new Properties();
when(binder.getExtendedProducerProperties(anyString())).thenReturn(extendedProps);
Binding<MessageChannel> mockBinding = Mockito.mock(Binding.class);
final AtomicReference<MessageChannel> dynamic = new AtomicReference<>();
when(binder.bindProducer(matches("foo"), any(DirectChannel.class), any(ProducerProperties.class))).thenReturn(mockBinding);
BindingService bindingService = new BindingService(properties, binderFactory) {
@Override
protected <T> Binder<T, ?, ?> getBinder(String channelName, Class<T> bindableType) {
return binder;
}
};
SubscribableChannelBindingTargetFactory bindableSubscribableChannelFactory = new SubscribableChannelBindingTargetFactory(new MessageConverterConfigurer(properties, new CompositeMessageConverterFactory()));
final AtomicBoolean callbackInvoked = new AtomicBoolean();
BinderAwareChannelResolver resolver = new BinderAwareChannelResolver(bindingService, bindableSubscribableChannelFactory, new DynamicDestinationsBindable(), (name, channel, props, extended) -> {
callbackInvoked.set(true);
assertThat(name).isEqualTo("foo");
assertThat(channel).isNotNull();
assertThat(props).isNotNull();
assertThat(extended).isSameAs(extendedProps);
props.setUseNativeEncoding(true);
extendedProps.setProperty("bar", "baz");
});
ConfigurableListableBeanFactory beanFactory = mock(ConfigurableListableBeanFactory.class);
when(beanFactory.getBean("foo", MessageChannel.class)).thenThrow(new NoSuchBeanDefinitionException(MessageChannel.class));
when(beanFactory.getBean("bar", MessageChannel.class)).thenThrow(new NoSuchBeanDefinitionException(MessageChannel.class));
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
dynamic.set(invocation.getArgument(1));
return null;
}
}).when(beanFactory).registerSingleton(eq("foo"), any(MessageChannel.class));
doAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
return dynamic.get();
}
}).when(beanFactory).initializeBean(any(MessageChannel.class), eq("foo"));
resolver.setBeanFactory(beanFactory);
MessageChannel resolved = resolver.resolveDestination("foo");
assertThat(resolved).isSameAs(dynamic.get());
ArgumentCaptor<ProducerProperties> captor = ArgumentCaptor.forClass(ProducerProperties.class);
verify(binder).bindProducer(eq("foo"), eq(dynamic.get()), captor.capture());
assertThat(captor.getValue().isUseNativeEncoding()).isTrue();
assertThat(captor.getValue()).isInstanceOf(ExtendedProducerProperties.class);
assertThat(((ExtendedProducerProperties) captor.getValue()).getExtension()).isSameAs(extendedProps);
doReturn(dynamic.get()).when(beanFactory).getBean("foo", MessageChannel.class);
properties.setDynamicDestinations(new String[] { "foo" });
resolved = resolver.resolveDestination("foo");
assertThat(resolved).isSameAs(dynamic.get());
properties.setDynamicDestinations(new String[] { "test" });
try {
resolved = resolver.resolveDestination("bar");
fail();
} catch (DestinationResolutionException e) {
assertThat(e).hasMessageContaining("Failed to find MessageChannel bean with name 'bar'");
}
}
use of org.springframework.messaging.core.DestinationResolutionException in project spring-cloud-stream by spring-cloud.
the class BinderAwareChannelResolver method resolveDestination.
@SuppressWarnings("unchecked")
@Override
public MessageChannel resolveDestination(String channelName) {
try {
return super.resolveDestination(channelName);
} catch (DestinationResolutionException e) {
// intentionally empty; will check again while holding the monitor
}
synchronized (this) {
BindingServiceProperties bindingServiceProperties = this.bindingService.getBindingServiceProperties();
String[] dynamicDestinations = bindingServiceProperties.getDynamicDestinations();
boolean dynamicAllowed = ObjectUtils.isEmpty(dynamicDestinations) || ObjectUtils.containsElement(dynamicDestinations, channelName);
try {
return super.resolveDestination(channelName);
} catch (DestinationResolutionException e) {
if (!dynamicAllowed) {
throw e;
}
}
MessageChannel channel = this.bindingTargetFactory.createOutput(channelName);
this.beanFactory.registerSingleton(channelName, channel);
this.instrumentChannelWithGlobalInterceptors(channel, channelName);
channel = (MessageChannel) this.beanFactory.initializeBean(channel, channelName);
if (this.newBindingCallback != null) {
ProducerProperties producerProperties = bindingServiceProperties.getProducerProperties(channelName);
Object extendedProducerProperties = this.bindingService.getExtendedProducerProperties(channel, channelName);
this.newBindingCallback.configure(channelName, channel, producerProperties, extendedProducerProperties);
bindingServiceProperties.updateProducerProperties(channelName, producerProperties);
}
Binding<MessageChannel> binding = this.bindingService.bindProducer(channel, channelName);
this.dynamicDestinationsBindable.addOutputBinding(channelName, binding);
return channel;
}
}
use of org.springframework.messaging.core.DestinationResolutionException in project spring-integration by spring-projects.
the class MessagingAnnotationPostProcessorChannelCreationTests method testDontCreateChannelWhenChannelHasBadDefinition.
@Test
public void testDontCreateChannelWhenChannelHasBadDefinition() {
ConfigurableListableBeanFactory beanFactory = mock(ConfigurableListableBeanFactory.class);
given(beanFactory.getBean("channel", MessageChannel.class)).willThrow(BeanCreationException.class);
willAnswer(invocation -> invocation.getArgument(0)).given(beanFactory).initializeBean(any(DirectChannel.class), eq("channel"));
willAnswer(invocation -> invocation.getArgument(0)).given(beanFactory).initializeBean(any(MessageHandler.class), eq("foo.foo.serviceActivator.handler"));
MessagingAnnotationPostProcessor mapp = new MessagingAnnotationPostProcessor();
mapp.setBeanFactory(beanFactory);
mapp.afterPropertiesSet();
try {
mapp.postProcessAfterInitialization(new Foo(), "foo");
fail("Expected a DestinationResolutionException");
} catch (DestinationResolutionException e) {
assertThat(e.getMessage(), containsString("A bean definition with name 'channel' exists, but failed to be created"));
}
}
use of org.springframework.messaging.core.DestinationResolutionException in project spring-integration by spring-projects.
the class HeaderChannelRegistryTests method testBFCRNoRegistry.
@Test
public void testBFCRNoRegistry() {
BeanFactoryChannelResolver resolver = new BeanFactoryChannelResolver();
BeanFactory beanFactory = mock(BeanFactory.class);
doAnswer(invocation -> {
throw new NoSuchBeanDefinitionException("bar");
}).when(beanFactory).getBean("foo", MessageChannel.class);
resolver.setBeanFactory(beanFactory);
try {
resolver.resolveDestination("foo");
fail("Expected exception");
} catch (DestinationResolutionException e) {
assertThat(e.getMessage(), Matchers.containsString("failed to look up MessageChannel with name 'foo' in the BeanFactory " + "(and there is no HeaderChannelRegistry present)."));
}
}
use of org.springframework.messaging.core.DestinationResolutionException in project spring-integration by spring-projects.
the class HeaderChannelRegistryTests method testBFCRWithRegistry.
@Test
public void testBFCRWithRegistry() {
BeanFactoryChannelResolver resolver = new BeanFactoryChannelResolver();
BeanFactory beanFactory = mock(BeanFactory.class);
when(beanFactory.getBean(IntegrationContextUtils.INTEGRATION_HEADER_CHANNEL_REGISTRY_BEAN_NAME, HeaderChannelRegistry.class)).thenReturn(mock(HeaderChannelRegistry.class));
doAnswer(invocation -> {
throw new NoSuchBeanDefinitionException("bar");
}).when(beanFactory).getBean("foo", MessageChannel.class);
resolver.setBeanFactory(beanFactory);
try {
resolver.resolveDestination("foo");
fail("Expected exception");
} catch (DestinationResolutionException e) {
assertThat(e.getMessage(), Matchers.containsString("failed to look up MessageChannel with name 'foo' in the BeanFactory."));
}
}
Aggregations