List of usage examples for org.springframework.messaging Message getHeaders
MessageHeaders getHeaders();
From source file:org.springframework.cloud.stream.app.tasklaunchrequest.DataFlowTaskLaunchRequestAutoConfiguration.java
private TaskLaunchRequestContext taskLaunchRequestContext(Message<?> message) { TaskLaunchRequestContext taskLaunchRequestContext = (TaskLaunchRequestContext) message.getHeaders() .get(TaskLaunchRequestContext.HEADER_NAME); return taskLaunchRequestContext != null ? taskLaunchRequestContext : new TaskLaunchRequestContext(); }
From source file:org.springframework.cloud.stream.app.tensorflow.processor.TensorflowProcessorConfiguration.java
@ServiceActivator(inputChannel = Processor.INPUT, outputChannel = Processor.OUTPUT) public Message<?> evaluate(Message<?> input) { Map<String, Object> processorContext = new ConcurrentHashMap<>(); Map<String, Object> inputData = tensorflowInputConverter.convert(input, processorContext); Tensor outputTensor = tensorFlowService.evaluate(inputData, properties.getOutputName(), properties.getOutputIndex()); Object outputData = tensorflowOutputConverter.convert(outputTensor, processorContext); if (properties.isSaveOutputInHeader()) { // Add the result to the message header return MessageBuilder.withPayload(input.getPayload()).copyHeadersIfAbsent(input.getHeaders()) .setHeaderIfAbsent(TF_OUTPUT_HEADER, outputData).build(); }/*from w w w .j a v a2 s. c o m*/ // Add the outputData as part of the message payload Message<?> outputMessage = MessageBuilder.withPayload(outputData).copyHeadersIfAbsent(input.getHeaders()) .build(); return outputMessage; }
From source file:org.springframework.cloud.stream.app.tensorflow.processor.TensorflowProcessorConfiguration.java
@Bean @ConditionalOnMissingBean(name = "tensorflowInputConverter") public TensorflowInputConverter tensorflowInputConverter() { return new TensorflowInputConverter() { @Override//from w w w . jav a 2 s . co m public Map<String, Object> convert(Message<?> input, Map<String, Object> processorContext) { if (input.getHeaders().containsKey(TF_INPUT_HEADER)) { return (Map<String, Object>) input.getHeaders().get(TF_INPUT_HEADER, Map.class); } else if (input.getPayload() instanceof Map) { return (Map<String, Object>) input.getPayload(); } throw new RuntimeException("Unsupported input format: " + input); } }; }
From source file:org.springframework.cloud.stream.app.websocket.sink.WebsocketSinkConfiguration.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.cloud.stream.binder.AbstractBinder.java
protected final MessageValues serializePayloadIfNecessary(Message<?> message) { Object originalPayload = message.getPayload(); Object originalContentType = message.getHeaders().get(MessageHeaders.CONTENT_TYPE); // Pass content type as String since some transport adapters will exclude // CONTENT_TYPE Header otherwise Object contentType = JavaClassMimeTypeConversion .mimeTypeFromObject(originalPayload, ObjectUtils.nullSafeToString(originalContentType)).toString(); Object payload = serializePayloadIfNecessary(originalPayload); MessageValues messageValues = new MessageValues(message); messageValues.setPayload(payload);//from w w w. j av a 2s. c om messageValues.put(MessageHeaders.CONTENT_TYPE, contentType); if (originalContentType != null && !originalContentType.toString().equals(contentType.toString())) { messageValues.put(BinderHeaders.BINDER_ORIGINAL_CONTENT_TYPE, originalContentType.toString()); } return messageValues; }
From source file:org.springframework.cloud.stream.binder.AbstractBinderTests.java
@Test public void testSendAndReceive() throws Exception { Binder binder = getBinder();/*from w w w . j a va 2 s . co m*/ BindingProperties outputBindingProperties = createProducerBindingProperties(createProducerProperties()); DirectChannel moduleOutputChannel = createBindableChannel("output", outputBindingProperties); QueueChannel moduleInputChannel = new QueueChannel(); Binding<MessageChannel> producerBinding = binder.bindProducer("foo.0", moduleOutputChannel, outputBindingProperties.getProducer()); Binding<MessageChannel> consumerBinding = binder.bindConsumer("foo.0", "test", moduleInputChannel, createConsumerProperties()); Message<?> message = MessageBuilder.withPayload("foo").setHeader(MessageHeaders.CONTENT_TYPE, "foo/bar") .build(); // Let the consumer actually bind to the producer before sending a msg binderBindUnbindLatency(); moduleOutputChannel.send(message); Message<?> inbound = receive(moduleInputChannel); assertThat(inbound).isNotNull(); assertThat(inbound.getPayload()).isEqualTo("foo"); assertThat(inbound.getHeaders().get(BinderHeaders.BINDER_ORIGINAL_CONTENT_TYPE)).isNull(); assertThat(inbound.getHeaders().get(MessageHeaders.CONTENT_TYPE)).isEqualTo("foo/bar"); producerBinding.unbind(); consumerBinding.unbind(); }
From source file:org.springframework.cloud.stream.binder.AbstractBinderTests.java
@Test public void testSendAndReceiveNoOriginalContentType() throws Exception { Binder binder = getBinder();//w w w . java2 s . c o m BindingProperties producerBindingProperties = createProducerBindingProperties(createProducerProperties()); DirectChannel moduleOutputChannel = createBindableChannel("output", producerBindingProperties); QueueChannel moduleInputChannel = new QueueChannel(); Binding<MessageChannel> producerBinding = binder.bindProducer("bar.0", moduleOutputChannel, producerBindingProperties.getProducer()); Binding<MessageChannel> consumerBinding = binder.bindConsumer("bar.0", "test", moduleInputChannel, createConsumerProperties()); binderBindUnbindLatency(); Message<?> message = MessageBuilder.withPayload("foo").build(); moduleOutputChannel.send(message); Message<?> inbound = receive(moduleInputChannel); assertThat(inbound).isNotNull(); assertThat(inbound.getPayload()).isEqualTo("foo"); assertThat(inbound.getHeaders().get(BinderHeaders.BINDER_ORIGINAL_CONTENT_TYPE)).isNull(); assertThat(inbound.getHeaders().get(MessageHeaders.CONTENT_TYPE)).isEqualTo(MimeTypeUtils.TEXT_PLAIN_VALUE); producerBinding.unbind(); consumerBinding.unbind(); }
From source file:org.springframework.cloud.stream.binder.kafka.streams.KafkaStreamsMessageConversionDelegate.java
/** * Serialize {@link KStream} records on outbound based on contentType. * * @param outboundBindTarget outbound KStream target * @return serialized KStream//from w w w .j a v a2 s .co m */ @SuppressWarnings("rawtypes") public KStream serializeOnOutbound(KStream<?, ?> outboundBindTarget) { String contentType = this.kstreamBindingInformationCatalogue.getContentType(outboundBindTarget); MessageConverter messageConverter = this.compositeMessageConverterFactory .getMessageConverterForAllRegistered(); return outboundBindTarget.mapValues((v) -> { Message<?> message = v instanceof Message<?> ? (Message<?>) v : MessageBuilder.withPayload(v).build(); Map<String, Object> headers = new HashMap<>(message.getHeaders()); if (!StringUtils.isEmpty(contentType)) { headers.put(MessageHeaders.CONTENT_TYPE, contentType); } MessageHeaders messageHeaders = new MessageHeaders(headers); return messageConverter.toMessage(message.getPayload(), messageHeaders).getPayload(); }); }
From source file:org.springframework.cloud.stream.binder.pubsub.PubSubMessageHandler.java
protected PubSubMessage convert(Message<?> message) throws Exception { String encodedHeaders = encodeHeaders(message.getHeaders()); String topic = producerProperties.isPartitioned() ? topics.get((Integer) message.getHeaders().get(BinderHeaders.PARTITION_HEADER)).name() : topics.get(0).name();/* w w w .j a va2s . c o m*/ PubSubMessage pubSubMessage = new PubSubMessage( com.google.cloud.pubsub.Message.builder(ByteArray.copyFrom((byte[]) message.getPayload())) .addAttribute(PubSubBinder.SCST_HEADERS, encodedHeaders).build(), topic); return pubSubMessage; }
From source file:org.springframework.cloud.stream.binder.rabbit.RabbitBinderTests.java
@SuppressWarnings("unchecked") @Test/*from w w w .j a v a2s. c o m*/ public void testBatchingAndCompression() throws Exception { RabbitTestBinder binder = getBinder(); ExtendedProducerProperties<RabbitProducerProperties> producerProperties = createProducerProperties(); producerProperties.getExtension().setDeliveryMode(MessageDeliveryMode.NON_PERSISTENT); producerProperties.getExtension().setBatchingEnabled(true); producerProperties.getExtension().setBatchSize(2); producerProperties.getExtension().setBatchBufferLimit(100000); producerProperties.getExtension().setBatchTimeout(30000); producerProperties.getExtension().setCompress(true); producerProperties.setRequiredGroups("default"); DirectChannel output = createBindableChannel("input", createProducerBindingProperties(producerProperties)); output.setBeanName("batchingProducer"); Binding<MessageChannel> producerBinding = binder.bindProducer("batching.0", output, producerProperties); Log logger = spy(TestUtils.getPropertyValue(binder, "binder.compressingPostProcessor.logger", Log.class)); new DirectFieldAccessor(TestUtils.getPropertyValue(binder, "binder.compressingPostProcessor")) .setPropertyValue("logger", logger); when(logger.isTraceEnabled()).thenReturn(true); assertThat(TestUtils.getPropertyValue(binder, "binder.compressingPostProcessor.level")) .isEqualTo(Deflater.BEST_SPEED); output.send(new GenericMessage<>("foo".getBytes())); output.send(new GenericMessage<>("bar".getBytes())); Object out = spyOn("batching.0.default").receive(false); assertThat(out).isInstanceOf(byte[].class); assertThat(new String((byte[]) out)).isEqualTo("\u0000\u0000\u0000\u0003foo\u0000\u0000\u0000\u0003bar"); ArgumentCaptor<Object> captor = ArgumentCaptor.forClass(Object.class); verify(logger).trace(captor.capture()); assertThat(captor.getValue().toString()).contains(("Compressed 14 to ")); QueueChannel input = new QueueChannel(); input.setBeanName("batchingConsumer"); Binding<MessageChannel> consumerBinding = binder.bindConsumer("batching.0", "test", input, createConsumerProperties()); output.send(new GenericMessage<>("foo".getBytes())); output.send(new GenericMessage<>("bar".getBytes())); Message<byte[]> in = (Message<byte[]>) input.receive(10000); assertThat(in).isNotNull(); assertThat(new String(in.getPayload())).isEqualTo("foo"); in = (Message<byte[]>) input.receive(10000); assertThat(in).isNotNull(); assertThat(new String(in.getPayload())).isEqualTo("bar"); assertThat(in.getHeaders().get(AmqpHeaders.DELIVERY_MODE)).isNull(); producerBinding.unbind(); consumerBinding.unbind(); }