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;
}
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();
}
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);
}
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);
}
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");
}
Aggregations