List of usage examples for org.springframework.messaging Message getPayload
T getPayload();
From source file:org.springframework.cloud.stream.app.field.value.counter.sink.FieldValueCounterSinkConfiguration.java
@ServiceActivator(inputChannel = Sink.INPUT) public void process(Message<?> message) { Object payload = message.getPayload(); if (payload instanceof byte[]) { try {/*from w w w. j av a2s.com*/ String stringPayload = new String((byte[]) payload, "UTF-8"); payload = jsonToTupleTransformer.transformPayload(stringPayload); } catch (Exception e) { throw new MessageTransformationException(message, e.getMessage(), e); } } if (payload instanceof Tuple) { processTuple(computeMetricName(message), (Tuple) payload); } else { processPojo(computeMetricName(message), payload); } }
From source file:org.springframework.cloud.stream.app.ftp.source.FtpSourceIntegrationTests.java
@Test public void sourceFilesAsRef() throws InterruptedException { assertEquals("*", TestUtils.getPropertyValue(TestUtils .getPropertyValue(sourcePollingChannelAdapter, "source.synchronizer.filter.fileFilters", Set.class) .iterator().next(), "path")); for (int i = 1; i <= 2; i++) { @SuppressWarnings("unchecked") Message<File> received = (Message<File>) messageCollector.forChannel(ftpSource.output()).poll(10, TimeUnit.SECONDS); assertNotNull(received);//from w w w . j a va 2s.co m assertThat(received.getPayload(), equalTo(new File(config.getLocalDir() + "/ftpSource" + i + ".txt"))); } assertThat(this.sessionFactory, instanceOf(CachingSessionFactory.class)); }
From source file:org.springframework.cloud.stream.app.gpfdist.sink.GpfdistMessageHandler.java
@Override protected void doWrite(Message<?> message) throws Exception { Object payload = message.getPayload(); if (payload instanceof String) { String data = (String) payload; if (delimiter != null) { processor.onNext(Buffer.wrap(data + delimiter)); } else {//from www . j a v a 2 s . c o m processor.onNext(Buffer.wrap(data)); } if (meter != null) { if ((meterCount++ % rateInterval) == 0) { meter.mark(rateInterval); log.info("METER: 1 minute rate = " + meter.getOneMinuteRate() + " mean rate = " + meter.getMeanRate()); } } } else { throw new MessageHandlingException(message, "message not a String"); } }
From source file:org.springframework.cloud.stream.app.hdfs.dataset.sink.HdfsDatasetSinkConfiguration.java
@Bean @ServiceActivator(inputChannel = "toSink") public MessageHandler datasetSinkMessageHandler(final DatasetOperations datasetOperations) { return new MessageHandler() { @Override//from w w w. j a v a2s. co m public void handleMessage(Message<?> message) throws MessagingException { Object payload = message.getPayload(); if (payload instanceof Collection<?>) { Collection<?> payloads = (Collection<?>) payload; logger.debug("Writing a collection of {} POJOs" + payloads.size()); datasetOperations.write((Collection<?>) message.getPayload()); } else { // This should never happen since message handler is fronted by an aggregator throw new IllegalStateException("Expected a collection of POJOs but received " + message.getPayload().getClass().getName()); } } }; }
From source file:org.springframework.cloud.stream.app.image.recognition.processor.ImageRecognitionOutputMessageBuilder.java
@Override public MessageBuilder<?> createOutputMessageBuilder(Message<?> inputMessage, Object computedScore) { Message<?> annotatedInput = inputMessage; if (this.drawLabels) { byte[] annotatedImage = drawLabels((byte[]) inputMessage.getPayload(), computedScore); annotatedInput = MessageBuilder.withPayload(annotatedImage).build(); }//from ww w. j a v a2s . co m return super.createOutputMessageBuilder(annotatedInput, computedScore); }
From source file:org.springframework.cloud.stream.app.log.sink.LogSinkApplicationTests.java
private void testMessage(Message<byte[]> message, String expectedPayload) { assertNotNull(this.sink.input()); assertEquals(LoggingHandler.Level.WARN, this.logSinkHandler.getLevel()); Log logger = TestUtils.getPropertyValue(this.logSinkHandler, "messageLogger", Log.class); assertEquals("foo", TestUtils.getPropertyValue(logger, "logger.name")); logger = spy(logger);//from w ww . j ava 2 s .c o m new DirectFieldAccessor(this.logSinkHandler).setPropertyValue("messageLogger", logger); this.sink.input().send(message); ArgumentCaptor<Object> captor = ArgumentCaptor.forClass(Object.class); verify(logger).warn(captor.capture()); assertEquals(expectedPayload.toUpperCase(), captor.getValue()); this.logSinkHandler.setLogExpressionString("#this"); this.sink.input().send(message); verify(logger, times(2)).warn(captor.capture()); Message captorMessage = (Message) captor.getAllValues().get(2); assertEquals("Unexpected payload value", expectedPayload, captorMessage.getPayload()); MessageHeaders messageHeaders = captorMessage.getHeaders(); assertEquals("Unexpected number of headers", 3, messageHeaders.size()); String[] headers = { "contentType" }; for (String header : headers) { assertTrue("Missing " + header + " header", messageHeaders.containsKey(header)); assertEquals("Header " + header + " does not match", messageHeaders.get(header), message.getHeaders().get(header)); } }
From source file:org.springframework.cloud.stream.app.object.detection.processor.ObjectDetectionOutputMessageBuilder.java
@Override public MessageBuilder<?> createOutputMessageBuilder(Message<?> inputMessage, Object computedScore) { Message<?> annotatedInput = inputMessage; List<ObjectDetection> objectDetections = (List<ObjectDetection>) computedScore; if (this.drawBoundingBox) { byte[] annotatedImage = drawBoundingBox((byte[]) inputMessage.getPayload(), objectDetections); annotatedInput = MessageBuilder.withPayload(annotatedImage).build(); }//from w ww. j a v a2s . co m return super.createOutputMessageBuilder(annotatedInput, toJson(objectDetections)); }
From source file:org.springframework.cloud.stream.app.pmml.processor.PmmlProcessorConfiguration.java
private MutableMessage<?> convertToMutable(Message<?> input) { Object payload = input.getPayload(); if (payload instanceof Tuple && !(payload instanceof MutableTuple)) { payload = TupleBuilder.mutableTuple().putAll((Tuple) payload).build(); }//from w ww . j av a 2s.c o m return new MutableMessage<>(payload, input.getHeaders()); }
From source file:org.springframework.cloud.stream.app.pose.estimation.processor.PoseEstimateOutputMessageBuilder.java
@Override public MessageBuilder<?> createOutputMessageBuilder(Message<?> inputMessage, Object computedScore) { Message<?> annotatedInput = inputMessage; List<Body> bodies = (List<Body>) computedScore; if (this.poseProperties.isDrawPoses()) { try {//w ww . j a v a2 s.co m byte[] annotatedImage = drawPoses((byte[]) inputMessage.getPayload(), bodies); annotatedInput = MessageBuilder.withPayload(annotatedImage).build(); } catch (IOException e) { logger.error("Failed to draw the poses", e); } } return super.createOutputMessageBuilder(annotatedInput, toJson(bodies)); }
From source file:org.springframework.cloud.stream.app.tensorflow.processor.TensorflowCommonProcessorConfiguration.java
@StreamListener(Processor.INPUT) @SendTo(Processor.OUTPUT)//from w w w . j a va 2 s. co m public Object evaluate(Message<?> input) { Object inputData = this.properties.getExpression() == null ? input.getPayload() : this.properties.getExpression().getValue(this.evaluationContext, input, Object.class); // The processorContext allows to convey metadata from the Input to Output converter. Map<String, Object> processorContext = new ConcurrentHashMap<>(); Map<String, Object> inputDataMap = this.tensorflowInputConverter.convert(inputData, processorContext); Map<String, Tensor<?>> outputTensorMap = this.tensorFlowService.evaluate(inputDataMap, this.properties.getModelFetch()); Object outputData = tensorflowOutputConverter.convert(outputTensorMap, processorContext); return tensorflowOutputMessageBuilder.createOutputMessageBuilder(input, outputData); }