Java Code Examples for org.springframework.messaging.PollableChannel#receive()

The following examples show how to use org.springframework.messaging.PollableChannel#receive() . 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: QueueMessageChannelTest.java    From spring-cloud-aws with Apache License 2.0 6 votes vote down vote up
@Test
void receiveMessage_withoutTimeout_returnsTextMessage() throws Exception {
	// Arrange
	AmazonSQSAsync amazonSqs = mock(AmazonSQSAsync.class);
	when(amazonSqs.receiveMessage(new ReceiveMessageRequest("http://testQueue")
			.withWaitTimeSeconds(0).withMaxNumberOfMessages(1)
			.withAttributeNames(QueueMessageChannel.ATTRIBUTE_NAMES)
			.withMessageAttributeNames("All")))
					.thenReturn(new ReceiveMessageResult().withMessages(Collections
							.singleton(new com.amazonaws.services.sqs.model.Message()
									.withBody("content"))));

	PollableChannel messageChannel = new QueueMessageChannel(amazonSqs,
			"http://testQueue");

	// Act
	Message<?> receivedMessage = messageChannel.receive();

	// Assert
	assertThat(receivedMessage).isNotNull();
	assertThat(receivedMessage.getPayload()).isEqualTo("content");
}
 
Example 2
Source File: QueueMessageChannelTest.java    From spring-cloud-aws with Apache License 2.0 6 votes vote down vote up
@Test
void receiveMessage_withSpecifiedTimeout_returnsTextMessage() throws Exception {
	// Arrange
	AmazonSQSAsync amazonSqs = mock(AmazonSQSAsync.class);
	when(amazonSqs.receiveMessage(new ReceiveMessageRequest("http://testQueue")
			.withWaitTimeSeconds(2).withMaxNumberOfMessages(1)
			.withAttributeNames(QueueMessageChannel.ATTRIBUTE_NAMES)
			.withMessageAttributeNames("All")))
					.thenReturn(new ReceiveMessageResult().withMessages(Collections
							.singleton(new com.amazonaws.services.sqs.model.Message()
									.withBody("content"))));

	PollableChannel messageChannel = new QueueMessageChannel(amazonSqs,
			"http://testQueue");

	// Act
	Message<?> receivedMessage = messageChannel.receive(2);

	// Assert
	assertThat(receivedMessage).isNotNull();
	assertThat(receivedMessage.getPayload()).isEqualTo("content");
}
 
Example 3
Source File: QueueMessageChannelTest.java    From spring-cloud-aws with Apache License 2.0 6 votes vote down vote up
@Test
void receiveMessage_withSpecifiedTimeout_returnsNull() throws Exception {
	// Arrange
	AmazonSQSAsync amazonSqs = mock(AmazonSQSAsync.class);
	when(amazonSqs.receiveMessage(new ReceiveMessageRequest("http://testQueue")
			.withWaitTimeSeconds(2).withMaxNumberOfMessages(1)
			.withAttributeNames(QueueMessageChannel.ATTRIBUTE_NAMES)
			.withMessageAttributeNames("All"))).thenReturn(
					new ReceiveMessageResult().withMessages(Collections.emptyList()));

	PollableChannel messageChannel = new QueueMessageChannel(amazonSqs,
			"http://testQueue");

	// Act
	Message<?> receivedMessage = messageChannel.receive(2);

	// Assert
	assertThat(receivedMessage).isNull();
}
 
Example 4
Source File: QueueMessageChannelTest.java    From spring-cloud-aws with Apache License 2.0 6 votes vote down vote up
@Test
void receiveMessage_withoutDefaultTimeout_returnsNull() throws Exception {
	// Arrange
	AmazonSQSAsync amazonSqs = mock(AmazonSQSAsync.class);
	when(amazonSqs.receiveMessage(new ReceiveMessageRequest("http://testQueue")
			.withWaitTimeSeconds(0).withMaxNumberOfMessages(1)
			.withAttributeNames(QueueMessageChannel.ATTRIBUTE_NAMES)
			.withMessageAttributeNames("All"))).thenReturn(
					new ReceiveMessageResult().withMessages(Collections.emptyList()));

	PollableChannel messageChannel = new QueueMessageChannel(amazonSqs,
			"http://testQueue");

	// Act
	Message<?> receivedMessage = messageChannel.receive(0);

	// Assert
	assertThat(receivedMessage).isNull();
}
 
Example 5
Source File: StreamPollableChannelMessageReceiver.java    From spring-cloud-contract with Apache License 2.0 5 votes vote down vote up
@Override
public Message<?> receive(String destination, long timeout, TimeUnit timeUnit) {
	try {
		PollableChannel messageChannel = this.context.getBean(this.destinationResolver
				.resolvedDestination(destination, DefaultChannels.INPUT),
				PollableChannel.class);
		return messageChannel.receive(timeUnit.toMillis(timeout));
	}
	catch (Exception e) {
		log.error("Exception occurred while trying to read a message from "
				+ " a channel with name [" + destination + "]", e);
		throw new IllegalStateException(e);
	}
}
 
Example 6
Source File: SpringIntegrationStubMessages.java    From spring-cloud-contract with Apache License 2.0 5 votes vote down vote up
@Override
public Message<?> receive(String destination, long timeout, TimeUnit timeUnit) {
	try {
		PollableChannel messageChannel = this.context.getBean(destination,
				PollableChannel.class);
		return messageChannel.receive(timeUnit.toMillis(timeout));
	}
	catch (Exception e) {
		log.error("Exception occurred while trying to read a message from "
				+ " a channel with name [" + destination + "]", e);
		throw new IllegalStateException(e);
	}
}
 
Example 7
Source File: QueueMessageChannelTest.java    From spring-cloud-aws with Apache License 2.0 5 votes vote down vote up
@Test
void receiveMessage_withMimeTypeMessageAttribute_shouldCopyToHeaders()
		throws Exception {
	// Arrange
	AmazonSQSAsync amazonSqs = mock(AmazonSQSAsync.class);
	MimeType mimeType = new MimeType("test", "plain", Charset.forName("UTF-8"));
	when(amazonSqs.receiveMessage(new ReceiveMessageRequest("http://testQueue")
			.withWaitTimeSeconds(0).withMaxNumberOfMessages(1)
			.withAttributeNames(QueueMessageChannel.ATTRIBUTE_NAMES)
			.withMessageAttributeNames("All"))).thenReturn(new ReceiveMessageResult()
					.withMessages(new com.amazonaws.services.sqs.model.Message()
							.withBody("Hello")
							.withMessageAttributes(Collections.singletonMap(
									MessageHeaders.CONTENT_TYPE,
									new MessageAttributeValue()
											.withDataType(
													MessageAttributeDataTypes.STRING)
											.withStringValue(mimeType.toString())))));

	PollableChannel messageChannel = new QueueMessageChannel(amazonSqs,
			"http://testQueue");

	// Act
	Message<?> receivedMessage = messageChannel.receive();

	// Assert
	assertThat(receivedMessage.getHeaders().get(MessageHeaders.CONTENT_TYPE))
			.isEqualTo(mimeType);
}
 
Example 8
Source File: QueueMessageChannelTest.java    From spring-cloud-aws with Apache License 2.0 5 votes vote down vote up
@Test
void receiveMessage_withStringMessageHeader_shouldBeReceivedAsQueueMessageAttribute()
		throws Exception {
	// Arrange
	AmazonSQSAsync amazonSqs = mock(AmazonSQSAsync.class);
	String headerValue = "Header value";
	String headerName = "MyHeader";
	when(amazonSqs.receiveMessage(new ReceiveMessageRequest("http://testQueue")
			.withWaitTimeSeconds(0).withMaxNumberOfMessages(1)
			.withAttributeNames(QueueMessageChannel.ATTRIBUTE_NAMES)
			.withMessageAttributeNames("All")))
					.thenReturn(new ReceiveMessageResult().withMessages(
							new com.amazonaws.services.sqs.model.Message()
									.withBody("Hello")
									.withMessageAttributes(Collections.singletonMap(
											headerName,
											new MessageAttributeValue().withDataType(
													MessageAttributeDataTypes.STRING)
													.withStringValue(headerValue)))));

	PollableChannel messageChannel = new QueueMessageChannel(amazonSqs,
			"http://testQueue");

	// Act
	Message<?> receivedMessage = messageChannel.receive();

	// Assert
	assertThat(receivedMessage.getHeaders().get(headerName)).isEqualTo(headerValue);
}
 
Example 9
Source File: QueueMessageChannelTest.java    From spring-cloud-aws with Apache License 2.0 5 votes vote down vote up
@Test
void receiveMessage_withBinaryMessageHeader_shouldBeReceivedAsByteBufferMessageAttribute()
		throws Exception {
	// Arrange
	AmazonSQSAsync amazonSqs = mock(AmazonSQSAsync.class);
	ByteBuffer headerValue = ByteBuffer.wrap("My binary data!".getBytes());
	String headerName = "MyHeader";
	when(amazonSqs.receiveMessage(new ReceiveMessageRequest("http://testQueue")
			.withWaitTimeSeconds(0).withMaxNumberOfMessages(1)
			.withAttributeNames(QueueMessageChannel.ATTRIBUTE_NAMES)
			.withMessageAttributeNames("All")))
					.thenReturn(new ReceiveMessageResult().withMessages(
							new com.amazonaws.services.sqs.model.Message()
									.withBody("Hello")
									.withMessageAttributes(Collections.singletonMap(
											headerName,
											new MessageAttributeValue().withDataType(
													MessageAttributeDataTypes.BINARY)
													.withBinaryValue(headerValue)))));

	PollableChannel messageChannel = new QueueMessageChannel(amazonSqs,
			"http://testQueue");

	// Act
	Message<?> receivedMessage = messageChannel.receive();

	// Assert
	assertThat(receivedMessage.getHeaders().get(headerName)).isEqualTo(headerValue);
}
 
Example 10
Source File: QueueMessageChannelTest.java    From spring-cloud-aws with Apache License 2.0 5 votes vote down vote up
@Test
void receiveMessage_withIdOfTypeString_IdShouldBeConvertedToUuid() throws Exception {
	// Arrange
	AmazonSQSAsync amazonSqs = mock(AmazonSQSAsync.class);
	UUID uuid = UUID.randomUUID();
	when(amazonSqs.receiveMessage(new ReceiveMessageRequest("http://testQueue")
			.withWaitTimeSeconds(0).withMaxNumberOfMessages(1)
			.withAttributeNames(QueueMessageChannel.ATTRIBUTE_NAMES)
			.withMessageAttributeNames("All"))).thenReturn(new ReceiveMessageResult()
					.withMessages(new com.amazonaws.services.sqs.model.Message()
							.withBody("Hello")
							.withMessageAttributes(Collections.singletonMap(
									MessageHeaders.ID,
									new MessageAttributeValue()
											.withDataType(
													MessageAttributeDataTypes.STRING)
											.withStringValue(uuid.toString())))));

	PollableChannel messageChannel = new QueueMessageChannel(amazonSqs,
			"http://testQueue");

	// Act
	Message<?> receivedMessage = messageChannel.receive();

	// Assert
	Object idMessageHeader = receivedMessage.getHeaders().get(MessageHeaders.ID);
	assertThat(UUID.class.isInstance(idMessageHeader)).isTrue();
	assertThat(idMessageHeader).isEqualTo(uuid);
}
 
Example 11
Source File: QueueMessageChannelTest.java    From spring-cloud-aws with Apache License 2.0 4 votes vote down vote up
@Test
void receiveMessage_withNumericMessageHeaders_shouldBeReceivedAsQueueMessageAttributes()
		throws Exception {
	// Arrange
	AmazonSQSAsync amazonSqs = mock(AmazonSQSAsync.class);

	HashMap<String, MessageAttributeValue> messageAttributes = new HashMap<>();
	double doubleValue = 1234.56;
	messageAttributes.put("double",
			new MessageAttributeValue()
					.withDataType(
							MessageAttributeDataTypes.NUMBER + ".java.lang.Double")
					.withStringValue(String.valueOf(doubleValue)));
	long longValue = 1234L;
	messageAttributes.put("long",
			new MessageAttributeValue()
					.withDataType(
							MessageAttributeDataTypes.NUMBER + ".java.lang.Long")
					.withStringValue(String.valueOf(longValue)));
	int integerValue = 1234;
	messageAttributes.put("integer",
			new MessageAttributeValue()
					.withDataType(
							MessageAttributeDataTypes.NUMBER + ".java.lang.Integer")
					.withStringValue(String.valueOf(integerValue)));
	byte byteValue = 2;
	messageAttributes.put("byte",
			new MessageAttributeValue()
					.withDataType(
							MessageAttributeDataTypes.NUMBER + ".java.lang.Byte")
					.withStringValue(String.valueOf(byteValue)));
	short shortValue = 12;
	messageAttributes.put("short",
			new MessageAttributeValue()
					.withDataType(
							MessageAttributeDataTypes.NUMBER + ".java.lang.Short")
					.withStringValue(String.valueOf(shortValue)));
	float floatValue = 1234.56f;
	messageAttributes.put("float",
			new MessageAttributeValue()
					.withDataType(
							MessageAttributeDataTypes.NUMBER + ".java.lang.Float")
					.withStringValue(String.valueOf(floatValue)));
	BigInteger bigIntegerValue = new BigInteger("616416546156");
	messageAttributes.put("bigInteger", new MessageAttributeValue()
			.withDataType(MessageAttributeDataTypes.NUMBER + ".java.math.BigInteger")
			.withStringValue(String.valueOf(bigIntegerValue)));
	BigDecimal bigDecimalValue = new BigDecimal("7834938");
	messageAttributes.put("bigDecimal", new MessageAttributeValue()
			.withDataType(MessageAttributeDataTypes.NUMBER + ".java.math.BigDecimal")
			.withStringValue(String.valueOf(bigDecimalValue)));

	when(amazonSqs.receiveMessage(new ReceiveMessageRequest("http://testQueue")
			.withWaitTimeSeconds(0).withMaxNumberOfMessages(1)
			.withAttributeNames(QueueMessageChannel.ATTRIBUTE_NAMES)
			.withMessageAttributeNames("All")))
					.thenReturn(new ReceiveMessageResult().withMessages(
							new com.amazonaws.services.sqs.model.Message()
									.withBody("Hello")
									.withMessageAttributes(messageAttributes)));

	PollableChannel messageChannel = new QueueMessageChannel(amazonSqs,
			"http://testQueue");

	// Act
	Message<?> receivedMessage = messageChannel.receive();

	// Assert
	assertThat(receivedMessage.getHeaders().get("double")).isEqualTo(doubleValue);
	assertThat(receivedMessage.getHeaders().get("long")).isEqualTo(longValue);
	assertThat(receivedMessage.getHeaders().get("integer")).isEqualTo(integerValue);
	assertThat(receivedMessage.getHeaders().get("byte")).isEqualTo(byteValue);
	assertThat(receivedMessage.getHeaders().get("short")).isEqualTo(shortValue);
	assertThat(receivedMessage.getHeaders().get("float")).isEqualTo(floatValue);
	assertThat(receivedMessage.getHeaders().get("bigInteger"))
			.isEqualTo(bigIntegerValue);
	assertThat(receivedMessage.getHeaders().get("bigDecimal"))
			.isEqualTo(bigDecimalValue);
}
 
Example 12
Source File: HelloWorldRestController.java    From spring-cloud-sleuth with Apache License 2.0 3 votes vote down vote up
@RequestMapping(path = "getHelloWorldMessage", method = RequestMethod.GET,
		produces = MediaType.TEXT_PLAIN_VALUE)
public ResponseEntity<String> getHelloWorld() throws Exception {

	LOG.info("Inside getHelloWorldMessage");

	String[] requestMessage = new String[3];
	requestMessage[0] = "Hellow World Message 1";
	requestMessage[1] = "Hellow World Message 2";
	requestMessage[2] = "Hellow World Message 3";

	PollableChannel outputChannel = (PollableChannel) this.applicationContext
			.getBean("messagingOutputChannel");

	MessagingGateway messagingGateway = (MessagingGateway) this.applicationContext
			.getBean("messagingGateway");

	messagingGateway.processMessage(requestMessage);

	GenericMessage reply = (GenericMessage) outputChannel.receive();

	List<String> body = (List<String>) reply.getPayload();

	LOG.info(" Response Message " + body);

	return new ResponseEntity<String>(body.toString(), HttpStatus.OK);
}