Example usage for org.springframework.messaging Message getPayload

List of usage examples for org.springframework.messaging Message getPayload

Introduction

In this page you can find the example usage for org.springframework.messaging Message getPayload.

Prototype

T getPayload();

Source Link

Document

Return the message payload.

Usage

From source file:org.springframework.cloud.stream.module.loggregator.source.LoggregatorSourceTest.java

@Test
public void testLogReceipt() throws Exception {

    log.info(String.format("testing application %s against CF API endpoint %s with CF user %s",
            this.loggregatorProperties.getApplicationName(), this.loggregatorProperties.getCloudFoundryApi(),
            this.loggregatorProperties.getCloudFoundryUser()));

    String traceMessage = "logged-message-" + System.currentTimeMillis();
    ResponseEntity<String> entity = this.restTemplate.getForEntity(this.urlForApplication() + "/{uri}",
            String.class, Collections.singletonMap("uri", traceMessage));
    assertEquals(entity.getStatusCode(), HttpStatus.OK);
    assertEquals(entity.getBody(), traceMessage);

    BlockingQueue<Message<?>> messageBlockingQueue = this.messageCollector.forChannel(this.channels.output());

    Message<?> message;
    int count = 0, max = 20;

    while ((message = messageBlockingQueue.poll(1, TimeUnit.MINUTES)) != null && (count++ < max)) { // this could run for for 20 minutes
        log.info(String.format("received the following log from Loggregator: %s", message.getPayload()));
        String payload = String.class.cast(message.getPayload());
        assertNotNull(payload, "the message can't be null!");
        if (payload.contains(traceMessage)) {
            log.info("---------------------------------------------");
            log.info(String.format("loggregator tracer message: %s", traceMessage));
            log.info(String.format("delivered: %s", payload));
            log.info("---------------------------------------------");
            return;
        }//from w  w  w  .j ava  2s.  co m
    }
    fail("tracer message was never delivered");
}

From source file:org.springframework.cloud.stream.module.websocket.sink.WebsocketSink.java

private void addMessageToTraceRepository(Message<?> message) {
    Map<String, Object> trace = new LinkedHashMap<>();
    trace.put("type", "text");
    trace.put("direction", "out");
    trace.put("id", message.getHeaders().getId());
    trace.put("payload", message.getPayload().toString());
    websocketTraceRepository.add(trace);
}

From source file:org.springframework.integration.aggregator.AggregatorTests.java

@Test
public void testAggPerf() throws InterruptedException, ExecutionException, TimeoutException {
    AggregatingMessageHandler handler = new AggregatingMessageHandler(
            new DefaultAggregatingMessageGroupProcessor());
    handler.setCorrelationStrategy(message -> "foo");
    handler.setReleaseStrategy(new MessageCountReleaseStrategy(60000));
    handler.setExpireGroupsUponCompletion(true);
    handler.setSendPartialResultOnExpiry(true);
    DirectChannel outputChannel = new DirectChannel();
    handler.setOutputChannel(outputChannel);

    final CompletableFuture<Collection<?>> resultFuture = new CompletableFuture<>();
    outputChannel.subscribe(message -> {
        Collection<?> payload = (Collection<?>) message.getPayload();
        logger.warn("Received " + payload.size());
        resultFuture.complete(payload);//from  w  w w. j  av  a  2s. co m
    });

    SimpleMessageStore store = new SimpleMessageStore();

    SimpleMessageGroupFactory messageGroupFactory = new SimpleMessageGroupFactory(
            SimpleMessageGroupFactory.GroupType.BLOCKING_QUEUE);

    store.setMessageGroupFactory(messageGroupFactory);

    handler.setMessageStore(store);

    Message<?> message = new GenericMessage<String>("foo");
    StopWatch stopwatch = new StopWatch();
    stopwatch.start();
    for (int i = 0; i < 120000; i++) {
        if (i % 10000 == 0) {
            stopwatch.stop();
            logger.warn("Sent " + i + " in " + stopwatch.getTotalTimeSeconds() + " (10k in "
                    + stopwatch.getLastTaskTimeMillis() + "ms)");
            stopwatch.start();
        }
        handler.handleMessage(message);
    }
    stopwatch.stop();
    logger.warn("Sent " + 120000 + " in " + stopwatch.getTotalTimeSeconds() + " (10k in "
            + stopwatch.getLastTaskTimeMillis() + "ms)");

    Collection<?> result = resultFuture.get(10, TimeUnit.SECONDS);
    assertNotNull(result);
    assertEquals(60000, result.size());
}

From source file:org.springframework.integration.aggregator.AggregatorTests.java

@Test
public void testCustomAggPerf() throws InterruptedException, ExecutionException, TimeoutException {
    class CustomHandler extends AbstractMessageHandler {

        // custom aggregator, only handles a single correlation

        private final ReentrantLock lock = new ReentrantLock();

        private final Collection<Message<?>> messages = new ArrayList<Message<?>>(60000);

        private final MessageChannel outputChannel;

        private CustomHandler(MessageChannel outputChannel) {
            this.outputChannel = outputChannel;
        }/*from w w  w .j  av  a2  s.  c om*/

        @Override
        public void handleMessageInternal(Message<?> requestMessage) {
            lock.lock();
            try {
                this.messages.add(requestMessage);
                if (this.messages.size() == 60000) {
                    List<Object> payloads = new ArrayList<Object>(this.messages.size());
                    for (Message<?> message : this.messages) {
                        payloads.add(message.getPayload());
                    }
                    this.messages.clear();
                    outputChannel.send(getMessageBuilderFactory().withPayload(payloads)
                            .copyHeaders(requestMessage.getHeaders()).build());
                }
            } finally {
                lock.unlock();
            }
        }

    }

    DirectChannel outputChannel = new DirectChannel();
    CustomHandler handler = new CustomHandler(outputChannel);

    final CompletableFuture<Collection<?>> resultFuture = new CompletableFuture<>();
    outputChannel.subscribe(message -> {
        Collection<?> payload = (Collection<?>) message.getPayload();
        logger.warn("Received " + payload.size());
        resultFuture.complete(payload);
    });
    Message<?> message = new GenericMessage<String>("foo");
    StopWatch stopwatch = new StopWatch();
    stopwatch.start();
    for (int i = 0; i < 120000; i++) {
        if (i % 10000 == 0) {
            stopwatch.stop();
            logger.warn("Sent " + i + " in " + stopwatch.getTotalTimeSeconds() + " (10k in "
                    + stopwatch.getLastTaskTimeMillis() + "ms)");
            stopwatch.start();
        }
        handler.handleMessage(message);
    }
    stopwatch.stop();
    logger.warn("Sent " + 120000 + " in " + stopwatch.getTotalTimeSeconds() + " (10k in "
            + stopwatch.getLastTaskTimeMillis() + "ms)");

    Collection<?> result = resultFuture.get(10, TimeUnit.SECONDS);
    assertNotNull(result);
    assertEquals(60000, result.size());
}

From source file:org.springframework.integration.aggregator.AggregatorTests.java

@Test
public void testCompleteGroupWithinTimeout() throws InterruptedException {
    QueueChannel replyChannel = new QueueChannel();
    Message<?> message1 = createMessage(3, "ABC", 3, 1, replyChannel, null);
    Message<?> message2 = createMessage(5, "ABC", 3, 2, replyChannel, null);
    Message<?> message3 = createMessage(7, "ABC", 3, 3, replyChannel, null);

    this.aggregator.handleMessage(message1);
    this.aggregator.handleMessage(message2);
    this.aggregator.handleMessage(message3);

    Message<?> reply = replyChannel.receive(10000);
    assertNotNull(reply);//from www. j  a  v a2  s  .c o m
    assertEquals(reply.getPayload(), 105);
}

From source file:org.springframework.integration.aggregator.AggregatorTests.java

@Test
public void testShouldSendPartialResultOnTimeoutTrue() throws InterruptedException {
    this.aggregator.setSendPartialResultOnExpiry(true);
    QueueChannel replyChannel = new QueueChannel();
    Message<?> message1 = createMessage(3, "ABC", 3, 1, replyChannel, null);
    Message<?> message2 = createMessage(5, "ABC", 3, 2, replyChannel, null);
    this.aggregator.handleMessage(message1);
    this.aggregator.handleMessage(message2);
    this.store.expireMessageGroups(-10000);
    Message<?> reply = replyChannel.receive(1000);
    assertNotNull("A reply message should have been received", reply);
    assertEquals(15, reply.getPayload());
    assertEquals(1, expiryEvents.size());
    assertSame(this.aggregator, expiryEvents.get(0).getSource());
    assertEquals("ABC", this.expiryEvents.get(0).getGroupId());
    assertEquals(2, this.expiryEvents.get(0).getMessageCount());
    assertEquals(false, this.expiryEvents.get(0).isDiscarded());
    Message<?> message3 = createMessage(5, "ABC", 3, 3, replyChannel, null);
    this.aggregator.handleMessage(message3);
    assertEquals(1, this.store.getMessageGroup("ABC").size());
}

From source file:org.springframework.integration.aggregator.AggregatorTests.java

@Test
public void testGroupRemainsAfterTimeout() throws InterruptedException {
    this.aggregator.setSendPartialResultOnExpiry(true);
    this.aggregator.setExpireGroupsUponTimeout(false);
    QueueChannel replyChannel = new QueueChannel();
    QueueChannel discardChannel = new QueueChannel();
    this.aggregator.setDiscardChannel(discardChannel);
    Message<?> message1 = createMessage(3, "ABC", 3, 1, replyChannel, null);
    Message<?> message2 = createMessage(5, "ABC", 3, 2, replyChannel, null);
    this.aggregator.handleMessage(message1);
    this.aggregator.handleMessage(message2);
    this.store.expireMessageGroups(-10000);
    Message<?> reply = replyChannel.receive(1000);
    assertNotNull("A reply message should have been received", reply);
    assertEquals(15, reply.getPayload());
    assertEquals(1, expiryEvents.size());
    assertSame(this.aggregator, expiryEvents.get(0).getSource());
    assertEquals("ABC", this.expiryEvents.get(0).getGroupId());
    assertEquals(2, this.expiryEvents.get(0).getMessageCount());
    assertEquals(false, this.expiryEvents.get(0).isDiscarded());
    assertEquals(0, this.store.getMessageGroup("ABC").size());
    Message<?> message3 = createMessage(5, "ABC", 3, 3, replyChannel, null);
    this.aggregator.handleMessage(message3);
    assertEquals(0, this.store.getMessageGroup("ABC").size());
    Message<?> discardedMessage = discardChannel.receive(1000);
    assertNotNull("A message should have been discarded", discardedMessage);
    assertSame(message3, discardedMessage);
}

From source file:org.springframework.integration.aggregator.AggregatorTests.java

@Test
public void testMultipleGroupsSimultaneously() throws InterruptedException {
    QueueChannel replyChannel1 = new QueueChannel();
    QueueChannel replyChannel2 = new QueueChannel();
    Message<?> message1 = createMessage(3, "ABC", 3, 1, replyChannel1, null);
    Message<?> message2 = createMessage(5, "ABC", 3, 2, replyChannel1, null);
    Message<?> message3 = createMessage(7, "ABC", 3, 3, replyChannel1, null);
    Message<?> message4 = createMessage(11, "XYZ", 3, 1, replyChannel2, null);
    Message<?> message5 = createMessage(13, "XYZ", 3, 2, replyChannel2, null);
    Message<?> message6 = createMessage(17, "XYZ", 3, 3, replyChannel2, null);
    aggregator.handleMessage(message1);//from ww w.j  a v a  2s .c  om
    aggregator.handleMessage(message5);
    aggregator.handleMessage(message3);
    aggregator.handleMessage(message6);
    aggregator.handleMessage(message4);
    aggregator.handleMessage(message2);
    @SuppressWarnings("unchecked")
    Message<Integer> reply1 = (Message<Integer>) replyChannel1.receive(1000);
    assertNotNull(reply1);
    assertThat(reply1.getPayload(), is(105));
    @SuppressWarnings("unchecked")
    Message<Integer> reply2 = (Message<Integer>) replyChannel2.receive(1000);
    assertNotNull(reply2);
    assertThat(reply2.getPayload(), is(2431));
}

From source file:org.springframework.integration.aggregator.AggregatorTests.java

@Test
public void testAdditionalMessageAfterCompletion() throws InterruptedException {
    QueueChannel replyChannel = new QueueChannel();
    Message<?> message1 = createMessage(3, "ABC", 3, 1, replyChannel, null);
    Message<?> message2 = createMessage(5, "ABC", 3, 2, replyChannel, null);
    Message<?> message3 = createMessage(7, "ABC", 3, 3, replyChannel, null);
    Message<?> message4 = createMessage(7, "ABC", 3, 3, replyChannel, null);

    this.aggregator.handleMessage(message1);
    this.aggregator.handleMessage(message2);
    this.aggregator.handleMessage(message3);
    this.aggregator.handleMessage(message4);

    Message<?> reply = replyChannel.receive(10000);
    assertNotNull("A message should be aggregated", reply);
    assertThat(((Integer) reply.getPayload()), is(105));
}

From source file:org.springframework.integration.aggregator.AggregatorTests.java

@Test
public void shouldRejectDuplicatedSequenceNumbers() throws InterruptedException {
    QueueChannel replyChannel = new QueueChannel();
    Message<?> message1 = createMessage(3, "ABC", 3, 1, replyChannel, null);
    Message<?> message2 = createMessage(5, "ABC", 3, 2, replyChannel, null);
    Message<?> message3 = createMessage(7, "ABC", 3, 3, replyChannel, null);
    Message<?> message4 = createMessage(7, "ABC", 3, 3, replyChannel, null);
    this.aggregator.setReleaseStrategy(new SequenceSizeReleaseStrategy());

    this.aggregator.handleMessage(message1);
    this.aggregator.handleMessage(message3);
    // duplicated sequence number, either message3 or message4 should be rejected
    this.aggregator.handleMessage(message4);
    this.aggregator.handleMessage(message2);

    Message<?> reply = replyChannel.receive(10000);
    assertNotNull("A message should be aggregated", reply);
    assertThat(((Integer) reply.getPayload()), is(105));
}