Java Code Examples for org.springframework.messaging.support.MessageBuilder#build()

The following examples show how to use org.springframework.messaging.support.MessageBuilder#build() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RocketMQUtil.java    From rocketmq-spring with Apache License 2.0 6 votes vote down vote up
public static org.springframework.messaging.Message convertToSpringMessage(
    org.apache.rocketmq.common.message.MessageExt message) {
    MessageBuilder messageBuilder =
        MessageBuilder.withPayload(message.getBody()).
            setHeader(toRocketHeaderKey(RocketMQHeaders.KEYS), message.getKeys()).
            setHeader(toRocketHeaderKey(RocketMQHeaders.TAGS), message.getTags()).
            setHeader(toRocketHeaderKey(RocketMQHeaders.TOPIC), message.getTopic()).
            setHeader(toRocketHeaderKey(RocketMQHeaders.MESSAGE_ID), message.getMsgId()).
            setHeader(toRocketHeaderKey(RocketMQHeaders.BORN_TIMESTAMP), message.getBornTimestamp()).
            setHeader(toRocketHeaderKey(RocketMQHeaders.BORN_HOST), message.getBornHostString()).
            setHeader(toRocketHeaderKey(RocketMQHeaders.FLAG), message.getFlag()).
            setHeader(toRocketHeaderKey(RocketMQHeaders.QUEUE_ID), message.getQueueId()).
            setHeader(toRocketHeaderKey(RocketMQHeaders.SYS_FLAG), message.getSysFlag()).
            setHeader(toRocketHeaderKey(RocketMQHeaders.TRANSACTION_ID), message.getTransactionId());
    addUserProperties(message.getProperties(), messageBuilder);
    return messageBuilder.build();
}
 
Example 2
Source File: RocketMQTemplate.java    From rocketmq-spring with Apache License 2.0 5 votes vote down vote up
@Override
protected Message<?> doConvert(Object payload, Map<String, Object> headers, MessagePostProcessor postProcessor) {
    Message<?> message = super.doConvert(payload, headers, postProcessor);
    MessageBuilder<?> builder = MessageBuilder.fromMessage(message);
    builder.setHeaderIfAbsent(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.TEXT_PLAIN);
    return builder.build();
}
 
Example 3
Source File: SimpleFunctionRegistry.java    From spring-cloud-function with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({"rawtypes", "unchecked"})
private Message convertValueToMessage(Object value, Function<Message, Message> enricher, MimeType acceptedContentType) {
	Message outputMessage = null;
	if (value instanceof Message) {
		MessageHeaders headers = ((Message) value).getHeaders();
		Map<String, Object> headersMap = (Map<String, Object>) ReflectionUtils
			.getField(this.headersField, headers);
		headersMap.put("accept", acceptedContentType);
		// Set the contentType header to the value of accept for "legacy" reasons. But, do not set the
		// contentType header to the value of accept if it is a wildcard type, as this doesn't make sense.
		// This also applies to the else branch below.
		if (acceptedContentType.isConcrete()) {
			headersMap.put(MessageHeaders.CONTENT_TYPE, acceptedContentType);
		}
	}
	else {
		MessageBuilder<Object> builder = MessageBuilder.withPayload(value)
			.setHeader("accept", acceptedContentType);
		if (acceptedContentType.isConcrete()) {
			builder.setHeader(MessageHeaders.CONTENT_TYPE, acceptedContentType);
		}
		value = builder.build();
	}
	if (enricher != null) {
		value = enricher.apply((Message) value);
	}
	outputMessage = messageConverter.toMessage(((Message) value).getPayload(), ((Message) value).getHeaders());
	return outputMessage;
}
 
Example 4
Source File: SimpMessageTypeMessageConditionTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private Message<byte[]> message(SimpMessageType messageType) {
	MessageBuilder<byte[]> builder = MessageBuilder.withPayload(new byte[0]);
	if (messageType != null) {
		builder.setHeader(SimpMessageHeaderAccessor.MESSAGE_TYPE_HEADER, messageType);
	}
	return builder.build();
}
 
Example 5
Source File: AbstractMessageConverter.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public final Message<?> toMessage(Object payload, MessageHeaders headers, Object conversionHint) {
	if (!canConvertTo(payload, headers)) {
		return null;
	}

	payload = convertToInternal(payload, headers, conversionHint);
	if (payload == null) {
		return null;
	}

	MimeType mimeType = getDefaultContentType(payload);
	if (headers != null) {
		MessageHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(headers, MessageHeaderAccessor.class);
		if (accessor != null && accessor.isMutable()) {
			accessor.setHeaderIfAbsent(MessageHeaders.CONTENT_TYPE, mimeType);
			return MessageBuilder.createMessage(payload, accessor.getMessageHeaders());
		}
	}

	MessageBuilder<?> builder = MessageBuilder.withPayload(payload);
	if (headers != null) {
		builder.copyHeaders(headers);
	}
	builder.setHeaderIfAbsent(MessageHeaders.CONTENT_TYPE, mimeType);
	return builder.build();
}
 
Example 6
Source File: TracingChannelInterceptor.java    From java-spring-cloud with Apache License 2.0 5 votes vote down vote up
private Message<?> preSendClientSpan(Message<?> message) {
  Span span = tracer.buildSpan((String) message.getHeaders()
      .getOrDefault(SIMP_DESTINATION, UNKNOWN_DESTINATION))
      .withTag(Tags.SPAN_KIND.getKey(), spanKind)
      .withTag(Tags.COMPONENT.getKey(), WEBSOCKET)
      .start();
  MessageBuilder<?> messageBuilder = MessageBuilder.fromMessage(message)
      .setHeader(OPENTRACING_SPAN, span);
  tracer
      .inject(span.context(), Format.Builtin.TEXT_MAP, new TextMapInjectAdapter(messageBuilder));
  return messageBuilder.build();
}
 
Example 7
Source File: StatemachineService.java    From tools-journey with Apache License 2.0 5 votes vote down vote up
public void execute(Integer businessId, TurnstileEvents event, Map<String, Object> context) {
    // 利用随记ID创建状态机,创建时没有与具体定义状态机绑定
    StateMachine<TurnstileStates, TurnstileEvents> stateMachine = stateMachineFactory.getStateMachine(UUID.randomUUID());
    stateMachine.start();
    try {
        // 在BizStateMachinePersist的restore过程中,绑定turnstileStateMachine状态机相关事件监听
        stateMachinePersist.restore(stateMachine, businessId);
        // 本处写法较为繁琐,实际为注入Map<String, Object> context内容到message中
        MessageBuilder<TurnstileEvents> messageBuilder = MessageBuilder
                .withPayload(event)
                .setHeader("BusinessId", businessId);
        if (context != null) {
            context.entrySet().forEach(p -> messageBuilder.setHeader(p.getKey(), p.getValue()));
        }
        Message<TurnstileEvents> message = messageBuilder.build();

        // 发送事件,返回是否执行成功
        boolean success = stateMachine.sendEvent(message);
        if (success) {
            stateMachinePersist.persist(stateMachine, businessId);
        } else {
            System.out.println("状态机处理未执行成功,请处理,ID:" + businessId + ",当前context:" + context);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        stateMachine.stop();
    }
}
 
Example 8
Source File: RocketMQTemplate.java    From spring-boot-starter-rocketmq with Apache License 2.0 5 votes vote down vote up
@Override
protected Message<?> doConvert(Object payload, Map<String, Object> headers, MessagePostProcessor postProcessor) {
    String content;
    if (payload instanceof String) {
        content = (String) payload;
    } else {
        // if payload not as string, use objectMapper change it.
        try {
            content = objectMapper.writeValueAsString(payload);
        } catch (JsonProcessingException e) {
            log.info("convert payload to String failed. payload:{}", payload);
            throw new RuntimeException("convert to payload to String failed.", e);
        }
    }

    MessageBuilder<?> builder = MessageBuilder.withPayload(content);
    if (headers != null) {
        builder.copyHeaders(headers);
    }
    builder.setHeaderIfAbsent(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.TEXT_PLAIN);

    Message<?> message = builder.build();
    if (postProcessor != null) {
        message = postProcessor.postProcessMessage(message);
    }
    return message;
}
 
Example 9
Source File: SenderService.java    From spring-cloud-alibaba with Apache License 2.0 5 votes vote down vote up
public <T> void sendTransactionalMsg(T msg, int num) throws Exception {
	MessageBuilder builder = MessageBuilder.withPayload(msg)
			.setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_JSON);
	builder.setHeader("test", String.valueOf(num));
	builder.setHeader(RocketMQHeaders.TAGS, "binder");
	Message message = builder.build();
	source.output2().send(message);
}
 
Example 10
Source File: TracingChannelInterceptor.java    From java-specialagent with Apache License 2.0 5 votes vote down vote up
private Message<?> preSendClientSpan(final Message<?> message) {
  final String destination = (String)message.getHeaders().get(SIMP_DESTINATION);
  final Span span = tracer
    .buildSpan(destination != null ? destination : UNKNOWN_DESTINATION)
    .withTag(Tags.SPAN_KIND.getKey(), spanKind)
    .withTag(Tags.COMPONENT.getKey(), WEBSOCKET).start();
  final MessageBuilder<?> messageBuilder = MessageBuilder.fromMessage(message).setHeader(OPENTRACING_SPAN, span);
  tracer.inject(span.context(), Format.Builtin.TEXT_MAP, new TextMapInjectAdapter(messageBuilder));
  return messageBuilder.build();
}
 
Example 11
Source File: AbstractMessageConverter.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
@Nullable
public final Message<?> toMessage(Object payload, @Nullable MessageHeaders headers, @Nullable Object conversionHint) {
	if (!canConvertTo(payload, headers)) {
		return null;
	}

	Object payloadToUse = convertToInternal(payload, headers, conversionHint);
	if (payloadToUse == null) {
		return null;
	}

	MimeType mimeType = getDefaultContentType(payloadToUse);
	if (headers != null) {
		MessageHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(headers, MessageHeaderAccessor.class);
		if (accessor != null && accessor.isMutable()) {
			if (mimeType != null) {
				accessor.setHeaderIfAbsent(MessageHeaders.CONTENT_TYPE, mimeType);
			}
			return MessageBuilder.createMessage(payloadToUse, accessor.getMessageHeaders());
		}
	}

	MessageBuilder<?> builder = MessageBuilder.withPayload(payloadToUse);
	if (headers != null) {
		builder.copyHeaders(headers);
	}
	if (mimeType != null) {
		builder.setHeaderIfAbsent(MessageHeaders.CONTENT_TYPE, mimeType);
	}
	return builder.build();
}
 
Example 12
Source File: RocketMQUtil.java    From rocketmq-spring with Apache License 2.0 5 votes vote down vote up
public static org.springframework.messaging.Message convertToSpringMessage(
    org.apache.rocketmq.common.message.Message message) {
    MessageBuilder messageBuilder =
        MessageBuilder.withPayload(message.getBody()).
            setHeader(toRocketHeaderKey(RocketMQHeaders.KEYS), message.getKeys()).
            setHeader(toRocketHeaderKey(RocketMQHeaders.TAGS), message.getTags()).
            setHeader(toRocketHeaderKey(RocketMQHeaders.TOPIC), message.getTopic()).
            setHeader(toRocketHeaderKey(RocketMQHeaders.FLAG), message.getFlag()).
            setHeader(toRocketHeaderKey(RocketMQHeaders.TRANSACTION_ID), message.getTransactionId());
    addUserProperties(message.getProperties(), messageBuilder);
    return messageBuilder.build();
}
 
Example 13
Source File: DefaultRocketMQListenerContainer.java    From rocketmq-spring with Apache License 2.0 5 votes vote down vote up
private Message<?> doConvert(Object payload, MessageHeaders headers) {
    Message<?> message = this.messageConverter instanceof SmartMessageConverter ?
        ((SmartMessageConverter) this.messageConverter).toMessage(payload, headers, null) :
        this.messageConverter.toMessage(payload, headers);
    if (message == null) {
        String payloadType = payload.getClass().getName();
        Object contentType = headers != null ? headers.get(MessageHeaders.CONTENT_TYPE) : null;
        throw new MessageConversionException("Unable to convert payload with type='" + payloadType +
            "', contentType='" + contentType + "', converter=[" + this.messageConverter + "]");
    }
    MessageBuilder<?> builder = MessageBuilder.fromMessage(message);
    builder.setHeaderIfAbsent(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.TEXT_PLAIN);
    return builder.build();
}
 
Example 14
Source File: SimpMessageTypeMessageConditionTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
private Message<byte[]> message(SimpMessageType messageType) {
	MessageBuilder<byte[]> builder = MessageBuilder.withPayload(new byte[0]);
	if (messageType != null) {
		builder.setHeader(SimpMessageHeaderAccessor.MESSAGE_TYPE_HEADER, messageType);
	}
	return builder.build();
}
 
Example 15
Source File: AbstractMessageConverter.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
@Nullable
public final Message<?> toMessage(Object payload, @Nullable MessageHeaders headers, @Nullable Object conversionHint) {
	if (!canConvertTo(payload, headers)) {
		return null;
	}

	Object payloadToUse = convertToInternal(payload, headers, conversionHint);
	if (payloadToUse == null) {
		return null;
	}

	MimeType mimeType = getDefaultContentType(payloadToUse);
	if (headers != null) {
		MessageHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(headers, MessageHeaderAccessor.class);
		if (accessor != null && accessor.isMutable()) {
			if (mimeType != null) {
				accessor.setHeaderIfAbsent(MessageHeaders.CONTENT_TYPE, mimeType);
			}
			return MessageBuilder.createMessage(payloadToUse, accessor.getMessageHeaders());
		}
	}

	MessageBuilder<?> builder = MessageBuilder.withPayload(payloadToUse);
	if (headers != null) {
		builder.copyHeaders(headers);
	}
	if (mimeType != null) {
		builder.setHeaderIfAbsent(MessageHeaders.CONTENT_TYPE, mimeType);
	}
	return builder.build();
}
 
Example 16
Source File: SenderService.java    From hello-spring-cloud-alibaba with MIT License 5 votes vote down vote up
public <T> void sendTransactionalMsg(T msg, int num) throws Exception {
    MessageBuilder builder = MessageBuilder.withPayload(msg)
            .setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_JSON);
    builder.setHeader("test", String.valueOf(num));
    builder.setHeader(RocketMQHeaders.TAGS, "binder");
    Message message = builder.build();
    source.output2().send(message);
}
 
Example 17
Source File: SimpMessageTypeMessageConditionTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private Message<byte[]> message(SimpMessageType messageType) {
	MessageBuilder<byte[]> builder = MessageBuilder.withPayload(new byte[0]);
	if (messageType != null) {
		builder.setHeader(SimpMessageHeaderAccessor.MESSAGE_TYPE_HEADER, messageType);
	}
	return builder.build();
}