Java Code Examples for org.springframework.messaging.support.ExecutorSubscribableChannel

The following examples show how to use org.springframework.messaging.support.ExecutorSubscribableChannel. These examples are extracted from open source projects. 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
@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]ail.com", user.getName());
}
 
Example 2
@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 Project: spring-cloud-consul   Source File: TestConsumer.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void run(ApplicationArguments args) throws Exception {
	logger.info("Consumer running with binder {}", this.binder);
	SubscribableChannel consumerChannel = new ExecutorSubscribableChannel();
	consumerChannel.subscribe(new MessageHandler() {
		@Override
		public void handleMessage(Message<?> message) throws MessagingException {
			TestConsumer.this.messagePayload = (String) message.getPayload();
			logger.info("Received message: {}", TestConsumer.this.messagePayload);
		}
	});
	String group = null;

	if (args.containsOption("group")) {
		group = args.getOptionValues("group").get(0);
	}

	this.binder.bindConsumer(ConsulBinderTests.BINDING_NAME, group, consumerChannel,
			new ConsumerProperties());
	this.isBound = true;
}
 
Example 4
@Bean
public AbstractSubscribableChannel clientInboundChannel() {
	ExecutorSubscribableChannel channel = new ExecutorSubscribableChannel(clientInboundChannelExecutor());
	channel.setLogger(SimpLogging.forLog(channel.getLogger()));
	ChannelRegistration reg = getClientInboundChannelRegistration();
	if (reg.hasInterceptors()) {
		channel.setInterceptors(reg.getInterceptors());
	}
	return channel;
}
 
Example 5
@Bean
public AbstractSubscribableChannel clientOutboundChannel() {
	ExecutorSubscribableChannel channel = new ExecutorSubscribableChannel(clientOutboundChannelExecutor());
	channel.setLogger(SimpLogging.forLog(channel.getLogger()));
	ChannelRegistration reg = getClientOutboundChannelRegistration();
	if (reg.hasInterceptors()) {
		channel.setInterceptors(reg.getInterceptors());
	}
	return channel;
}
 
Example 6
@Bean
public AbstractSubscribableChannel brokerChannel() {
	ChannelRegistration reg = getBrokerRegistry().getBrokerChannelRegistration();
	ExecutorSubscribableChannel channel = (reg.hasTaskExecutor() ?
			new ExecutorSubscribableChannel(brokerChannelExecutor()) : new ExecutorSubscribableChannel());
	reg.interceptors(new ImmutableMessageChannelInterceptor());
	channel.setLogger(SimpLogging.forLog(channel.getLogger()));
	channel.setInterceptors(reg.getInterceptors());
	return channel;
}
 
Example 7
@Before
public void setup() throws Exception {
	logger.debug("Setting up before '" + this.testName.getMethodName() + "'");
	this.port = SocketUtils.findAvailableTcpPort(61613);
	this.responseChannel = new ExecutorSubscribableChannel();
	this.responseHandler = new TestMessageHandler();
	this.responseChannel.subscribe(this.responseHandler);
	this.eventPublisher = new TestEventPublisher();
	startActiveMqBroker();
	createAndStartRelay();
}
 
Example 8
Source Project: spring-analysis-note   Source File: OrderedMessageSenderTests.java    License: MIT License 5 votes vote down vote up
@Before
public void setup() {
	this.executor = new ThreadPoolTaskExecutor();
	this.executor.setCorePoolSize(Runtime.getRuntime().availableProcessors() * 2);
	this.executor.setAllowCoreThreadTimeOut(true);
	this.executor.afterPropertiesSet();

	this.channel = new ExecutorSubscribableChannel(this.executor);
	OrderedMessageSender.configureOutboundChannel(this.channel, true);

	this.sender = new OrderedMessageSender(this.channel, logger);

}
 
Example 9
@Test
public void sendAndReceive() {
	SubscribableChannel channel = new ExecutorSubscribableChannel(this.executor);
	channel.subscribe(new MessageHandler() {
		@Override
		public void handleMessage(Message<?> message) throws MessagingException {
			MessageChannel replyChannel = (MessageChannel) message.getHeaders().getReplyChannel();
			replyChannel.send(new GenericMessage<>("response"));
		}
	});

	String actual = this.template.convertSendAndReceive(channel, "request", String.class);
	assertEquals("response", actual);
}
 
Example 10
@Bean
public AbstractSubscribableChannel clientInboundChannel() {
	ExecutorSubscribableChannel channel = new ExecutorSubscribableChannel(clientInboundChannelExecutor());
	channel.setLogger(SimpLogging.forLog(channel.getLogger()));
	ChannelRegistration reg = getClientInboundChannelRegistration();
	if (reg.hasInterceptors()) {
		channel.setInterceptors(reg.getInterceptors());
	}
	return channel;
}
 
Example 11
@Bean
public AbstractSubscribableChannel clientOutboundChannel() {
	ExecutorSubscribableChannel channel = new ExecutorSubscribableChannel(clientOutboundChannelExecutor());
	channel.setLogger(SimpLogging.forLog(channel.getLogger()));
	ChannelRegistration reg = getClientOutboundChannelRegistration();
	if (reg.hasInterceptors()) {
		channel.setInterceptors(reg.getInterceptors());
	}
	return channel;
}
 
Example 12
@Bean
public AbstractSubscribableChannel brokerChannel() {
	ChannelRegistration reg = getBrokerRegistry().getBrokerChannelRegistration();
	ExecutorSubscribableChannel channel = (reg.hasTaskExecutor() ?
			new ExecutorSubscribableChannel(brokerChannelExecutor()) : new ExecutorSubscribableChannel());
	reg.interceptors(new ImmutableMessageChannelInterceptor());
	channel.setLogger(SimpLogging.forLog(channel.getLogger()));
	channel.setInterceptors(reg.getInterceptors());
	return channel;
}
 
Example 13
@Before
public void setup() throws Exception {
	logger.debug("Setting up before '" + this.testName.getMethodName() + "'");
	this.port = SocketUtils.findAvailableTcpPort(61613);
	this.responseChannel = new ExecutorSubscribableChannel();
	this.responseHandler = new TestMessageHandler();
	this.responseChannel.subscribe(this.responseHandler);
	this.eventPublisher = new TestEventPublisher();
	startActiveMqBroker();
	createAndStartRelay();
}
 
Example 14
Source Project: java-technology-stack   Source File: OrderedMessageSenderTests.java    License: MIT License 5 votes vote down vote up
@Before
public void setup() {
	this.executor = new ThreadPoolTaskExecutor();
	this.executor.setCorePoolSize(Runtime.getRuntime().availableProcessors() * 2);
	this.executor.setAllowCoreThreadTimeOut(true);
	this.executor.afterPropertiesSet();

	this.channel = new ExecutorSubscribableChannel(this.executor);
	OrderedMessageSender.configureOutboundChannel(this.channel, true);

	this.sender = new OrderedMessageSender(this.channel, logger);

}
 
Example 15
@Test
public void sendAndReceive() {
	SubscribableChannel channel = new ExecutorSubscribableChannel(this.executor);
	channel.subscribe(new MessageHandler() {
		@Override
		public void handleMessage(Message<?> message) throws MessagingException {
			MessageChannel replyChannel = (MessageChannel) message.getHeaders().getReplyChannel();
			replyChannel.send(new GenericMessage<>("response"));
		}
	});

	String actual = this.template.convertSendAndReceive(channel, "request", String.class);
	assertEquals("response", actual);
}
 
Example 16
@Bean
public AbstractSubscribableChannel clientInboundChannel() {
	ExecutorSubscribableChannel channel = new ExecutorSubscribableChannel(clientInboundChannelExecutor());
	ChannelRegistration reg = getClientInboundChannelRegistration();
	channel.setInterceptors(reg.getInterceptors());
	return channel;
}
 
Example 17
@Bean
public AbstractSubscribableChannel clientOutboundChannel() {
	ExecutorSubscribableChannel channel = new ExecutorSubscribableChannel(clientOutboundChannelExecutor());
	ChannelRegistration reg = getClientOutboundChannelRegistration();
	channel.setInterceptors(reg.getInterceptors());
	return channel;
}
 
Example 18
@Bean
public AbstractSubscribableChannel brokerChannel() {
	ChannelRegistration reg = getBrokerRegistry().getBrokerChannelRegistration();
	ExecutorSubscribableChannel channel = reg.hasTaskExecutor() ?
			new ExecutorSubscribableChannel(brokerChannelExecutor()) : new ExecutorSubscribableChannel();
	reg.setInterceptors(new ImmutableMessageChannelInterceptor());
	channel.setInterceptors(reg.getInterceptors());
	return channel;
}
 
Example 19
@Before
public void setUp() throws Exception {
	logger.debug("Setting up before '" + this.testName.getMethodName() + "'");
	this.port = SocketUtils.findAvailableTcpPort(61613);
	this.responseChannel = new ExecutorSubscribableChannel();
	this.responseHandler = new TestMessageHandler();
	this.responseChannel.subscribe(this.responseHandler);
	this.eventPublisher = new TestEventPublisher();
	startActiveMqBroker();
	createAndStartRelay();
}
 
Example 20
@Test
public void sendAndReceive() {

	SubscribableChannel channel = new ExecutorSubscribableChannel(this.executor);
	channel.subscribe(new MessageHandler() {
		@Override
		public void handleMessage(Message<?> message) throws MessagingException {
			MessageChannel replyChannel = (MessageChannel) message.getHeaders().getReplyChannel();
			replyChannel.send(new GenericMessage<String>("response"));
		}
	});

	String actual = this.template.convertSendAndReceive(channel, "request", String.class);
	assertEquals("response", actual);
}
 
Example 21
@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 22
/**
 * 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 23
@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 24
@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 25
@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 26
@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 27
@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);
}
 
Example 28
@Override
@Bean
public AbstractSubscribableChannel clientInboundChannel() {
	// synchronous
	return new ExecutorSubscribableChannel(null);
}
 
Example 29
@Override
@Bean
public AbstractSubscribableChannel brokerChannel() {
	// synchronous
	return new ExecutorSubscribableChannel(null);
}
 
Example 30
@Override
@Bean
public AbstractSubscribableChannel clientInboundChannel() {
	return new ExecutorSubscribableChannel();  // synchronous
}