use of org.springframework.messaging.Message in project spring-framework by spring-projects.
the class DefaultStompSession method createMessage.
@SuppressWarnings("unchecked")
private Message<byte[]> createMessage(StompHeaderAccessor accessor, Object payload) {
accessor.updateSimpMessageHeadersFromStompHeaders();
Message<byte[]> message;
if (payload == null) {
message = MessageBuilder.createMessage(EMPTY_PAYLOAD, accessor.getMessageHeaders());
} else if (payload instanceof byte[]) {
message = MessageBuilder.createMessage((byte[]) payload, accessor.getMessageHeaders());
} else {
message = (Message<byte[]>) getMessageConverter().toMessage(payload, accessor.getMessageHeaders());
accessor.updateStompHeadersFromSimpMessageHeaders();
if (message == null) {
throw new MessageConversionException("Unable to convert payload with type='" + payload.getClass().getName() + "', contentType='" + accessor.getContentType() + "', converter=[" + getMessageConverter() + "]");
}
}
return message;
}
use of org.springframework.messaging.Message in project spring-framework by spring-projects.
the class SimpAnnotationMethodMessageHandlerTests method listenableFutureFailure.
@Test
@SuppressWarnings("unchecked")
public void listenableFutureFailure() {
Message emptyMessage = (Message) MessageBuilder.withPayload(new byte[0]).build();
given(this.channel.send(any(Message.class))).willReturn(true);
given(this.converter.toMessage(any(), any(MessageHeaders.class))).willReturn(emptyMessage);
ListenableFutureController controller = new ListenableFutureController();
this.messageHandler.registerHandler(controller);
this.messageHandler.setDestinationPrefixes(Arrays.asList("/app1", "/app2/"));
Message<?> message = createMessage("/app1/listenable-future/failure");
this.messageHandler.handleMessage(message);
controller.future.run();
assertTrue(controller.exceptionCaught);
}
use of org.springframework.messaging.Message in project rocketmq-externals by apache.
the class RocketMQTemplate method convertToRocketMsg.
/**
* Convert spring message to rocketMQ message
*
* @param destination formats: `topicName:tags`
* @param message {@link org.springframework.messaging.Message}
* @return instance of {@link org.apache.rocketmq.common.message.Message}
*/
private org.apache.rocketmq.common.message.Message convertToRocketMsg(String destination, Message<?> message) {
Object payloadObj = message.getPayload();
byte[] payloads;
if (payloadObj instanceof String) {
payloads = ((String) payloadObj).getBytes(Charset.forName(charset));
} else {
try {
String jsonObj = this.objectMapper.writeValueAsString(payloadObj);
payloads = jsonObj.getBytes(Charset.forName(charset));
} catch (Exception e) {
throw new RuntimeException("convert to RocketMQ message failed.", e);
}
}
String[] tempArr = destination.split(":", 2);
String topic = tempArr[0];
String tags = "";
if (tempArr.length > 1) {
tags = tempArr[1];
}
org.apache.rocketmq.common.message.Message rocketMsg = new org.apache.rocketmq.common.message.Message(topic, tags, payloads);
MessageHeaders headers = message.getHeaders();
if (Objects.nonNull(headers) && !headers.isEmpty()) {
Object keys = headers.get(MessageConst.PROPERTY_KEYS);
if (!StringUtils.isEmpty(keys)) {
// if headers has 'KEYS', set rocketMQ message key
rocketMsg.setKeys(keys.toString());
}
// set rocketMQ message flag
Object flagObj = headers.getOrDefault("FLAG", "0");
int flag = 0;
try {
flag = Integer.parseInt(flagObj.toString());
} catch (NumberFormatException e) {
// ignore
log.info("flag must be integer, flagObj:{}", flagObj);
}
rocketMsg.setFlag(flag);
// set rocketMQ message waitStoreMsgOkObj
Object waitStoreMsgOkObj = headers.getOrDefault("WAIT_STORE_MSG_OK", "true");
boolean waitStoreMsgOK = Boolean.TRUE.equals(waitStoreMsgOkObj);
rocketMsg.setWaitStoreMsgOK(waitStoreMsgOK);
headers.entrySet().stream().filter(entry -> !Objects.equals(entry.getKey(), MessageConst.PROPERTY_KEYS) && !Objects.equals(entry.getKey(), "FLAG") && // exclude "KEYS", "FLAG", "WAIT_STORE_MSG_OK"
!Objects.equals(entry.getKey(), "WAIT_STORE_MSG_OK")).forEach(entry -> {
// add other properties with prefix "USERS_"
rocketMsg.putUserProperty("USERS_" + entry.getKey(), String.valueOf(entry.getValue()));
});
}
return rocketMsg;
}
use of org.springframework.messaging.Message in project spring-integration-samples by spring-projects.
the class JmsMockTests method setup.
@Before
public void setup() throws JMSException {
Mockito.reset(this.mockJmsTemplate);
TextMessage message = mock(TextMessage.class);
willReturn(new SimpleMessageConverter()).given(this.mockJmsTemplate).getMessageConverter();
willReturn(message).given(this.mockJmsTemplate).receiveSelected(isNull());
willAnswer((Answer<String>) invocation -> testMessageHolder.get()).given(message).getText();
}
use of org.springframework.messaging.Message in project spring-integration-samples by spring-projects.
the class AggregatorDemoTest method testGatewayDemo.
@Test
public void testGatewayDemo() throws InterruptedException {
System.setProperty("spring.profiles.active", "testCase");
final GenericXmlApplicationContext applicationContext = new GenericXmlApplicationContext(configFilesGatewayDemo);
final MessageChannel stdinToJmsOutChannel = applicationContext.getBean("stdinToJmsOutChannel", MessageChannel.class);
stdinToJmsOutChannel.send(MessageBuilder.withPayload("jms test").build());
final QueueChannel queueChannel = applicationContext.getBean("queueChannel", QueueChannel.class);
@SuppressWarnings("unchecked") Message<List<String>> reply = (Message<List<String>>) queueChannel.receive(600000);
Assert.assertNotNull(reply);
List<String> out = reply.getPayload();
Assert.assertEquals("[JMS TEST, JMS TEST]", out.toString());
applicationContext.close();
}
Aggregations