use of org.springframework.messaging.support.ChannelInterceptor in project spring-cloud-stream by spring-cloud.
the class DefaultPollableMessageSource method setSource.
public void setSource(MessageSource<?> source) {
ProxyFactory pf = new ProxyFactory(source);
class ReceiveAdvice implements MethodInterceptor {
private final List<ChannelInterceptor> interceptors = new ArrayList<>();
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
Object result = invocation.proceed();
if (result instanceof Message) {
Message<?> received = (Message<?>) result;
for (ChannelInterceptor interceptor : this.interceptors) {
received = interceptor.preSend(received, null);
if (received == null) {
return null;
}
}
return received;
}
return result;
}
}
final ReceiveAdvice advice = new ReceiveAdvice();
advice.interceptors.addAll(this.interceptors);
NameMatchMethodPointcutAdvisor sourceAdvisor = new NameMatchMethodPointcutAdvisor(advice);
sourceAdvisor.addMethodName("receive");
pf.addAdvisor(sourceAdvisor);
this.source = (MessageSource<?>) pf.getProxy();
}
use of org.springframework.messaging.support.ChannelInterceptor in project spring-framework by spring-projects.
the class TestValidator method testChannel.
private void testChannel(String channelName, List<Class<? extends MessageHandler>> subscriberTypes, int interceptorCount) {
AbstractSubscribableChannel channel = this.appContext.getBean(channelName, AbstractSubscribableChannel.class);
for (Class<? extends MessageHandler> subscriberType : subscriberTypes) {
MessageHandler subscriber = this.appContext.getBean(subscriberType);
assertThat(subscriber).as("No subscription for " + subscriberType).isNotNull();
assertThat(channel.hasSubscription(subscriber)).isTrue();
}
List<ChannelInterceptor> interceptors = channel.getInterceptors();
assertThat(interceptors.size()).isEqualTo(interceptorCount);
assertThat(interceptors.get(interceptors.size() - 1).getClass()).isEqualTo(ImmutableMessageChannelInterceptor.class);
}
use of org.springframework.messaging.support.ChannelInterceptor in project spring-framework by spring-projects.
the class WebSocketMessageBrokerConfigurationSupportTests method clientInboundChannelSendMessage.
@Test
void clientInboundChannelSendMessage() throws Exception {
ApplicationContext context = createContext(TestChannelConfig.class, TestConfigurer.class);
TestChannel channel = context.getBean("clientInboundChannel", TestChannel.class);
SubProtocolWebSocketHandler webSocketHandler = context.getBean(SubProtocolWebSocketHandler.class);
List<ChannelInterceptor> interceptors = channel.getInterceptors();
assertThat(interceptors.get(interceptors.size() - 1).getClass()).isEqualTo(ImmutableMessageChannelInterceptor.class);
TestWebSocketSession session = new TestWebSocketSession("s1");
session.setOpen(true);
webSocketHandler.afterConnectionEstablished(session);
webSocketHandler.handleMessage(session, StompTextMessageBuilder.create(StompCommand.SEND).headers("destination:/foo").build());
Message<?> message = channel.messages.get(0);
StompHeaderAccessor accessor = StompHeaderAccessor.getAccessor(message, StompHeaderAccessor.class);
assertThat(accessor).isNotNull();
assertThat(accessor.isMutable()).isFalse();
assertThat(accessor.getMessageType()).isEqualTo(SimpMessageType.MESSAGE);
assertThat(accessor.getDestination()).isEqualTo("/foo");
}
use of org.springframework.messaging.support.ChannelInterceptor in project spring-framework by spring-projects.
the class WebSocketMessageBrokerConfigurationSupportTests method clientOutboundChannel.
@Test
void clientOutboundChannel() {
ApplicationContext context = createContext(TestChannelConfig.class, TestConfigurer.class);
TestChannel channel = context.getBean("clientOutboundChannel", TestChannel.class);
Set<MessageHandler> handlers = channel.getSubscribers();
List<ChannelInterceptor> interceptors = channel.getInterceptors();
assertThat(interceptors.get(interceptors.size() - 1).getClass()).isEqualTo(ImmutableMessageChannelInterceptor.class);
assertThat(handlers).hasSize(1);
assertThat(handlers).contains(context.getBean(SubProtocolWebSocketHandler.class));
}
use of org.springframework.messaging.support.ChannelInterceptor in project spring-cloud-stream by spring-cloud.
the class CustomPartitionedProducerTest method testCustomPartitionedProducerByName.
@Test
public void testCustomPartitionedProducerByName() {
ApplicationContext context = SpringApplication.run(CustomPartitionedProducerTest.TestSource.class, "--spring.jmx.enabled=false", "--spring.main.web-application-type=none", "--spring.cloud.stream.bindings.output.producer.partitionKeyExtractorName=customPartitionKeyExtractor", "--spring.cloud.stream.bindings.output.producer.partitionSelectorName=customPartitionSelector");
Source testSource = context.getBean(Source.class);
DirectChannel messageChannel = (DirectChannel) testSource.output();
for (ChannelInterceptor channelInterceptor : messageChannel.getChannelInterceptors()) {
if (channelInterceptor instanceof MessageConverterConfigurer.PartitioningInterceptor) {
Field partitionHandlerField = ReflectionUtils.findField(MessageConverterConfigurer.PartitioningInterceptor.class, "partitionHandler");
ReflectionUtils.makeAccessible(partitionHandlerField);
PartitionHandler partitionHandler = (PartitionHandler) ReflectionUtils.getField(partitionHandlerField, channelInterceptor);
Field partitonKeyExtractorField = ReflectionUtils.findField(PartitionHandler.class, "partitionKeyExtractorStrategy");
ReflectionUtils.makeAccessible(partitonKeyExtractorField);
Field partitonSelectorField = ReflectionUtils.findField(PartitionHandler.class, "partitionSelectorStrategy");
ReflectionUtils.makeAccessible(partitonSelectorField);
Assert.assertTrue(((PartitionKeyExtractorStrategy) ReflectionUtils.getField(partitonKeyExtractorField, partitionHandler)).getClass().equals(CustomPartitionKeyExtractorClass.class));
Assert.assertTrue(((PartitionSelectorStrategy) ReflectionUtils.getField(partitonSelectorField, partitionHandler)).getClass().equals(CustomPartitionSelectorClass.class));
}
}
}
Aggregations