use of org.springframework.messaging.support.MessageHeaderAccessor in project spring-framework by spring-projects.
the class GenericMessagingTemplate method doSend.
@Override
protected final void doSend(MessageChannel channel, Message<?> message) {
Assert.notNull(channel, "MessageChannel is required");
MessageHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message, MessageHeaderAccessor.class);
if (accessor != null && accessor.isMutable()) {
accessor.setImmutable();
}
long timeout = this.sendTimeout;
boolean sent = (timeout >= 0 ? channel.send(message, timeout) : channel.send(message));
if (!sent) {
throw new MessageDeliveryException(message, "Failed to send message to channel '" + channel + "' within timeout: " + timeout);
}
}
use of org.springframework.messaging.support.MessageHeaderAccessor in project spring-framework by spring-projects.
the class HeadersMethodArgumentResolverTests method resolveArgumentMessageHeaderAccessor.
@Test
public void resolveArgumentMessageHeaderAccessor() throws Exception {
Object resolved = this.resolver.resolveArgument(this.paramMessageHeaderAccessor, this.message);
assertTrue(resolved instanceof MessageHeaderAccessor);
MessageHeaderAccessor headers = (MessageHeaderAccessor) resolved;
assertEquals("bar", headers.getHeader("foo"));
}
use of org.springframework.messaging.support.MessageHeaderAccessor in project spring-framework by spring-projects.
the class SimpMessagingTemplateTests method doSendWithStompHeaders.
@Test
public void doSendWithStompHeaders() {
StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.SUBSCRIBE);
accessor.setDestination("/user/queue/foo");
Message<?> message = MessageBuilder.createMessage(new byte[0], accessor.getMessageHeaders());
this.messagingTemplate.doSend("/queue/foo-user123", message);
List<Message<byte[]>> messages = this.messageChannel.getMessages();
Message<byte[]> sentMessage = messages.get(0);
MessageHeaderAccessor sentAccessor = MessageHeaderAccessor.getAccessor(sentMessage, MessageHeaderAccessor.class);
assertEquals(StompHeaderAccessor.class, sentAccessor.getClass());
assertEquals("/queue/foo-user123", ((StompHeaderAccessor) sentAccessor).getDestination());
}
use of org.springframework.messaging.support.MessageHeaderAccessor in project spring-framework by spring-projects.
the class GenericMessagingTemplateTests method convertAndSendWithSimpMessageHeaders.
@Test
public void convertAndSendWithSimpMessageHeaders() {
MessageHeaderAccessor accessor = new MessageHeaderAccessor();
accessor.setHeader("key", "value");
accessor.setLeaveMutable(true);
MessageHeaders headers = accessor.getMessageHeaders();
this.template.convertAndSend("channel", "data", headers);
List<Message<byte[]>> messages = this.messageChannel.getMessages();
Message<byte[]> message = messages.get(0);
assertSame(headers, message.getHeaders());
assertFalse(accessor.isMutable());
}
use of org.springframework.messaging.support.MessageHeaderAccessor in project spring-framework by spring-projects.
the class StompSubProtocolHandler method getStompHeaderAccessor.
private StompHeaderAccessor getStompHeaderAccessor(Message<?> message) {
MessageHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message, MessageHeaderAccessor.class);
if (accessor instanceof StompHeaderAccessor) {
return (StompHeaderAccessor) accessor;
} else {
StompHeaderAccessor stompAccessor = StompHeaderAccessor.wrap(message);
SimpMessageType messageType = SimpMessageHeaderAccessor.getMessageType(message.getHeaders());
if (SimpMessageType.CONNECT_ACK.equals(messageType)) {
stompAccessor = convertConnectAcktoStompConnected(stompAccessor);
} else if (SimpMessageType.DISCONNECT_ACK.equals(messageType)) {
String receipt = getDisconnectReceipt(stompAccessor);
if (receipt != null) {
stompAccessor = StompHeaderAccessor.create(StompCommand.RECEIPT);
stompAccessor.setReceiptId(receipt);
} else {
stompAccessor = StompHeaderAccessor.create(StompCommand.ERROR);
stompAccessor.setMessage("Session closed.");
}
} else if (SimpMessageType.HEARTBEAT.equals(messageType)) {
stompAccessor = StompHeaderAccessor.createForHeartbeat();
} else if (stompAccessor.getCommand() == null || StompCommand.SEND.equals(stompAccessor.getCommand())) {
stompAccessor.updateStompCommandAsServerMessage();
}
return stompAccessor;
}
}
Aggregations