Example usage for org.springframework.messaging.support MessageBuilder withPayload

List of usage examples for org.springframework.messaging.support MessageBuilder withPayload

Introduction

In this page you can find the example usage for org.springframework.messaging.support MessageBuilder withPayload.

Prototype

public static <T> MessageBuilder<T> withPayload(T payload) 

Source Link

Document

Create a new builder for a message with the given payload.

Usage

From source file:smpp.networking.SimpleStompClient.java

public void subscribe(String destination, MessageHandler messageHandler) {

    String id = String.valueOf(this.subscriptionIndex.getAndIncrement());
    this.subscriptionHandlers.put(id, messageHandler);

    StompHeaderAccessor headers = StompHeaderAccessor.create(StompCommand.SUBSCRIBE);
    headers.setSubscriptionId(id);// ww  w  . ja v a 2 s . co  m
    headers.setDestination(destination);

    Message<byte[]> message = MessageBuilder.withPayload(new byte[0]).setHeaders(headers).build();
    byte[] bytes = encoder.encode(message);
    try {
        this.session.getRemote().sendString(new String(bytes, DEFAULT_CHARSET));
    } catch (IOException e) {
        throw new IllegalStateException(e);
    }
}

From source file:org.springframework.samples.portfolio.web.standalone.StandalonePortfolioControllerTests.java

@Test
public void executeTrade() throws Exception {

    Trade trade = new Trade();
    trade.setAction(Trade.TradeAction.Buy);
    trade.setTicker("DELL");
    trade.setShares(25);//from  w w  w.  j a  va  2s.c o  m

    byte[] payload = new ObjectMapper().writeValueAsBytes(trade);

    StompHeaderAccessor headers = StompHeaderAccessor.create(StompCommand.SEND);
    headers.setDestination("/app/trade");
    headers.setSessionId("0");
    headers.setUser(new TestPrincipal("fabrice"));
    headers.setSessionAttributes(new HashMap<String, Object>());
    Message<byte[]> message = MessageBuilder.withPayload(payload).setHeaders(headers).build();

    this.annotationMethodMessageHandler.handleMessage(message);

    assertEquals(1, this.tradeService.getTrades().size());
    Trade actual = this.tradeService.getTrades().get(0);

    assertEquals(Trade.TradeAction.Buy, actual.getAction());
    assertEquals("DELL", actual.getTicker());
    assertEquals(25, actual.getShares());
    assertEquals("fabrice", actual.getUsername());
}

From source file:org.springframework.cloud.aws.messaging.listener.QueueMessageHandlerTest.java

@Test
public void receiveAndReplyMessage_methodAnnotatedWithMessageMappingAnnotation_methodInvokedForIncomingMessageAndReplySentBackToSendToDestination()
        throws Exception {
    StaticApplicationContext applicationContext = new StaticApplicationContext();
    applicationContext.registerSingleton("incomingMessageHandler", IncomingMessageHandler.class);
    applicationContext.registerBeanDefinition("queueMessageHandler", getQueueMessageHandlerBeanDefinition());
    applicationContext.refresh();/*w w w.java2 s  . c  o m*/

    MessageHandler messageHandler = applicationContext.getBean(MessageHandler.class);
    messageHandler.handleMessage(MessageBuilder.withPayload("testContent")
            .setHeader(QueueMessageHandler.Headers.LOGICAL_RESOURCE_ID_MESSAGE_HEADER_KEY, "receiveAndReply")
            .build());

    IncomingMessageHandler messageListener = applicationContext.getBean(IncomingMessageHandler.class);
    assertEquals("testContent", messageListener.getLastReceivedMessage());
    verify(this.messageTemplate).convertAndSend(eq("sendTo"), eq("TESTCONTENT"));
}

From source file:demo.web.StateMachineController.java

@RequestMapping("/event")
@ResponseStatus(HttpStatus.OK)// w  w w .j a  v a  2  s.  c  o m
public void sendEvent(@RequestParam(value = "id") Events id,
        @RequestParam(value = "testVariable", required = false) String testVariable) {
    log.info("Got request to send event " + id + " testVariable " + testVariable);
    Message<Events> message = MessageBuilder.withPayload(id).setHeader("testVariable", testVariable).build();
    stateMachine.sendEvent(message);
}

From source file:ch.rasc.wampspring.method.PayloadArgumentResolverTest.java

@Test
public void resolveNonAnnotatedParameter() throws Exception {
    PublishMessage notEmptyMessage = new PublishMessage("pub", "ABC");
    assertEquals("ABC", this.resolver.resolveArgument(this.paramNotAnnotated, notEmptyMessage));

    Message<?> emptyStringMessage = MessageBuilder.withPayload("").build();
    this.thrown.expect(MethodArgumentNotValidException.class);
    this.resolver.resolveArgument(this.paramValidated, emptyStringMessage);
}

From source file:org.springframework.cloud.aws.messaging.listener.QueueMessageHandlerTest.java

@Test
public void receiveMessage_methodAnnotatedWithMessageMappingContainingMultipleQueueNames_methodInvokedForEachQueueName()
        throws Exception {
    StaticApplicationContext applicationContext = new StaticApplicationContext();
    applicationContext.registerSingleton("incomingMessageHandlerWithMultipleQueueNames",
            IncomingMessageHandlerWithMultipleQueueNames.class);
    applicationContext.registerSingleton("queueMessageHandler", QueueMessageHandler.class);
    applicationContext.refresh();/* w w w .  j  av  a 2s .c  o m*/

    QueueMessageHandler queueMessageHandler = applicationContext.getBean(QueueMessageHandler.class);
    IncomingMessageHandlerWithMultipleQueueNames incomingMessageHandler = applicationContext
            .getBean(IncomingMessageHandlerWithMultipleQueueNames.class);

    queueMessageHandler.handleMessage(MessageBuilder.withPayload("Hello from queue one!")
            .setHeader(QueueMessageHandler.Headers.LOGICAL_RESOURCE_ID_MESSAGE_HEADER_KEY, "queueOne").build());
    assertEquals("Hello from queue one!", incomingMessageHandler.getLastReceivedMessage());

    queueMessageHandler.handleMessage(MessageBuilder.withPayload("Hello from queue two!")
            .setHeader(QueueMessageHandler.Headers.LOGICAL_RESOURCE_ID_MESSAGE_HEADER_KEY, "queueTwo").build());
    assertEquals("Hello from queue two!", incomingMessageHandler.getLastReceivedMessage());
}

From source file:org.springframework.cloud.aws.messaging.listener.QueueMessageHandlerTest.java

@Test
public void receiveMessage_withHeaderAnnotationAsArgument_shouldReceiveRequestedHeader() throws Exception {
    // Arrange//from www.j  a v  a  2  s . c  o  m
    StaticApplicationContext applicationContext = new StaticApplicationContext();
    applicationContext.registerSingleton("messageHandlerWithHeaderAnnotation",
            MessageReceiverWithHeaderAnnotation.class);
    applicationContext.registerSingleton("queueMessageHandler", QueueMessageHandler.class);
    applicationContext.refresh();

    QueueMessageHandler queueMessageHandler = applicationContext.getBean(QueueMessageHandler.class);
    MessageReceiverWithHeaderAnnotation messageReceiver = applicationContext
            .getBean(MessageReceiverWithHeaderAnnotation.class);

    // Act
    queueMessageHandler.handleMessage(
            MessageBuilder.withPayload("Hello from a sender").setHeader("SenderId", "elsUnitTest")
                    .setHeader(QueueMessageHandler.Headers.LOGICAL_RESOURCE_ID_MESSAGE_HEADER_KEY, "testQueue")
                    .build());

    // Assert
    assertEquals("Hello from a sender", messageReceiver.getPayload());
    assertEquals("elsUnitTest", messageReceiver.getSenderId());
}

From source file:org.springframework.cloud.aws.messaging.listener.QueueMessageHandlerTest.java

@Test
public void receiveMessage_withWrongHeaderAnnotationValueAsArgument_shouldReceiveNullAsHeaderValue()
        throws Exception {
    // Arrange//from w  ww .  j av a2 s . co m
    StaticApplicationContext applicationContext = new StaticApplicationContext();
    applicationContext.registerSingleton("messageHandlerWithHeaderAnnotation",
            MessageReceiverWithHeaderAnnotation.class);
    applicationContext.registerSingleton("queueMessageHandler", QueueMessageHandler.class);
    applicationContext.refresh();

    QueueMessageHandler queueMessageHandler = applicationContext.getBean(QueueMessageHandler.class);
    MessageReceiverWithHeaderAnnotation messageReceiver = applicationContext
            .getBean(MessageReceiverWithHeaderAnnotation.class);

    // Act
    queueMessageHandler.handleMessage(MessageBuilder.withPayload("Hello from a sender")
            .setHeader(QueueMessageHandler.Headers.LOGICAL_RESOURCE_ID_MESSAGE_HEADER_KEY, "testQueue")
            .build());

    // Assert
    assertEquals("Hello from a sender", messageReceiver.getPayload());
    assertNull(messageReceiver.getSenderId());
}

From source file:org.springframework.cloud.aws.messaging.listener.QueueMessageHandlerTest.java

@Test
public void receiveMessage_withHeadersAsArgumentAnnotation_shouldReceiveAllHeaders() throws Exception {
    // Arrange//from   w  w  w .ja  v  a 2  s  .  c  om
    StaticApplicationContext applicationContext = new StaticApplicationContext();
    applicationContext.registerSingleton("messageHandlerWithHeadersAnnotation",
            MessageReceiverWithHeadersAnnotation.class);
    applicationContext.registerSingleton("queueMessageHandler", QueueMessageHandler.class);
    applicationContext.refresh();

    QueueMessageHandler queueMessageHandler = applicationContext.getBean(QueueMessageHandler.class);
    MessageReceiverWithHeadersAnnotation messageReceiver = applicationContext
            .getBean(MessageReceiverWithHeadersAnnotation.class);

    // Act
    queueMessageHandler.handleMessage(MessageBuilder.withPayload("Hello from a sender")
            .setHeader(QueueMessageHandler.Headers.LOGICAL_RESOURCE_ID_MESSAGE_HEADER_KEY, "testQueue")
            .setHeader("SenderId", "ID").build());

    // Assert
    assertNotNull(messageReceiver.getHeaders());
    assertEquals("ID", messageReceiver.getHeaders().get("SenderId"));
    assertEquals("testQueue", messageReceiver.getHeaders()
            .get(QueueMessageHandler.Headers.LOGICAL_RESOURCE_ID_MESSAGE_HEADER_KEY));
}

From source file:org.springframework.cloud.aws.messaging.listener.QueueMessageHandlerTest.java

@Test
public void receiveMessage_withCustomArgumentResolvers_shouldCallThemBeforeTheDefaultOnes() throws Exception {
    // Arrange//from   w w w  .jav a2s .co m
    StaticApplicationContext applicationContext = new StaticApplicationContext();
    applicationContext.registerSingleton("incomingMessageHandler", IncomingMessageHandler.class);

    HandlerMethodArgumentResolver handlerMethodArgumentResolver = mock(HandlerMethodArgumentResolver.class);
    when(handlerMethodArgumentResolver.supportsParameter(any(MethodParameter.class))).thenReturn(true);
    when(handlerMethodArgumentResolver.resolveArgument(any(MethodParameter.class), any(Message.class)))
            .thenReturn("Hello from a sender");
    MutablePropertyValues properties = new MutablePropertyValues(Collections
            .singletonList(new PropertyValue("customArgumentResolvers", handlerMethodArgumentResolver)));
    applicationContext.registerSingleton("queueMessageHandler", QueueMessageHandler.class, properties);
    applicationContext.refresh();

    QueueMessageHandler queueMessageHandler = applicationContext.getBean(QueueMessageHandler.class);

    // Act
    queueMessageHandler.handleMessage(MessageBuilder.withPayload("Hello from a sender")
            .setHeader(QueueMessageHandler.Headers.LOGICAL_RESOURCE_ID_MESSAGE_HEADER_KEY, "receive").build());

    // Assert
    verify(handlerMethodArgumentResolver, times(1)).resolveArgument(any(MethodParameter.class),
            any(Message.class));
}