Search in sources :

Example 1 with Bindable

use of org.springframework.cloud.stream.binding.Bindable in project spring-cloud-stream by spring-cloud.

the class BinderAwareChannelResolverTests method resolveChannel.

@Test
public void resolveChannel() {
    Map<String, Bindable> bindables = context.getBeansOfType(Bindable.class);
    assertThat(bindables).hasSize(1);
    for (Bindable bindable : bindables.values()) {
        // producer
        assertEquals(0, bindable.getInputs().size());
        // consumer
        assertEquals(0, bindable.getOutputs().size());
    }
    MessageChannel registered = resolver.resolveDestination("foo");
    assertEquals(2, ((AbstractMessageChannel) registered).getChannelInterceptors().size());
    assertTrue(((AbstractMessageChannel) registered).getChannelInterceptors().get(1) instanceof ImmutableMessageChannelInterceptor);
    bindables = context.getBeansOfType(Bindable.class);
    assertThat(bindables).hasSize(1);
    for (Bindable bindable : bindables.values()) {
        // producer
        assertEquals(0, bindable.getInputs().size());
        // consumer
        assertEquals(1, bindable.getOutputs().size());
    }
    DirectChannel testChannel = new DirectChannel();
    testChannel.setComponentName("INPUT");
    final CountDownLatch latch = new CountDownLatch(1);
    final List<Message<?>> received = new ArrayList<>();
    testChannel.subscribe(new MessageHandler() {

        @Override
        public void handleMessage(Message<?> message) throws MessagingException {
            received.add(message);
            latch.countDown();
        }
    });
    this.binder.bindConsumer("foo", null, testChannel, new ConsumerProperties());
    assertThat(received).hasSize(0);
    registered.send(MessageBuilder.withPayload("hello").build());
    try {
        assertThat(latch.await(1, TimeUnit.SECONDS)).describedAs("Latch timed out");
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        fail("interrupted while awaiting latch");
    }
    assertThat(received).hasSize(1);
    assertThat(new String((byte[]) received.get(0).getPayload())).isEqualTo("hello");
    this.context.close();
    for (Bindable bindable : bindables.values()) {
        assertEquals(0, bindable.getInputs().size());
        // Must not be bound"
        assertEquals(0, bindable.getOutputs().size());
    }
}
Also used : AbstractMessageChannel(org.springframework.integration.channel.AbstractMessageChannel) Message(org.springframework.messaging.Message) MessageHandler(org.springframework.messaging.MessageHandler) DirectChannel(org.springframework.integration.channel.DirectChannel) MessagingException(org.springframework.messaging.MessagingException) ImmutableMessageChannelInterceptor(org.springframework.messaging.support.ImmutableMessageChannelInterceptor) Bindable(org.springframework.cloud.stream.binding.Bindable) DynamicDestinationsBindable(org.springframework.cloud.stream.binding.DynamicDestinationsBindable) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) AbstractMessageChannel(org.springframework.integration.channel.AbstractMessageChannel) MessageChannel(org.springframework.messaging.MessageChannel) Test(org.junit.Test)

Example 2 with Bindable

use of org.springframework.cloud.stream.binding.Bindable in project spring-cloud-stream by spring-cloud.

the class ExtendedPropertiesBinderAwareChannelResolverTests method resolveChannel.

@Test
@Override
public void resolveChannel() {
    Map<String, Bindable> bindables = context.getBeansOfType(Bindable.class);
    assertThat(bindables).hasSize(1);
    for (Bindable bindable : bindables.values()) {
        // producer
        assertEquals(0, bindable.getInputs().size());
        // consumer
        assertEquals(0, bindable.getOutputs().size());
    }
    MessageChannel registered = resolver.resolveDestination("foo");
    bindables = context.getBeansOfType(Bindable.class);
    assertThat(bindables).hasSize(1);
    for (Bindable bindable : bindables.values()) {
        // producer
        assertEquals(0, bindable.getInputs().size());
        // consumer
        assertEquals(1, bindable.getOutputs().size());
    }
    DirectChannel testChannel = new DirectChannel();
    final CountDownLatch latch = new CountDownLatch(1);
    final List<Message<?>> received = new ArrayList<>();
    testChannel.subscribe(new MessageHandler() {

        @Override
        public void handleMessage(Message<?> message) throws MessagingException {
            received.add(message);
            latch.countDown();
        }
    });
    binder.bindConsumer("foo", null, testChannel, new ExtendedConsumerProperties<ConsumerProperties>(new ConsumerProperties()));
    assertThat(received).hasSize(0);
    registered.send(MessageBuilder.withPayload("hello").build());
    try {
        assertThat(latch.await(1, TimeUnit.SECONDS)).describedAs("latch timed out");
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        fail("interrupted while awaiting latch");
    }
    assertThat(received).hasSize(1);
    assertThat(new String((byte[]) received.get(0).getPayload())).isEqualTo("hello");
    context.close();
    for (Bindable bindable : bindables.values()) {
        assertEquals(0, bindable.getInputs().size());
        // Must not be bound"
        assertEquals(0, bindable.getOutputs().size());
    }
}
Also used : Message(org.springframework.messaging.Message) MessageHandler(org.springframework.messaging.MessageHandler) DirectChannel(org.springframework.integration.channel.DirectChannel) MessagingException(org.springframework.messaging.MessagingException) Bindable(org.springframework.cloud.stream.binding.Bindable) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) MessageChannel(org.springframework.messaging.MessageChannel) Test(org.junit.Test)

Example 3 with Bindable

use of org.springframework.cloud.stream.binding.Bindable in project spring-cloud-stream by spring-cloud.

the class ChannelsEndpoint method channels.

@ReadOperation
public Map<String, Object> channels() {
    ChannelsMetaData map = new ChannelsMetaData();
    Map<String, BindingProperties> inputs = map.getInputs();
    Map<String, BindingProperties> outputs = map.getOutputs();
    for (Bindable factory : this.adapters) {
        for (String name : factory.getInputs()) {
            inputs.put(name, this.properties.getBindingProperties(name));
        }
        for (String name : factory.getOutputs()) {
            outputs.put(name, this.properties.getBindingProperties(name));
        }
    }
    return new ObjectMapper().convertValue(map, new TypeReference<Map<String, Object>>() {
    });
}
Also used : BindingProperties(org.springframework.cloud.stream.config.BindingProperties) Bindable(org.springframework.cloud.stream.binding.Bindable) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ReadOperation(org.springframework.boot.actuate.endpoint.annotation.ReadOperation)

Aggregations

Bindable (org.springframework.cloud.stream.binding.Bindable)3 ArrayList (java.util.ArrayList)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 Test (org.junit.Test)2 DirectChannel (org.springframework.integration.channel.DirectChannel)2 Message (org.springframework.messaging.Message)2 MessageChannel (org.springframework.messaging.MessageChannel)2 MessageHandler (org.springframework.messaging.MessageHandler)2 MessagingException (org.springframework.messaging.MessagingException)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 ReadOperation (org.springframework.boot.actuate.endpoint.annotation.ReadOperation)1 DynamicDestinationsBindable (org.springframework.cloud.stream.binding.DynamicDestinationsBindable)1 BindingProperties (org.springframework.cloud.stream.config.BindingProperties)1 AbstractMessageChannel (org.springframework.integration.channel.AbstractMessageChannel)1 ImmutableMessageChannelInterceptor (org.springframework.messaging.support.ImmutableMessageChannelInterceptor)1