Search in sources :

Example 1 with AbstractMessageChannel

use of org.springframework.integration.channel.AbstractMessageChannel in project spring-cloud-stream by spring-cloud.

the class TestChannelBinderProvisioner method provisionDestination.

private SubscribableChannel provisionDestination(String name, boolean pubSub) {
    String destinationName = name + ".destination";
    SubscribableChannel destination = this.provisionedDestinations.get(destinationName);
    if (destination == null) {
        destination = pubSub ? new PublishSubscribeChannel() : new DirectChannel();
        ((AbstractMessageChannel) destination).setBeanName(destinationName);
        ((AbstractMessageChannel) destination).setComponentName(destinationName);
        this.provisionedDestinations.put(destinationName, destination);
    }
    return destination;
}
Also used : PublishSubscribeChannel(org.springframework.integration.channel.PublishSubscribeChannel) AbstractMessageChannel(org.springframework.integration.channel.AbstractMessageChannel) DirectChannel(org.springframework.integration.channel.DirectChannel) SubscribableChannel(org.springframework.messaging.SubscribableChannel)

Example 2 with AbstractMessageChannel

use of org.springframework.integration.channel.AbstractMessageChannel 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 3 with AbstractMessageChannel

use of org.springframework.integration.channel.AbstractMessageChannel in project spring-cloud-stream by spring-cloud.

the class MessageConverterConfigurer method configureMessageChannel.

/**
 * Setup data-type and message converters for the given message channel.
 *
 * @param channel message channel to set the data-type and message converters
 * @param channelName the channel name
 * @param inbound inbound (i.e., "input") or outbound channel
 */
private void configureMessageChannel(MessageChannel channel, String channelName, boolean inbound) {
    Assert.isAssignable(AbstractMessageChannel.class, channel.getClass());
    AbstractMessageChannel messageChannel = (AbstractMessageChannel) channel;
    BindingProperties bindingProperties = this.bindingServiceProperties.getBindingProperties(channelName);
    String contentType = bindingProperties.getContentType();
    ProducerProperties producerProperties = bindingProperties.getProducer();
    if (!inbound && producerProperties != null && producerProperties.isPartitioned()) {
        messageChannel.addInterceptor(new PartitioningInterceptor(bindingProperties, getPartitionKeyExtractorStrategy(producerProperties), getPartitionSelectorStrategy(producerProperties)));
    }
    ConsumerProperties consumerProperties = bindingProperties.getConsumer();
    if (this.isNativeEncodingNotSet(producerProperties, consumerProperties, inbound)) {
        if (inbound) {
            messageChannel.addInterceptor(new InboundContentTypeEnhancingInterceptor(contentType));
        } else {
            messageChannel.addInterceptor(new OutboundContentTypeConvertingInterceptor(contentType, this.compositeMessageConverterFactory.getMessageConverterForAllRegistered()));
        }
    }
}
Also used : ProducerProperties(org.springframework.cloud.stream.binder.ProducerProperties) AbstractMessageChannel(org.springframework.integration.channel.AbstractMessageChannel) BindingProperties(org.springframework.cloud.stream.config.BindingProperties) ConsumerProperties(org.springframework.cloud.stream.binder.ConsumerProperties)

Example 4 with AbstractMessageChannel

use of org.springframework.integration.channel.AbstractMessageChannel in project spring-integration by spring-projects.

the class EnableIntegrationTests method testParentChildAnnotationConfigurationFromAnotherPackage.

@Test
public void testParentChildAnnotationConfigurationFromAnotherPackage() {
    AnnotationConfigApplicationContext child = new AnnotationConfigApplicationContext();
    child.register(org.springframework.integration.configuration2.ChildConfiguration.class);
    child.setParent(this.context);
    child.refresh();
    AbstractMessageChannel foo = child.getBean("foo", AbstractMessageChannel.class);
    ChannelInterceptor baz = child.getBean("baz", ChannelInterceptor.class);
    assertTrue(foo.getChannelInterceptors().contains(baz));
    assertFalse(this.output.getChannelInterceptors().contains(baz));
    child.close();
}
Also used : AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) AbstractMessageChannel(org.springframework.integration.channel.AbstractMessageChannel) GlobalChannelInterceptor(org.springframework.integration.config.GlobalChannelInterceptor) ChannelInterceptor(org.springframework.messaging.support.ChannelInterceptor) Test(org.junit.Test)

Example 5 with AbstractMessageChannel

use of org.springframework.integration.channel.AbstractMessageChannel in project spring-integration by spring-projects.

the class ChannelInterceptorTests method afterCompletionWithSendException.

@Test
public void afterCompletionWithSendException() {
    final AbstractMessageChannel testChannel = new AbstractMessageChannel() {

        @Override
        protected boolean doSend(Message<?> message, long timeout) {
            throw new RuntimeException("Simulated exception");
        }
    };
    AfterCompletionTestInterceptor interceptor1 = new AfterCompletionTestInterceptor();
    AfterCompletionTestInterceptor interceptor2 = new AfterCompletionTestInterceptor();
    testChannel.addInterceptor(interceptor1);
    testChannel.addInterceptor(interceptor2);
    try {
        testChannel.send(MessageBuilder.withPayload("test").build());
    } catch (Exception ex) {
        assertEquals("Simulated exception", ex.getCause().getMessage());
    }
    assertTrue(interceptor1.wasAfterCompletionInvoked());
    assertTrue(interceptor2.wasAfterCompletionInvoked());
}
Also used : AbstractMessageChannel(org.springframework.integration.channel.AbstractMessageChannel) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) Test(org.junit.Test)

Aggregations

AbstractMessageChannel (org.springframework.integration.channel.AbstractMessageChannel)19 Test (org.junit.Test)16 JpaExecutor (org.springframework.integration.jpa.core.JpaExecutor)7 JpaOperations (org.springframework.integration.jpa.core.JpaOperations)7 MessageHandler (org.springframework.messaging.MessageHandler)4 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)3 DirectChannel (org.springframework.integration.channel.DirectChannel)3 PersistMode (org.springframework.integration.jpa.support.PersistMode)3 LiteralExpression (org.springframework.expression.common.LiteralExpression)2 GlobalChannelInterceptor (org.springframework.integration.config.GlobalChannelInterceptor)2 EventDrivenConsumer (org.springframework.integration.endpoint.EventDrivenConsumer)2 AbstractReplyProducingMessageHandler (org.springframework.integration.handler.AbstractReplyProducingMessageHandler)2 JpaOutboundGateway (org.springframework.integration.jpa.outbound.JpaOutboundGateway)2 JpaParameter (org.springframework.integration.jpa.support.JpaParameter)2 OutboundGatewayType (org.springframework.integration.jpa.support.OutboundGatewayType)2 Message (org.springframework.messaging.Message)2 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1