Search in sources :

Example 6 with MessageHeaders

use of org.springframework.messaging.MessageHeaders in project spring-framework by spring-projects.

the class SimpMessagingTemplateTests method convertAndSendWithMutableSimpMessageHeaders.

@Test
public void convertAndSendWithMutableSimpMessageHeaders() {
    SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create();
    accessor.setHeader("key", "value");
    accessor.setNativeHeader("fooNative", "barNative");
    accessor.setLeaveMutable(true);
    MessageHeaders headers = accessor.getMessageHeaders();
    this.messagingTemplate.convertAndSend("/foo", "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) MessageHeaders(org.springframework.messaging.MessageHeaders) Test(org.junit.Test)

Example 7 with MessageHeaders

use of org.springframework.messaging.MessageHeaders in project spring-framework by spring-projects.

the class SimpMessagingTemplateTests method doSendWithMutableHeaders.

@Test
public void doSendWithMutableHeaders() {
    SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create();
    accessor.setHeader("key", "value");
    accessor.setNativeHeader("fooNative", "barNative");
    accessor.setLeaveMutable(true);
    MessageHeaders headers = accessor.getMessageHeaders();
    Message<?> message = MessageBuilder.createMessage("payload", headers);
    this.messagingTemplate.doSend("/topic/foo", message);
    List<Message<byte[]>> messages = this.messageChannel.getMessages();
    Message<byte[]> sentMessage = messages.get(0);
    assertSame(message, sentMessage);
    assertFalse(accessor.isMutable());
}
Also used : Message(org.springframework.messaging.Message) MessageHeaders(org.springframework.messaging.MessageHeaders) Test(org.junit.Test)

Example 8 with MessageHeaders

use of org.springframework.messaging.MessageHeaders in project spring-framework by spring-projects.

the class SubscriptionMethodReturnValueHandlerTests method testHeadersPassedToMessagingTemplate.

@Test
@SuppressWarnings({ "unchecked", "rawtypes" })
public void testHeadersPassedToMessagingTemplate() throws Exception {
    String sessionId = "sess1";
    String subscriptionId = "subs1";
    String destination = "/dest";
    Message<?> inputMessage = createInputMessage(sessionId, subscriptionId, destination, null);
    MessageSendingOperations messagingTemplate = Mockito.mock(MessageSendingOperations.class);
    SubscriptionMethodReturnValueHandler handler = new SubscriptionMethodReturnValueHandler(messagingTemplate);
    handler.handleReturnValue(PAYLOAD, this.subscribeEventReturnType, inputMessage);
    ArgumentCaptor<MessageHeaders> captor = ArgumentCaptor.forClass(MessageHeaders.class);
    verify(messagingTemplate).convertAndSend(eq("/dest"), eq(PAYLOAD), captor.capture());
    SimpMessageHeaderAccessor headerAccessor = MessageHeaderAccessor.getAccessor(captor.getValue(), SimpMessageHeaderAccessor.class);
    assertNotNull(headerAccessor);
    assertTrue(headerAccessor.isMutable());
    assertEquals(sessionId, headerAccessor.getSessionId());
    assertEquals(subscriptionId, headerAccessor.getSubscriptionId());
    assertEquals(this.subscribeEventReturnType, headerAccessor.getHeader(SimpMessagingTemplate.CONVERSION_HINT_HEADER));
}
Also used : MessageHeaders(org.springframework.messaging.MessageHeaders) MessageSendingOperations(org.springframework.messaging.core.MessageSendingOperations) SimpMessageHeaderAccessor(org.springframework.messaging.simp.SimpMessageHeaderAccessor) Test(org.junit.Test)

Example 9 with MessageHeaders

use of org.springframework.messaging.MessageHeaders in project spring-framework by spring-projects.

the class SimpleBrokerMessageHandlerTests method writeInactivity.

@Test
public void writeInactivity() throws Exception {
    this.messageHandler.setHeartbeatValue(new long[] { 1, 0 });
    this.messageHandler.setTaskScheduler(this.taskScheduler);
    this.messageHandler.start();
    ArgumentCaptor<Runnable> taskCaptor = ArgumentCaptor.forClass(Runnable.class);
    verify(this.taskScheduler).scheduleWithFixedDelay(taskCaptor.capture(), eq(1L));
    Runnable heartbeatTask = taskCaptor.getValue();
    assertNotNull(heartbeatTask);
    String id = "sess1";
    TestPrincipal user = new TestPrincipal("joe");
    Message<String> connectMessage = createConnectMessage(id, user, new long[] { 0, 1 });
    this.messageHandler.handleMessage(connectMessage);
    Thread.sleep(10);
    heartbeatTask.run();
    verify(this.clientOutboundChannel, times(2)).send(this.messageCaptor.capture());
    List<Message<?>> messages = this.messageCaptor.getAllValues();
    assertEquals(2, messages.size());
    MessageHeaders headers = messages.get(0).getHeaders();
    assertEquals(SimpMessageType.CONNECT_ACK, headers.get(SimpMessageHeaderAccessor.MESSAGE_TYPE_HEADER));
    headers = messages.get(1).getHeaders();
    assertEquals(SimpMessageType.HEARTBEAT, headers.get(SimpMessageHeaderAccessor.MESSAGE_TYPE_HEADER));
    assertEquals(id, headers.get(SimpMessageHeaderAccessor.SESSION_ID_HEADER));
    assertEquals(user, headers.get(SimpMessageHeaderAccessor.USER_HEADER));
}
Also used : TestPrincipal(org.springframework.messaging.simp.TestPrincipal) Message(org.springframework.messaging.Message) MessageHeaders(org.springframework.messaging.MessageHeaders) Test(org.junit.Test)

Example 10 with MessageHeaders

use of org.springframework.messaging.MessageHeaders in project spring-framework by spring-projects.

the class SimpleBrokerMessageHandlerTests method readInactivity.

@Test
public void readInactivity() throws Exception {
    this.messageHandler.setHeartbeatValue(new long[] { 0, 1 });
    this.messageHandler.setTaskScheduler(this.taskScheduler);
    this.messageHandler.start();
    ArgumentCaptor<Runnable> taskCaptor = ArgumentCaptor.forClass(Runnable.class);
    verify(this.taskScheduler).scheduleWithFixedDelay(taskCaptor.capture(), eq(1L));
    Runnable heartbeatTask = taskCaptor.getValue();
    assertNotNull(heartbeatTask);
    String id = "sess1";
    TestPrincipal user = new TestPrincipal("joe");
    Message<String> connectMessage = createConnectMessage(id, user, new long[] { 1, 0 });
    this.messageHandler.handleMessage(connectMessage);
    Thread.sleep(10);
    heartbeatTask.run();
    verify(this.clientOutboundChannel, atLeast(2)).send(this.messageCaptor.capture());
    List<Message<?>> messages = this.messageCaptor.getAllValues();
    assertEquals(2, messages.size());
    MessageHeaders headers = messages.get(0).getHeaders();
    assertEquals(SimpMessageType.CONNECT_ACK, headers.get(SimpMessageHeaderAccessor.MESSAGE_TYPE_HEADER));
    headers = messages.get(1).getHeaders();
    assertEquals(SimpMessageType.DISCONNECT_ACK, headers.get(SimpMessageHeaderAccessor.MESSAGE_TYPE_HEADER));
    assertEquals(id, headers.get(SimpMessageHeaderAccessor.SESSION_ID_HEADER));
    assertEquals(user, headers.get(SimpMessageHeaderAccessor.USER_HEADER));
}
Also used : TestPrincipal(org.springframework.messaging.simp.TestPrincipal) Message(org.springframework.messaging.Message) MessageHeaders(org.springframework.messaging.MessageHeaders) Test(org.junit.Test)

Aggregations

MessageHeaders (org.springframework.messaging.MessageHeaders)54 Test (org.junit.Test)39 HashMap (java.util.HashMap)15 Message (org.springframework.messaging.Message)9 SimpMessageType (org.springframework.messaging.simp.SimpMessageType)6 SimpMessageHeaderAccessor (org.springframework.messaging.simp.SimpMessageHeaderAccessor)5 MessageHeaderAccessor (org.springframework.messaging.support.MessageHeaderAccessor)4 MimeType (org.springframework.util.MimeType)3 Principal (java.security.Principal)2 HashSet (java.util.HashSet)2 MessageConverter (org.springframework.messaging.converter.MessageConverter)2 StringMessageConverter (org.springframework.messaging.converter.StringMessageConverter)2 TestPrincipal (org.springframework.messaging.simp.TestPrincipal)2 StompHeaderAccessor (org.springframework.messaging.simp.stomp.StompHeaderAccessor)2 GenericMessage (org.springframework.messaging.support.GenericMessage)2 Method (java.lang.reflect.Method)1 Set (java.util.Set)1 UUID (java.util.UUID)1 Destination (javax.jms.Destination)1 JMSException (javax.jms.JMSException)1