Search in sources :

Example 6 with ChannelInterceptorAdapter

use of org.springframework.messaging.support.ChannelInterceptorAdapter in project spring-integration by spring-projects.

the class TestServerConfig method clientOutboundChannel.

@Bean
public AbstractSubscribableChannel clientOutboundChannel() {
    DirectChannel directChannel = new DirectChannel();
    directChannel.addInterceptor(new ChannelInterceptorAdapter() {

        @Override
        public Message<?> preSend(Message<?> message, MessageChannel channel) {
            SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.wrap(message);
            headers.setLeaveMutable(true);
            return MessageBuilder.createMessage(message.getPayload(), headers.getMessageHeaders());
        }
    });
    return directChannel;
}
Also used : ChannelInterceptorAdapter(org.springframework.messaging.support.ChannelInterceptorAdapter) MessageChannel(org.springframework.messaging.MessageChannel) Message(org.springframework.messaging.Message) DirectChannel(org.springframework.integration.channel.DirectChannel) SimpMessageHeaderAccessor(org.springframework.messaging.simp.SimpMessageHeaderAccessor) Bean(org.springframework.context.annotation.Bean)

Example 7 with ChannelInterceptorAdapter

use of org.springframework.messaging.support.ChannelInterceptorAdapter in project spring-integration-samples by spring-projects.

the class ApplicationTests method testWebSockets.

@Test
public void testWebSockets() throws InterruptedException {
    System.setProperty("local.server.port", this.port);
    ConfigurableApplicationContext ctx = new ClassPathXmlApplicationContext("client-context.xml", org.springframework.integration.samples.websocket.standard.client.Application.class);
    DirectChannel webSocketInputChannel = ctx.getBean("webSocketInputChannel", DirectChannel.class);
    final CountDownLatch stopLatch = new CountDownLatch(2);
    webSocketInputChannel.addInterceptor(new ChannelInterceptorAdapter() {

        @Override
        public void postSend(Message<?> message, MessageChannel channel, boolean sent) {
            Object payload = message.getPayload();
            assertThat(payload, instanceOf(String.class));
            Date date = null;
            try {
                date = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.DEFAULT).parse((String) payload);
            } catch (ParseException e) {
                fail("fail to parse date");
            }
            assertThat(new Date().compareTo(date), greaterThanOrEqualTo(0));
            stopLatch.countDown();
        }
    });
    assertTrue(stopLatch.await(10, TimeUnit.SECONDS));
    ctx.close();
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) ChannelInterceptorAdapter(org.springframework.messaging.support.ChannelInterceptorAdapter) DirectChannel(org.springframework.integration.channel.DirectChannel) CountDownLatch(java.util.concurrent.CountDownLatch) Date(java.util.Date) MessageChannel(org.springframework.messaging.MessageChannel) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) ParseException(java.text.ParseException) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 8 with ChannelInterceptorAdapter

use of org.springframework.messaging.support.ChannelInterceptorAdapter in project spring-cloud-stream by spring-cloud.

the class PollableConsumerTests method testSimple.

@Test
public void testSimple() {
    TestChannelBinder binder = createBinder();
    MessageConverterConfigurer configurer = context.getBean(MessageConverterConfigurer.class);
    DefaultPollableMessageSource pollableSource = new DefaultPollableMessageSource(this.messageConverter);
    configurer.configurePolledMessageSource(pollableSource, "foo");
    pollableSource.addInterceptor(new ChannelInterceptorAdapter() {

        @Override
        public Message<?> preSend(Message<?> message, MessageChannel channel) {
            return MessageBuilder.withPayload(((String) message.getPayload()).toUpperCase()).copyHeaders(message.getHeaders()).build();
        }
    });
    ExtendedConsumerProperties<Object> properties = new ExtendedConsumerProperties<>(null);
    properties.setMaxAttempts(2);
    properties.setBackOffInitialInterval(0);
    binder.bindPollableConsumer("foo", "bar", pollableSource, properties);
    final AtomicInteger count = new AtomicInteger();
    assertThat(pollableSource.poll(received -> {
        assertThat(received.getPayload()).isEqualTo("POLLED DATA");
        assertThat(received.getHeaders().get(MessageHeaders.CONTENT_TYPE)).isEqualTo(MimeType.valueOf("text/plain"));
        if (count.incrementAndGet() == 1) {
            throw new RuntimeException("test retry");
        }
    })).isTrue();
    assertThat(count.get()).isEqualTo(2);
}
Also used : ChannelInterceptorAdapter(org.springframework.messaging.support.ChannelInterceptorAdapter) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) MessageChannel(org.springframework.messaging.MessageChannel) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MessageConverterConfigurer(org.springframework.cloud.stream.binding.MessageConverterConfigurer) TestChannelBinder(org.springframework.cloud.stream.binder.test.TestChannelBinder) Test(org.junit.Test)

Example 9 with ChannelInterceptorAdapter

use of org.springframework.messaging.support.ChannelInterceptorAdapter in project spring-cloud-stream by spring-cloud.

the class PollableConsumerTests method testErrorsNoRetry.

@Test
public void testErrorsNoRetry() {
    TestChannelBinder binder = createBinder();
    MessageConverterConfigurer configurer = context.getBean(MessageConverterConfigurer.class);
    DefaultPollableMessageSource pollableSource = new DefaultPollableMessageSource(this.messageConverter);
    configurer.configurePolledMessageSource(pollableSource, "foo");
    pollableSource.addInterceptor(new ChannelInterceptorAdapter() {

        @Override
        public Message<?> preSend(Message<?> message, MessageChannel channel) {
            return MessageBuilder.withPayload(((String) message.getPayload()).toUpperCase()).copyHeaders(message.getHeaders()).build();
        }
    });
    ExtendedConsumerProperties<Object> properties = new ExtendedConsumerProperties<>(null);
    properties.setMaxAttempts(1);
    binder.bindPollableConsumer("foo", "bar", pollableSource, properties);
    final CountDownLatch latch = new CountDownLatch(1);
    context.getBean(IntegrationContextUtils.ERROR_CHANNEL_BEAN_NAME, SubscribableChannel.class).subscribe(m -> {
        latch.countDown();
    });
    final AtomicInteger count = new AtomicInteger();
    assertThat(pollableSource.poll(received -> {
        count.incrementAndGet();
        throw new RuntimeException("test recoverer");
    })).isTrue();
    assertThat(count.get()).isEqualTo(1);
}
Also used : ChannelInterceptorAdapter(org.springframework.messaging.support.ChannelInterceptorAdapter) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) CountDownLatch(java.util.concurrent.CountDownLatch) MessageChannel(org.springframework.messaging.MessageChannel) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MessageConverterConfigurer(org.springframework.cloud.stream.binding.MessageConverterConfigurer) TestChannelBinder(org.springframework.cloud.stream.binder.test.TestChannelBinder) SubscribableChannel(org.springframework.messaging.SubscribableChannel) Test(org.junit.Test)

Example 10 with ChannelInterceptorAdapter

use of org.springframework.messaging.support.ChannelInterceptorAdapter in project spring-cloud-stream by spring-cloud.

the class PollableConsumerTests method testErrors.

@Test
public void testErrors() {
    TestChannelBinder binder = createBinder();
    MessageConverterConfigurer configurer = context.getBean(MessageConverterConfigurer.class);
    DefaultPollableMessageSource pollableSource = new DefaultPollableMessageSource(this.messageConverter);
    configurer.configurePolledMessageSource(pollableSource, "foo");
    pollableSource.addInterceptor(new ChannelInterceptorAdapter() {

        @Override
        public Message<?> preSend(Message<?> message, MessageChannel channel) {
            return MessageBuilder.withPayload(((String) message.getPayload()).toUpperCase()).copyHeaders(message.getHeaders()).build();
        }
    });
    ExtendedConsumerProperties<Object> properties = new ExtendedConsumerProperties<>(null);
    properties.setMaxAttempts(2);
    properties.setBackOffInitialInterval(0);
    binder.bindPollableConsumer("foo", "bar", pollableSource, properties);
    final CountDownLatch latch = new CountDownLatch(1);
    context.getBean(IntegrationContextUtils.ERROR_CHANNEL_BEAN_NAME, SubscribableChannel.class).subscribe(m -> {
        latch.countDown();
    });
    final AtomicInteger count = new AtomicInteger();
    assertThat(pollableSource.poll(received -> {
        count.incrementAndGet();
        throw new RuntimeException("test recoverer");
    })).isTrue();
    assertThat(count.get()).isEqualTo(2);
    Message<?> lastError = binder.getLastError();
    assertThat(lastError).isNotNull();
    assertThat(((Exception) lastError.getPayload()).getCause().getMessage()).isEqualTo("test recoverer");
}
Also used : ChannelInterceptorAdapter(org.springframework.messaging.support.ChannelInterceptorAdapter) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) CountDownLatch(java.util.concurrent.CountDownLatch) MessageChannel(org.springframework.messaging.MessageChannel) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MessageConverterConfigurer(org.springframework.cloud.stream.binding.MessageConverterConfigurer) TestChannelBinder(org.springframework.cloud.stream.binder.test.TestChannelBinder) SubscribableChannel(org.springframework.messaging.SubscribableChannel) Test(org.junit.Test)

Aggregations

MessageChannel (org.springframework.messaging.MessageChannel)16 ChannelInterceptorAdapter (org.springframework.messaging.support.ChannelInterceptorAdapter)16 Test (org.junit.Test)15 Message (org.springframework.messaging.Message)13 GenericMessage (org.springframework.messaging.support.GenericMessage)12 QueueChannel (org.springframework.integration.channel.QueueChannel)8 AtomicReference (java.util.concurrent.atomic.AtomicReference)7 TestService (org.springframework.integration.gateway.TestService)7 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 TestChannelBinder (org.springframework.cloud.stream.binder.test.TestChannelBinder)4 MessageConverterConfigurer (org.springframework.cloud.stream.binding.MessageConverterConfigurer)4 CountDownLatch (java.util.concurrent.CountDownLatch)3 AbstractMessageChannel (org.springframework.integration.channel.AbstractMessageChannel)3 DirectChannel (org.springframework.integration.channel.DirectChannel)2 MyCompletableFuture (org.springframework.integration.gateway.TestService.MyCompletableFuture)2 SubscribableChannel (org.springframework.messaging.SubscribableChannel)2 ParseException (java.text.ParseException)1 Date (java.util.Date)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Log (org.apache.commons.logging.Log)1