Java Code Examples for org.springframework.messaging.converter.MessageConverter#toMessage()

The following examples show how to use org.springframework.messaging.converter.MessageConverter#toMessage() . 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: EventRouter.java    From haven-platform with Apache License 2.0 6 votes vote down vote up
private void sendHistoryToNewSubscriber(AbstractSubProtocolEvent ev) {
    Message<byte[]> msg = ev.getMessage();
    StompHeaderAccessor ha = StompHeaderAccessor.wrap(msg);
    String pattern = ha.getDestination();
    if(!pattern.startsWith(PREFIX)) {
        // we must send only to appropriate paths
        return;
    }
    MessageConverter messageConverter = this.simpMessagingTemplate.getMessageConverter();

    for(BusData data: buses.values()) {
        String dest = getDestination(data.getId());
        if(!this.pathMatcher.match(pattern, dest)) {
            continue;
        }
        for(Object obj: data.getEvents()) {
            StompHeaderAccessor mha = Stomp.createHeaders(ha.getSessionId(), ha.getSubscriptionId());
            mha.setDestination(dest);
            Message<?> message = messageConverter.toMessage(obj, mha.getMessageHeaders());
            clientChannel.send(message);
        }
    }
}
 
Example 2
Source File: MessageUtils.java    From rqueue with Apache License 2.0 5 votes vote down vote up
public static RqueueMessage buildMessage(
    MessageConverter messageConverter,
    String queueName,
    Object message,
    Integer retryCount,
    Long delayInMilliSecs) {
  Message<?> msg = messageConverter.toMessage(message, null);
  if (msg == null) {
    throw new MessageConversionException("Message could not be build (null)");
  }
  return new RqueueMessage(queueName, (String) msg.getPayload(), retryCount, delayInMilliSecs);
}
 
Example 3
Source File: Stomp.java    From haven-platform with Apache License 2.0 5 votes vote down vote up
/**
 * Send message to queue of current session
 * @param subscriptionId
 * @param dest
 * @param msg
 */
public void sendToSubscription(String subscriptionId, String dest, Object msg) {
    Assert.notNull(subscriptionId, "subscriptionId is null");
    StompHeaderAccessor sha = createHeaders(sessionId, subscriptionId);
    MessageConverter messageConverter = this.template.getMessageConverter();
    sha.setDestination("/queue/" + dest);
    Message<?> message = messageConverter.toMessage(msg, sha.getMessageHeaders());
    clientChannel.send(message);
}
 
Example 4
Source File: ObjectMessageConverterTest.java    From spring-cloud-aws with Apache License 2.0 5 votes vote down vote up
@Test
void testToMessageAndFromMessage() throws Exception {
	String content = "stringwithspecialcharsöäü€a8";
	MySerializableClass sourceMessage = new MySerializableClass(content);
	MessageConverter messageConverter = new ObjectMessageConverter();
	Message<?> message = messageConverter.toMessage(sourceMessage,
			getMessageHeaders("UTF-8"));
	assertThat(Base64.isBase64(message.getPayload().toString().getBytes("UTF-8")))
			.isTrue();
	MySerializableClass result = (MySerializableClass) messageConverter
			.fromMessage(message, MySerializableClass.class);
	assertThat(result.getContent()).isEqualTo(content);
}
 
Example 5
Source File: ObjectMessageConverterTest.java    From spring-cloud-aws with Apache License 2.0 5 votes vote down vote up
@Test
void testToMessageAndFromMessageWithCustomEncoding() throws Exception {
	String content = "stringwithspecialcharsöäü€a8";
	MySerializableClass sourceMessage = new MySerializableClass(content);
	MessageConverter messageConverter = new ObjectMessageConverter("ISO-8859-1");
	Message<?> message = messageConverter.toMessage(sourceMessage,
			getMessageHeaders("ISO-8859-1"));
	assertThat(
			Base64.isBase64(message.getPayload().toString().getBytes("ISO-8859-1")))
					.isTrue();
	MySerializableClass result = (MySerializableClass) messageConverter
			.fromMessage(message, MySerializableClass.class);
	assertThat(result.getContent()).isEqualTo(content);
}