List of usage examples for org.springframework.messaging.support MessageBuilder fromMessage
public static <T> MessageBuilder<T> fromMessage(Message<T> message)
From source file:org.springframework.cloud.stream.binder.kafka.streams.KafkaStreamsMessageConversionDelegate.java
/** * Deserialize incoming {@link KStream} based on content type. * * @param valueClass on KStream value/*from w w w.j a v a2 s.c o m*/ * @param bindingTarget inbound KStream target * @return deserialized KStream */ @SuppressWarnings({ "unchecked", "rawtypes" }) public KStream deserializeOnInbound(Class<?> valueClass, KStream<?, ?> bindingTarget) { MessageConverter messageConverter = this.compositeMessageConverterFactory .getMessageConverterForAllRegistered(); final PerRecordContentTypeHolder perRecordContentTypeHolder = new PerRecordContentTypeHolder(); resolvePerRecordContentType(bindingTarget, perRecordContentTypeHolder); //Deserialize using a branching strategy KStream<?, ?>[] branch = bindingTarget.branch( //First filter where the message is converted and return true if everything went well, return false otherwise. (o, o2) -> { boolean isValidRecord = false; try { //if the record is a tombstone, ignore and exit from processing further. if (o2 != null) { if (o2 instanceof Message || o2 instanceof String || o2 instanceof byte[]) { Message<?> m1 = null; if (o2 instanceof Message) { m1 = perRecordContentTypeHolder.contentType != null ? MessageBuilder.fromMessage((Message<?>) o2) .setHeader(MessageHeaders.CONTENT_TYPE, perRecordContentTypeHolder.contentType) .build() : (Message<?>) o2; } else { m1 = perRecordContentTypeHolder.contentType != null ? MessageBuilder.withPayload(o2) .setHeader(MessageHeaders.CONTENT_TYPE, perRecordContentTypeHolder.contentType) .build() : MessageBuilder.withPayload(o2).build(); } convertAndSetMessage(o, valueClass, messageConverter, m1); } else { keyValueThreadLocal.set(new KeyValue<>(o, o2)); } isValidRecord = true; } else { LOG.info("Received a tombstone record. This will be skipped from further processing."); } } catch (Exception e) { LOG.warn("Deserialization has failed. This will be skipped from further processing.", e); //pass through } return isValidRecord; }, //second filter that catches any messages for which an exception thrown in the first filter above. (k, v) -> true); //process errors from the second filter in the branch above. processErrorFromDeserialization(bindingTarget, branch[1]); //first branch above is the branch where the messages are converted, let it go through further processing. return branch[0].mapValues((o2) -> { Object objectValue = keyValueThreadLocal.get().value; keyValueThreadLocal.remove(); return objectValue; }); }
From source file:org.springframework.messaging.core.GenericMessagingTemplate.java
protected final void doSend(MessageChannel channel, Message<?> message, long timeout) { Assert.notNull(channel, "MessageChannel is required"); Message<?> messageToSend = message; MessageHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message, MessageHeaderAccessor.class); if (accessor != null && accessor.isMutable()) { accessor.removeHeader(this.sendTimeoutHeader); accessor.removeHeader(this.receiveTimeoutHeader); accessor.setImmutable();//from ww w . j a v a 2 s. co m } else if (message.getHeaders().containsKey(this.sendTimeoutHeader) || message.getHeaders().containsKey(this.receiveTimeoutHeader)) { messageToSend = MessageBuilder.fromMessage(message).setHeader(this.sendTimeoutHeader, null) .setHeader(this.receiveTimeoutHeader, null).build(); } boolean sent = (timeout >= 0 ? channel.send(messageToSend, timeout) : channel.send(messageToSend)); if (!sent) { throw new MessageDeliveryException(message, "Failed to send message to channel '" + channel + "' within timeout: " + timeout); } }
From source file:org.springframework.messaging.core.GenericMessagingTemplate.java
@Override @Nullable//from www. j av a 2 s . c om protected final Message<?> doSendAndReceive(MessageChannel channel, Message<?> requestMessage) { Assert.notNull(channel, "'channel' is required"); Object originalReplyChannelHeader = requestMessage.getHeaders().getReplyChannel(); Object originalErrorChannelHeader = requestMessage.getHeaders().getErrorChannel(); long sendTimeout = sendTimeout(requestMessage); long receiveTimeout = receiveTimeout(requestMessage); TemporaryReplyChannel tempReplyChannel = new TemporaryReplyChannel(this.throwExceptionOnLateReply); requestMessage = MessageBuilder.fromMessage(requestMessage).setReplyChannel(tempReplyChannel) .setHeader(this.sendTimeoutHeader, null).setHeader(this.receiveTimeoutHeader, null) .setErrorChannel(tempReplyChannel).build(); try { doSend(channel, requestMessage, sendTimeout); } catch (RuntimeException ex) { tempReplyChannel.setSendFailed(true); throw ex; } Message<?> replyMessage = this.doReceive(tempReplyChannel, receiveTimeout); if (replyMessage != null) { replyMessage = MessageBuilder.fromMessage(replyMessage) .setHeader(MessageHeaders.REPLY_CHANNEL, originalReplyChannelHeader) .setHeader(MessageHeaders.ERROR_CHANNEL, originalErrorChannelHeader).build(); } return replyMessage; }
From source file:org.springframework.messaging.handler.method.AbstractMethodMessageHandler.java
@Override public void handleMessage(Message<?> message) throws MessagingException { String destination = getDestination(message); if (destination == null) { logger.trace("Ignoring message, no destination"); return;/* w ww . j a v a 2 s . c o m*/ } String lookupDestination = getLookupDestination(destination); if (lookupDestination == null) { if (logger.isTraceEnabled()) { logger.trace("Ignoring message to destination=" + destination); } return; } if (logger.isDebugEnabled()) { logger.debug("Handling message, lookupDestination=" + lookupDestination); } message = MessageBuilder.fromMessage(message).setHeader(LOOKUP_DESTINATION_HEADER, lookupDestination) .build(); handleMessageInternal(message, lookupDestination); }
From source file:org.springframework.statemachine.ensemble.DistributedStateMachine.java
@Override public boolean sendEvent(Message<E> event) { // adding state machine id to the message so that // listeners can know from where a state change originates return delegate.sendEvent(MessageBuilder.fromMessage(event) .setHeader(StateMachineSystemConstants.STATEMACHINE_IDENTIFIER, delegate.getUuid()).build()); }
From source file:org.springframework.yarn.support.statemachine.AbstractStateMachine.java
@Override public void sendEvent(Message<E> event) { event = MessageBuilder.fromMessage(event).setHeader("machine", this).build(); if (log.isDebugEnabled()) { log.debug("Queue event " + event); }//from w ww .j a v a 2 s . com eventQueue.add(event); scheduleEventQueueProcessing(); }