Java Code Examples for org.springframework.messaging.support.ExecutorSubscribableChannel#addInterceptor()

The following examples show how to use org.springframework.messaging.support.ExecutorSubscribableChannel#addInterceptor() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StompSubProtocolHandlerTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test // SPR-14690
public void handleMessageFromClientWithTokenAuthentication() {
	ExecutorSubscribableChannel channel = new ExecutorSubscribableChannel();
	channel.addInterceptor(new AuthenticationInterceptor("[email protected]"));
	channel.addInterceptor(new ImmutableMessageChannelInterceptor());

	TestMessageHandler messageHandler = new TestMessageHandler();
	channel.subscribe(messageHandler);

	StompSubProtocolHandler handler = new StompSubProtocolHandler();
	handler.afterSessionStarted(this.session, channel);

	TextMessage wsMessage = StompTextMessageBuilder.create(StompCommand.CONNECT).build();
	handler.handleMessageFromClient(this.session, wsMessage, channel);

	assertEquals(1, messageHandler.getMessages().size());
	Message<?> message = messageHandler.getMessages().get(0);
	Principal user = SimpMessageHeaderAccessor.getUser(message.getHeaders());
	assertNotNull(user);
	assertEquals("[email protected]", user.getName());
}
 
Example 2
Source File: StompSubProtocolHandlerTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test // SPR-14690
public void handleMessageFromClientWithTokenAuthentication() {
	ExecutorSubscribableChannel channel = new ExecutorSubscribableChannel();
	channel.addInterceptor(new AuthenticationInterceptor("[email protected]"));
	channel.addInterceptor(new ImmutableMessageChannelInterceptor());

	TestMessageHandler messageHandler = new TestMessageHandler();
	channel.subscribe(messageHandler);

	StompSubProtocolHandler handler = new StompSubProtocolHandler();
	handler.afterSessionStarted(this.session, channel);

	TextMessage wsMessage = StompTextMessageBuilder.create(StompCommand.CONNECT).build();
	handler.handleMessageFromClient(this.session, wsMessage, channel);

	assertEquals(1, messageHandler.getMessages().size());
	Message<?> message = messageHandler.getMessages().get(0);
	Principal user = SimpMessageHeaderAccessor.getUser(message.getHeaders());
	assertNotNull(user);
	assertEquals("[email protected]", user.getName());
}
 
Example 3
Source File: TracingChannelInterceptorTest.java    From spring-cloud-sleuth with Apache License 2.0 5 votes vote down vote up
@Test
public void subscriber_startsAndStopsConsumerAndProcessingSpan() {
	ExecutorSubscribableChannel channel = new ExecutorSubscribableChannel();
	channel.addInterceptor(executorSideOnly(this.interceptor));
	List<Message<?>> messages = new ArrayList<>();
	channel.subscribe(messages::add);

	channel.send(MessageBuilder.withPayload("foo").build());

	assertThat(messages.get(0).getHeaders()).doesNotContainKeys("b3",
			"nativeHeaders");
	assertThat(this.spans).extracting(MutableSpan::kind)
			.containsExactly(Span.Kind.CONSUMER, null);
}
 
Example 4
Source File: TracingChannelInterceptorTest.java    From spring-cloud-sleuth with Apache License 2.0 5 votes vote down vote up
/**
 * The subscriber consumes a message then synchronously processes it. Since we only
 * inject trace IDs on unprocessed messages, we remove IDs to prevent accidental
 * re-use of the same span.
 */
@Test
public void subscriber_removesTraceIdsFromMessage() {
	ExecutorSubscribableChannel channel = new ExecutorSubscribableChannel();
	channel.addInterceptor(this.interceptor);
	List<Message<?>> messages = new ArrayList<>();
	channel.subscribe(messages::add);

	channel.send(MessageBuilder.withPayload("foo").build());

	assertThat(messages.get(0).getHeaders()).doesNotContainKeys("b3");
}
 
Example 5
Source File: TracingChannelInterceptorTest.java    From spring-cloud-sleuth with Apache License 2.0 5 votes vote down vote up
@Test
public void subscriber_removesTraceIdsFromMessage_nativeHeaders() {
	ExecutorSubscribableChannel channel = new ExecutorSubscribableChannel();
	channel.addInterceptor(this.interceptor);
	List<Message<?>> messages = new ArrayList<>();
	channel.subscribe(messages::add);

	channel.send(MessageBuilder.withPayload("foo").build());

	assertThat((Map) messages.get(0).getHeaders().get(NATIVE_HEADERS))
			.doesNotContainKeys("b3");
}
 
Example 6
Source File: TracingChannelInterceptorTest.java    From spring-cloud-sleuth with Apache License 2.0 5 votes vote down vote up
@Test
public void integrated_sendAndSubscriber() {
	ExecutorSubscribableChannel channel = new ExecutorSubscribableChannel();
	channel.addInterceptor(this.interceptor);
	List<Message<?>> messages = new ArrayList<>();
	channel.subscribe(messages::add);

	channel.send(MessageBuilder.withPayload("foo").build());

	assertThat(this.spans).extracting(MutableSpan::kind)
			.containsExactly(Span.Kind.CONSUMER, null, Span.Kind.PRODUCER);
}
 
Example 7
Source File: TracingChannelInterceptorTest.java    From spring-cloud-sleuth with Apache License 2.0 5 votes vote down vote up
@Test
public void should_store_kafka_as_remote_service_name_when_kafka_header_is_present() {
	ExecutorSubscribableChannel channel = new ExecutorSubscribableChannel();
	channel.addInterceptor(this.interceptor);
	List<Message<?>> messages = new ArrayList<>();
	channel.subscribe(messages::add);

	Map<String, Object> headers = new HashMap<>();
	headers.put(KafkaHeaders.MESSAGE_KEY, "hello");
	channel.send(MessageBuilder.createMessage("foo", new MessageHeaders(headers)));

	assertThat(this.spans).extracting(MutableSpan::remoteServiceName)
			.contains("kafka");
}
 
Example 8
Source File: TracingChannelInterceptorTest.java    From spring-cloud-sleuth with Apache License 2.0 5 votes vote down vote up
@Test
public void should_store_rabbitmq_as_remote_service_name_when_rabbit_header_is_present() {
	ExecutorSubscribableChannel channel = new ExecutorSubscribableChannel();
	channel.addInterceptor(this.interceptor);
	List<Message<?>> messages = new ArrayList<>();
	channel.subscribe(messages::add);

	Map<String, Object> headers = new HashMap<>();
	headers.put(AmqpHeaders.RECEIVED_ROUTING_KEY, "hello");
	channel.send(MessageBuilder.createMessage("foo", new MessageHeaders(headers)));

	assertThat(this.spans).extracting(MutableSpan::remoteServiceName)
			.contains("rabbitmq");
}
 
Example 9
Source File: TracingChannelInterceptorTest.java    From spring-cloud-sleuth with Apache License 2.0 5 votes vote down vote up
@Test
public void should_store_broker_as_remote_service_name_when_no_special_headers_were_found() {
	ExecutorSubscribableChannel channel = new ExecutorSubscribableChannel();
	channel.addInterceptor(this.interceptor);
	List<Message<?>> messages = new ArrayList<>();
	channel.subscribe(messages::add);

	Map<String, Object> headers = new HashMap<>();
	channel.send(MessageBuilder.createMessage("foo", new MessageHeaders(headers)));

	assertThat(this.spans).extracting(MutableSpan::remoteServiceName)
			.containsOnly("broker", null);
}