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());
}
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());
}
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));
}
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));
}
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));
}
Aggregations