Search in sources :

Example 21 with Message

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

the class SimpAnnotationMethodMessageHandlerTests method listenableFutureSuccess.

@Test
@SuppressWarnings("unchecked")
public void listenableFutureSuccess() {
    Message emptyMessage = (Message) MessageBuilder.withPayload(new byte[0]).build();
    given(this.channel.send(any(Message.class))).willReturn(true);
    given(this.converter.toMessage(any(), any(MessageHeaders.class))).willReturn(emptyMessage);
    ListenableFutureController controller = new ListenableFutureController();
    this.messageHandler.registerHandler(controller);
    this.messageHandler.setDestinationPrefixes(Arrays.asList("/app1", "/app2/"));
    Message<?> message = createMessage("/app1/listenable-future/success");
    this.messageHandler.handleMessage(message);
    assertNotNull(controller.future);
    controller.future.run();
    verify(this.converter).toMessage(this.payloadCaptor.capture(), any(MessageHeaders.class));
    assertEquals("foo", this.payloadCaptor.getValue());
}
Also used : Message(org.springframework.messaging.Message) MessageHeaders(org.springframework.messaging.MessageHeaders) Test(org.junit.Test)

Example 22 with Message

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

the class SimpAnnotationMethodMessageHandlerTests method completableFutureSuccess.

@Test
@SuppressWarnings("unchecked")
public void completableFutureSuccess() {
    Message emptyMessage = (Message) MessageBuilder.withPayload(new byte[0]).build();
    given(this.channel.send(any(Message.class))).willReturn(true);
    given(this.converter.toMessage(any(), any(MessageHeaders.class))).willReturn(emptyMessage);
    CompletableFutureController controller = new CompletableFutureController();
    this.messageHandler.registerHandler(controller);
    this.messageHandler.setDestinationPrefixes(Arrays.asList("/app1", "/app2/"));
    Message<?> message = createMessage("/app1/completable-future");
    this.messageHandler.handleMessage(message);
    assertNotNull(controller.future);
    controller.future.complete("foo");
    verify(this.converter).toMessage(this.payloadCaptor.capture(), any(MessageHeaders.class));
    assertEquals("foo", this.payloadCaptor.getValue());
}
Also used : Message(org.springframework.messaging.Message) MessageHeaders(org.springframework.messaging.MessageHeaders) Test(org.junit.Test)

Example 23 with Message

use of org.springframework.messaging.Message 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 24 with Message

use of org.springframework.messaging.Message 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)

Example 25 with Message

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

the class SimpleBrokerMessageHandlerTests method readWriteIntervalCalculation.

@Test
public void readWriteIntervalCalculation() throws Exception {
    this.messageHandler.setHeartbeatValue(new long[] { 1, 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[] { 10000, 10000 });
    this.messageHandler.handleMessage(connectMessage);
    Thread.sleep(10);
    heartbeatTask.run();
    verify(this.clientOutboundChannel, times(1)).send(this.messageCaptor.capture());
    List<Message<?>> messages = this.messageCaptor.getAllValues();
    assertEquals(1, messages.size());
    assertEquals(SimpMessageType.CONNECT_ACK, messages.get(0).getHeaders().get(SimpMessageHeaderAccessor.MESSAGE_TYPE_HEADER));
}
Also used : TestPrincipal(org.springframework.messaging.simp.TestPrincipal) Message(org.springframework.messaging.Message) Test(org.junit.Test)

Aggregations

Message (org.springframework.messaging.Message)51 Test (org.junit.Test)41 SimpMessageHeaderAccessor (org.springframework.messaging.simp.SimpMessageHeaderAccessor)16 MessageHeaders (org.springframework.messaging.MessageHeaders)14 StompHeaderAccessor (org.springframework.messaging.simp.stomp.StompHeaderAccessor)9 BinaryMessage (org.springframework.web.socket.BinaryMessage)9 TextMessage (org.springframework.web.socket.TextMessage)9 WebSocketMessage (org.springframework.web.socket.WebSocketMessage)6 ByteBuffer (java.nio.ByteBuffer)5 MessageChannel (org.springframework.messaging.MessageChannel)4 MessageHeaderAccessor (org.springframework.messaging.support.MessageHeaderAccessor)4 Method (java.lang.reflect.Method)3 MethodParameter (org.springframework.core.MethodParameter)3 TestPrincipal (org.springframework.messaging.simp.TestPrincipal)3 Principal (java.security.Principal)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 TextMessage (javax.jms.TextMessage)2 SynthesizingMethodParameter (org.springframework.core.annotation.SynthesizingMethodParameter)2 StubTextMessage (org.springframework.jms.StubTextMessage)2 MessagingException (org.springframework.messaging.MessagingException)2