Search in sources :

Example 1 with MessageHeaderAccessor

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);
    }
}
Also used : MessageHeaderAccessor(org.springframework.messaging.support.MessageHeaderAccessor) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException)

Example 2 with MessageHeaderAccessor

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"));
}
Also used : NativeMessageHeaderAccessor(org.springframework.messaging.support.NativeMessageHeaderAccessor) MessageHeaderAccessor(org.springframework.messaging.support.MessageHeaderAccessor) Test(org.junit.Test)

Example 3 with MessageHeaderAccessor

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());
}
Also used : Message(org.springframework.messaging.Message) NativeMessageHeaderAccessor(org.springframework.messaging.support.NativeMessageHeaderAccessor) MessageHeaderAccessor(org.springframework.messaging.support.MessageHeaderAccessor) StompHeaderAccessor(org.springframework.messaging.simp.stomp.StompHeaderAccessor) Test(org.junit.Test)

Example 4 with MessageHeaderAccessor

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());
}
Also used : Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) MessageHeaderAccessor(org.springframework.messaging.support.MessageHeaderAccessor) MessageHeaders(org.springframework.messaging.MessageHeaders) Test(org.junit.Test)

Example 5 with MessageHeaderAccessor

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;
    }
}
Also used : SimpMessageType(org.springframework.messaging.simp.SimpMessageType) SimpMessageHeaderAccessor(org.springframework.messaging.simp.SimpMessageHeaderAccessor) MessageHeaderAccessor(org.springframework.messaging.support.MessageHeaderAccessor) StompHeaderAccessor(org.springframework.messaging.simp.stomp.StompHeaderAccessor)

Aggregations

MessageHeaderAccessor (org.springframework.messaging.support.MessageHeaderAccessor)12 Test (org.junit.Test)5 MessageHeaders (org.springframework.messaging.MessageHeaders)4 SimpMessageHeaderAccessor (org.springframework.messaging.simp.SimpMessageHeaderAccessor)3 Message (org.springframework.messaging.Message)2 MessageDeliveryException (org.springframework.messaging.MessageDeliveryException)2 StompHeaderAccessor (org.springframework.messaging.simp.stomp.StompHeaderAccessor)2 NativeMessageHeaderAccessor (org.springframework.messaging.support.NativeMessageHeaderAccessor)2 MimeType (org.springframework.util.MimeType)2 Method (java.lang.reflect.Method)1 StringMessageConverter (org.springframework.messaging.converter.StringMessageConverter)1 SimpMessageType (org.springframework.messaging.simp.SimpMessageType)1 GenericMessage (org.springframework.messaging.support.GenericMessage)1