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.contract.stubrunner.messaging.stream.StubRunnerStreamMessageSelector.java

@Override
public boolean accept(Message<?> message) {
    if (!headersMatch(message)) {
        return false;
    }//w w  w.  j ava  2  s  .  c om
    Object inputMessage = message.getPayload();
    JsonPaths jsonPaths = JsonToJsonPathsConverter.transformToJsonPathWithStubsSideValuesAndNoArraySizeCheck(
            this.groovyDsl.getInput().getMessageBody());
    DocumentContext parsedJson;
    try {
        parsedJson = JsonPath.parse(this.objectMapper.writeValueAsString(inputMessage));
        for (MethodBufferingJsonVerifiable it : jsonPaths) {
            if (!matchesJsonPath(parsedJson, it)) {
                return false;
            }
        }
    } catch (JsonProcessingException e) {
        throw new IllegalStateException("Cannot parse JSON", e);
    }
    return true;
}

From source file:org.springframework.cloud.function.web.source.SupplierExporter.java

private Mono<ClientResponse> post(URI uri, String destination, Object value) {
    Object body = value;//from  ww  w  . j  ava2 s. co m
    if (value instanceof Message) {
        Message<?> message = (Message<?>) value;
        body = message.getPayload();
    }
    Mono<ClientResponse> result = this.client.post().uri(uri)
            .headers(headers -> headers(headers, destination, value)).syncBody(body).exchange();
    if (this.debug) {
        result = result.log();
    }
    return result;
}

From source file:org.springframework.cloud.gcp.autoconfigure.pubsub.it.PubSubChannelAdaptersIntegrationTests.java

@Test
public void sendAndReceiveMessageAsString() {
    this.contextRunner.run((context) -> {
        try {/*from   w w  w .  j a v a2  s  .c  om*/
            Map<String, Object> headers = new HashMap<>();
            // Only String values for now..
            headers.put("storm", "lift your skinny fists");
            headers.put("static", "lift your skinny fists");
            headers.put("sleep", "lift your skinny fists");

            Message originalMessage = MessageBuilder.createMessage("I am a message.".getBytes(),
                    new MessageHeaders(headers));
            context.getBean("inputChannel", MessageChannel.class).send(originalMessage);

            Message<?> message = context.getBean("outputChannel", PollableChannel.class).receive(5000);
            assertThat(message).isNotNull();
            assertThat(message.getPayload()).isInstanceOf(byte[].class);
            String payload = new String((byte[]) message.getPayload());
            assertThat(payload).isEqualTo("I am a message.");

            assertThat(message.getHeaders().size()).isEqualTo(6);
            assertThat(message.getHeaders().get("storm")).isEqualTo("lift your skinny fists");
            assertThat(message.getHeaders().get("static")).isEqualTo("lift your skinny fists");
            assertThat(message.getHeaders().get("sleep")).isEqualTo("lift your skinny fists");
            assertThat(message.getHeaders().get(GcpPubSubHeaders.ORIGINAL_MESSAGE)).isNotNull();
        } finally {
            PubSubAdmin pubSubAdmin = context.getBean(PubSubAdmin.class);
            pubSubAdmin.deleteSubscription((String) context.getBean("subscriptionName"));
            pubSubAdmin.deleteTopic((String) context.getBean("topicName"));
        }
    });
}

From source file:org.springframework.cloud.gcp.autoconfigure.pubsub.it.PubSubChannelAdaptersIntegrationTests.java

@Test
public void sendAndReceiveMessage() {
    this.contextRunner.run((context) -> {
        try {/*  w  w  w .j  a  v a  2 s  .c o  m*/
            context.getBean("inputChannel", MessageChannel.class)
                    .send(MessageBuilder.withPayload("I am a message.".getBytes()).build());

            Message<?> message = context.getBean("outputChannel", PollableChannel.class).receive(5000);
            assertThat(message).isNotNull();
            assertThat(message.getPayload()).isInstanceOf(byte[].class);
            String stringPayload = new String((byte[]) message.getPayload());
            assertThat(stringPayload).isEqualTo("I am a message.");
        } finally {
            PubSubAdmin pubSubAdmin = context.getBean(PubSubAdmin.class);
            pubSubAdmin.deleteSubscription((String) context.getBean("subscriptionName"));
            pubSubAdmin.deleteTopic((String) context.getBean("topicName"));
        }
    });
}

From source file:org.springframework.cloud.gcp.storage.integration.inbound.GcsInboundFileSynchronizerTests.java

@Test
public void testCopyFiles() throws Exception {
    File localDirectory = new File("test");
    GcsInboundFileSynchronizer synchronizer = new GcsInboundFileSynchronizer(this.gcs);
    synchronizer.setRemoteDirectory("test-bucket");
    synchronizer.setBeanFactory(mock(BeanFactory.class));

    GcsInboundFileSynchronizingMessageSource adapter = new GcsInboundFileSynchronizingMessageSource(
            synchronizer);//from  w  w w  .  j  a v  a 2 s  .c o m
    adapter.setAutoCreateLocalDirectory(true);
    adapter.setLocalDirectory(localDirectory);
    adapter.setBeanFactory(mock(BeanFactory.class));

    adapter.setLocalFilter(new AcceptOnceFileListFilter<>());

    adapter.afterPropertiesSet();

    Message<File> message = adapter.receive();
    assertThat(message.getPayload().getName()).isEqualTo("legend of heroes");
    assertThat(Files.readAllBytes(message.getPayload().toPath())).isEqualTo("estelle".getBytes());

    message = adapter.receive();
    assertThat(message.getPayload().getName()).isEqualTo("trails in the sky");
    assertThat(Files.readAllBytes(message.getPayload().toPath())).isEqualTo("joshua".getBytes());

    message = adapter.receive();
    assertThat(message).isNull();
}

From source file:org.springframework.cloud.sleuth.instrument.messaging.TracingChannelInterceptor.java

private Message<?> outputMessage(Message<?> originalMessage, Message<?> retrievedMessage,
        MessageHeaderAccessor additionalHeaders) {
    MessageHeaderAccessor headers = MessageHeaderAccessor.getMutableAccessor(originalMessage);
    if (originalMessage.getPayload() instanceof MessagingException) {
        headers.copyHeaders(MessageHeaderPropagation.propagationHeaders(additionalHeaders.getMessageHeaders(),
                this.tracing.propagation().keys()));
        return new ErrorMessage((MessagingException) originalMessage.getPayload(),
                isWebSockets(headers) ? headers.getMessageHeaders()
                        : new MessageHeaders(headers.getMessageHeaders()));
    }/*from w w w . j  a  v  a 2 s  . c om*/
    headers.copyHeaders(additionalHeaders.getMessageHeaders());
    return new GenericMessage<>(retrievedMessage.getPayload(),
            isWebSockets(headers) ? headers.getMessageHeaders()
                    : new MessageHeaders(headers.getMessageHeaders()));
}

From source file:org.springframework.cloud.sleuth.instrument.messaging.TracingChannelInterceptor.java

/**
 * This starts a consumer span as a child of the incoming message or the current trace
 * context, placing it in scope until the receive completes.
 *///from  ww w .j ava 2  s .c o  m
@Override
public Message<?> postReceive(Message<?> message, MessageChannel channel) {
    if (emptyMessage(message)) {
        return message;
    }
    MessageHeaderAccessor headers = mutableHeaderAccessor(message);
    TraceContextOrSamplingFlags extracted = this.extractor.extract(headers);
    Span span = this.threadLocalSpan.next(extracted);
    MessageHeaderPropagation.removeAnyTraceHeaders(headers, this.tracing.propagation().keys());
    this.injector.inject(span.context(), headers);
    if (!span.isNoop()) {
        span.kind(Span.Kind.CONSUMER).name("receive").start();
        span.remoteServiceName(REMOTE_SERVICE_NAME);
        addTags(message, span, channel);
    }
    if (log.isDebugEnabled()) {
        log.debug("Created a new span in post receive " + span);
    }
    headers.setImmutable();
    return new GenericMessage<>(message.getPayload(), headers.getMessageHeaders());
}

From source file:org.springframework.cloud.sleuth.instrument.messaging.TracingChannelInterceptor.java

/**
 * This starts a consumer span as a child of the incoming message or the current trace
 * context. It then creates a span for the handler, placing it in scope.
 *//*from   ww w  .j  ava2s .c o  m*/
@Override
public Message<?> beforeHandle(Message<?> message, MessageChannel channel, MessageHandler handler) {
    if (emptyMessage(message)) {
        return message;
    }
    MessageHeaderAccessor headers = mutableHeaderAccessor(message);
    TraceContextOrSamplingFlags extracted = this.extractor.extract(headers);
    // Start and finish a consumer span as we will immediately process it.
    Span consumerSpan = this.tracer.nextSpan(extracted);
    if (!consumerSpan.isNoop()) {
        consumerSpan.kind(Span.Kind.CONSUMER).start();
        consumerSpan.remoteServiceName(REMOTE_SERVICE_NAME);
        addTags(message, consumerSpan, channel);
        consumerSpan.finish();
    }
    // create and scope a span for the message processor
    this.threadLocalSpan.next(TraceContextOrSamplingFlags.create(consumerSpan.context())).name("handle")
            .start();
    // remove any trace headers, but don't re-inject as we are synchronously
    // processing the
    // message and can rely on scoping to access this span later.
    MessageHeaderPropagation.removeAnyTraceHeaders(headers, this.tracing.propagation().keys());
    if (log.isDebugEnabled()) {
        log.debug("Created a new span in before handle" + consumerSpan);
    }
    if (message instanceof ErrorMessage) {
        return new ErrorMessage((Throwable) message.getPayload(), headers.getMessageHeaders());
    }
    headers.setImmutable();
    return new GenericMessage<>(message.getPayload(), headers.getMessageHeaders());
}

From source file:org.springframework.cloud.sleuth.instrument.messaging.TracingChannelInterceptor.java

private Message<?> getMessage(Message<?> message) {
    Object payload = message.getPayload();
    if (payload instanceof MessagingException) {
        MessagingException e = (MessagingException) payload;
        return e.getFailedMessage();
    }/*from  www.j  a v a2s . com*/
    return message;
}

From source file:org.springframework.cloud.stream.annotation.rxjava.SubjectMessageHandler.java

@Override
protected void handleMessageInternal(Message<?> message) throws Exception {
    this.subject.onNext(message.getPayload());
}