use of org.springframework.messaging.MessageHeaders 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.MessageHeaders in project spring-framework by spring-projects.
the class MessageHeaderAccessorTests method existingHeadersModification.
@Test
public void existingHeadersModification() throws InterruptedException {
Map<String, Object> map = new HashMap<>();
map.put("foo", "bar");
map.put("bar", "baz");
GenericMessage<String> message = new GenericMessage<>("payload", map);
Thread.sleep(50);
MessageHeaderAccessor accessor = new MessageHeaderAccessor(message);
accessor.setHeader("foo", "BAR");
MessageHeaders actual = accessor.getMessageHeaders();
assertEquals(3, actual.size());
assertNotEquals(message.getHeaders().getId(), actual.getId());
assertEquals("BAR", actual.get("foo"));
assertEquals("baz", actual.get("bar"));
}
use of org.springframework.messaging.MessageHeaders in project spring-framework by spring-projects.
the class MessageHeaderAccessorTests method leaveMutableDefaultBehavior.
@Test
public void leaveMutableDefaultBehavior() {
MessageHeaderAccessor accessor = new MessageHeaderAccessor();
accessor.setHeader("foo", "bar");
MessageHeaders headers = accessor.getMessageHeaders();
Message<?> message = MessageBuilder.createMessage("payload", headers);
this.thrown.expect(IllegalStateException.class);
this.thrown.expectMessage("Already immutable");
accessor.setLeaveMutable(true);
this.thrown.expect(IllegalStateException.class);
this.thrown.expectMessage("Already immutable");
accessor.setHeader("foo", "baz");
assertEquals("bar", headers.get("foo"));
assertSame(accessor, MessageHeaderAccessor.getAccessor(message, MessageHeaderAccessor.class));
}
use of org.springframework.messaging.MessageHeaders in project spring-framework by spring-projects.
the class MessageHeaderAccessorTests method copyHeadersIfAbsent.
@Test
public void copyHeadersIfAbsent() {
Map<String, Object> map1 = new HashMap<>();
map1.put("foo", "bar");
GenericMessage<String> message = new GenericMessage<>("payload", map1);
MessageHeaderAccessor accessor = new MessageHeaderAccessor(message);
Map<String, Object> map2 = new HashMap<>();
map2.put("foo", "BAR");
map2.put("bar", "baz");
accessor.copyHeadersIfAbsent(map2);
MessageHeaders actual = accessor.getMessageHeaders();
assertEquals(3, actual.size());
assertEquals("bar", actual.get("foo"));
assertEquals("baz", actual.get("bar"));
}
use of org.springframework.messaging.MessageHeaders in project spring-framework by spring-projects.
the class StompHeaderAccessorTests method messageIdAndTimestampDefaultBehavior.
@Test
public void messageIdAndTimestampDefaultBehavior() {
StompHeaderAccessor headerAccessor = StompHeaderAccessor.create(StompCommand.SEND);
MessageHeaders headers = headerAccessor.getMessageHeaders();
assertNull(headers.getId());
assertNull(headers.getTimestamp());
}
Aggregations