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());
}
use of org.springframework.messaging.MessageHeaders in project spring-framework by spring-projects.
the class SimpleJmsHeaderMapper method toHeaders.
@Override
public MessageHeaders toHeaders(javax.jms.Message jmsMessage) {
Map<String, Object> headers = new HashMap<>();
try {
try {
String correlationId = jmsMessage.getJMSCorrelationID();
if (correlationId != null) {
headers.put(JmsHeaders.CORRELATION_ID, correlationId);
}
} catch (Exception ex) {
logger.info("Failed to read JMSCorrelationID property - skipping", ex);
}
try {
Destination destination = jmsMessage.getJMSDestination();
if (destination != null) {
headers.put(JmsHeaders.DESTINATION, destination);
}
} catch (Exception ex) {
logger.info("Failed to read JMSDestination property - skipping", ex);
}
try {
int deliveryMode = jmsMessage.getJMSDeliveryMode();
headers.put(JmsHeaders.DELIVERY_MODE, deliveryMode);
} catch (Exception ex) {
logger.info("Failed to read JMSDeliveryMode property - skipping", ex);
}
try {
long expiration = jmsMessage.getJMSExpiration();
headers.put(JmsHeaders.EXPIRATION, expiration);
} catch (Exception ex) {
logger.info("Failed to read JMSExpiration property - skipping", ex);
}
try {
String messageId = jmsMessage.getJMSMessageID();
if (messageId != null) {
headers.put(JmsHeaders.MESSAGE_ID, messageId);
}
} catch (Exception ex) {
logger.info("Failed to read JMSMessageID property - skipping", ex);
}
try {
headers.put(JmsHeaders.PRIORITY, jmsMessage.getJMSPriority());
} catch (Exception ex) {
logger.info("Failed to read JMSPriority property - skipping", ex);
}
try {
Destination replyTo = jmsMessage.getJMSReplyTo();
if (replyTo != null) {
headers.put(JmsHeaders.REPLY_TO, replyTo);
}
} catch (Exception ex) {
logger.info("Failed to read JMSReplyTo property - skipping", ex);
}
try {
headers.put(JmsHeaders.REDELIVERED, jmsMessage.getJMSRedelivered());
} catch (Exception ex) {
logger.info("Failed to read JMSRedelivered property - skipping", ex);
}
try {
String type = jmsMessage.getJMSType();
if (type != null) {
headers.put(JmsHeaders.TYPE, type);
}
} catch (Exception ex) {
logger.info("Failed to read JMSType property - skipping", ex);
}
try {
headers.put(JmsHeaders.TIMESTAMP, jmsMessage.getJMSTimestamp());
} catch (Exception ex) {
logger.info("Failed to read JMSTimestamp property - skipping", ex);
}
Enumeration<?> jmsPropertyNames = jmsMessage.getPropertyNames();
if (jmsPropertyNames != null) {
while (jmsPropertyNames.hasMoreElements()) {
String propertyName = jmsPropertyNames.nextElement().toString();
try {
String headerName = this.toHeaderName(propertyName);
headers.put(headerName, jmsMessage.getObjectProperty(propertyName));
} catch (Exception ex) {
if (logger.isWarnEnabled()) {
logger.warn("Error occurred while mapping JMS property '" + propertyName + "' to Message header", ex);
}
}
}
}
} catch (JMSException ex) {
if (logger.isWarnEnabled()) {
logger.warn("Error occurred while mapping from JMS properties to MessageHeaders", ex);
}
}
return new MessageHeaders(headers);
}
Aggregations