use of org.springframework.cloud.stream.config.BindingServiceProperties in project spring-cloud-stream by spring-cloud.
the class PollableConsumerTests method testConvertSimpler.
@Test
public void testConvertSimpler() {
TestChannelBinder binder = createBinder();
MessageConverterConfigurer configurer = context.getBean(MessageConverterConfigurer.class);
BindingServiceProperties bsps = this.context.getBean(BindingServiceProperties.class);
BindingProperties props = new BindingProperties();
props.setContentType("text/plain");
bsps.setBindings(Collections.singletonMap("foo", props));
binder.setMessageSourceDelegate(() -> new GenericMessage<>("foo".getBytes()));
DefaultPollableMessageSource pollableSource = new DefaultPollableMessageSource(this.messageConverter);
configurer.configurePolledMessageSource(pollableSource, "foo");
ExtendedConsumerProperties<Object> properties = new ExtendedConsumerProperties<>(null);
properties.setMaxAttempts(1);
properties.setBackOffInitialInterval(0);
binder.bindPollableConsumer("foo", "bar", pollableSource, properties);
final AtomicReference<Object> payload = new AtomicReference<>();
assertThat(pollableSource.poll(received -> {
payload.set(received.getPayload());
}, new ParameterizedTypeReference<String>() {
})).isTrue();
assertThat(payload.get()).isInstanceOf(String.class);
assertThat(payload.get()).isEqualTo("foo");
// test the cache for coverage
assertThat(pollableSource.poll(received -> {
payload.set(received.getPayload());
}, new ParameterizedTypeReference<String>() {
})).isTrue();
assertThat(payload.get()).isInstanceOf(String.class);
assertThat(payload.get()).isEqualTo("foo");
}
use of org.springframework.cloud.stream.config.BindingServiceProperties in project spring-cloud-stream by spring-cloud.
the class MessageConverterConfigurerTests method testConfigureOutputChannelCannotConvert.
@Test
@Ignore
public void testConfigureOutputChannelCannotConvert() {
BindingServiceProperties props = new BindingServiceProperties();
BindingProperties bindingProps = new BindingProperties();
bindingProps.setContentType("foo/bar");
props.setBindings(Collections.singletonMap("foo", bindingProps));
MessageConverter converter = new AbstractMessageConverter(new MimeType("foo", "bar")) {
@Override
protected boolean supports(Class<?> clazz) {
return true;
}
@Override
protected Object convertToInternal(Object payload, MessageHeaders headers, Object conversionHint) {
return null;
}
};
CompositeMessageConverterFactory converterFactory = new CompositeMessageConverterFactory(Collections.<MessageConverter>singletonList(converter), null);
MessageConverterConfigurer configurer = new MessageConverterConfigurer(props, converterFactory);
QueueChannel out = new QueueChannel();
configurer.configureOutputChannel(out, "foo");
try {
out.send(new GenericMessage<Foo>(new Foo(), Collections.<String, Object>singletonMap(MessageHeaders.CONTENT_TYPE, "bad/ct")));
fail("Expected MessageConversionException: " + out.receive(0));
} catch (MessageConversionException e) {
assertThat(e.getMessage()).endsWith("to the configured output type: 'foo/bar'");
}
}
use of org.springframework.cloud.stream.config.BindingServiceProperties 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.cloud.stream.config.BindingServiceProperties in project spring-cloud-stream by spring-cloud.
the class BindingServiceTests method testLateBindingConsumer.
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
// Inconsistent, needs fixing
@Ignore
public void testLateBindingConsumer() throws Exception {
BindingServiceProperties properties = new BindingServiceProperties();
properties.setBindingRetryInterval(1);
Map<String, BindingProperties> bindingProperties = new HashMap<>();
BindingProperties props = new BindingProperties();
props.setDestination("foo");
final String inputChannelName = "input";
bindingProperties.put(inputChannelName, props);
properties.setBindings(bindingProperties);
DefaultBinderFactory binderFactory = createMockBinderFactory();
Binder binder = binderFactory.getBinder("mock", MessageChannel.class);
ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
scheduler.initialize();
BindingService service = new BindingService(properties, binderFactory, scheduler);
MessageChannel inputChannel = new DirectChannel();
final Binding<MessageChannel> mockBinding = Mockito.mock(Binding.class);
final CountDownLatch fail = new CountDownLatch(2);
doAnswer(i -> {
fail.countDown();
if (fail.getCount() == 1) {
throw new RuntimeException("fail");
}
return mockBinding;
}).when(binder).bindConsumer(eq("foo"), isNull(), same(inputChannel), any(ConsumerProperties.class));
Collection<Binding<MessageChannel>> bindings = service.bindConsumer(inputChannel, inputChannelName);
assertThat(fail.await(10, TimeUnit.SECONDS)).isTrue();
assertThat(bindings).hasSize(1);
Binding<MessageChannel> delegate = TestUtils.getPropertyValue(bindings.iterator().next(), "delegate", Binding.class);
int n = 0;
while (n++ < 300 && delegate == null) {
Thread.sleep(100);
}
assertThat(delegate).isSameAs(mockBinding);
service.unbindConsumers(inputChannelName);
verify(binder, times(2)).bindConsumer(eq("foo"), isNull(), same(inputChannel), any(ConsumerProperties.class));
verify(delegate).unbind();
binderFactory.destroy();
}
use of org.springframework.cloud.stream.config.BindingServiceProperties in project spring-cloud-stream by spring-cloud.
the class BindingServiceTests method testExplicitGroup.
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testExplicitGroup() throws Exception {
BindingServiceProperties properties = new BindingServiceProperties();
Map<String, BindingProperties> bindingProperties = new HashMap<>();
BindingProperties props = new BindingProperties();
props.setDestination("foo");
props.setGroup("fooGroup");
final String inputChannelName = "input";
bindingProperties.put(inputChannelName, props);
properties.setBindings(bindingProperties);
DefaultBinderFactory binderFactory = createMockBinderFactory();
Binder binder = binderFactory.getBinder("mock", MessageChannel.class);
BindingService service = new BindingService(properties, binderFactory);
MessageChannel inputChannel = new DirectChannel();
Binding<MessageChannel> mockBinding = Mockito.mock(Binding.class);
when(binder.bindConsumer(eq("foo"), eq("fooGroup"), same(inputChannel), any(ConsumerProperties.class))).thenReturn(mockBinding);
Collection<Binding<MessageChannel>> bindings = service.bindConsumer(inputChannel, inputChannelName);
assertThat(bindings).hasSize(1);
Binding<MessageChannel> binding = bindings.iterator().next();
assertThat(binding).isSameAs(mockBinding);
service.unbindConsumers(inputChannelName);
verify(binder).bindConsumer(eq("foo"), eq(props.getGroup()), same(inputChannel), any(ConsumerProperties.class));
verify(binding).unbind();
binderFactory.destroy();
}
Aggregations