org.springframework.amqp.support.converter.AbstractJavaTypeMapper Java Examples

The following examples show how to use org.springframework.amqp.support.converter.AbstractJavaTypeMapper. 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: BookController.java    From springboot-learning-experience with Apache License 2.0 6 votes vote down vote up
/**
 * this.rabbitTemplate.convertAndSend(RabbitConfig.REGISTER_DELAY_EXCHANGE, RabbitConfig.DELAY_ROUTING_KEY, book); 对应 {@link BookHandler#listenerDelayQueue}
 */
@GetMapping
public void defaultMessage() {
    Book book = new Book();
    book.setId("1");
    book.setName("一起来学Spring Boot");
    // 添加延时队列
    this.rabbitTemplate.convertAndSend(RabbitConfig.REGISTER_DELAY_EXCHANGE, RabbitConfig.DELAY_ROUTING_KEY, book, message -> {
        // TODO 第一句是可要可不要,根据自己需要自行处理
        message.getMessageProperties().setHeader(AbstractJavaTypeMapper.DEFAULT_CONTENT_CLASSID_FIELD_NAME, Book.class.getName());
        // TODO 如果配置了 params.put("x-message-ttl", 5 * 1000); 那么这一句也可以省略,具体根据业务需要是声明 Queue 的时候就指定好延迟时间还是在发送自己控制时间
        message.getMessageProperties().setExpiration(5 * 1000 + "");
        return message;
    });
    log.info("[发送时间] - [{}]", LocalDateTime.now());
}
 
Example #2
Source File: DmfSenderService.java    From hawkbit-examples with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Send a message if the message is not null.
 *
 * @param address
 *            the exchange name
 * @param message
 *            the amqp message which will be send if its not null
 */
public void sendMessage(final String address, final Message message) {
    if (message == null) {
        return;
    }
    message.getMessageProperties().getHeaders().remove(AbstractJavaTypeMapper.DEFAULT_CLASSID_FIELD_NAME);

    final String correlationId = UUID.randomUUID().toString();

    if (isCorrelationIdEmpty(message)) {
        message.getMessageProperties().setCorrelationId(correlationId);
    }

    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace("Sending message {} to exchange {} with correlationId {}", message, address, correlationId);
    } else {
        LOGGER.debug("Sending message to exchange {} with correlationId {}", address, correlationId);
    }

    rabbitTemplate.send(address, null, message, new CorrelationData(correlationId));
}
 
Example #3
Source File: AmqpMessageDispatcherServiceTest.java    From hawkbit with Eclipse Public License 1.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
private <T> T convertMessage(final Message message, final Class<T> clazz) {
    message.getMessageProperties().getHeaders().put(AbstractJavaTypeMapper.DEFAULT_CLASSID_FIELD_NAME,
            clazz.getTypeName());
    return (T) rabbitTemplate.getMessageConverter().fromMessage(message);
}
 
Example #4
Source File: MessageService.java    From hawkbit-examples with Eclipse Public License 1.0 3 votes vote down vote up
/**
 * Convert a message body to a given class and set the message header
 * AbstractJavaTypeMapper.DEFAULT_CLASSID_FIELD_NAME for Jackson converter.
 * 
 * @param message
 *            which body will converted
 * @param clazz
 *            the body class
 * @return the converted body
 */
@SuppressWarnings("unchecked")
public <T> T convertMessage(final Message message, final Class<T> clazz) {
    message.getMessageProperties().getHeaders().put(AbstractJavaTypeMapper.DEFAULT_CLASSID_FIELD_NAME,
            clazz.getTypeName());
    return (T) rabbitTemplate.getMessageConverter().fromMessage(message);
}
 
Example #5
Source File: BaseAmqpService.java    From hawkbit with Eclipse Public License 1.0 3 votes vote down vote up
/**
 * Is needed to convert a incoming message to is originally object type.
 *
 * @param message
 *            the message to convert.
 * @param clazz
 *            the class of the originally object.
 * @return the converted object
 */
@SuppressWarnings("unchecked")
public <T> T convertMessage(@NotNull final Message message, final Class<T> clazz) {
    checkMessageBody(message);
    message.getMessageProperties().getHeaders().put(AbstractJavaTypeMapper.DEFAULT_CLASSID_FIELD_NAME,
            clazz.getName());
    return (T) rabbitTemplate.getMessageConverter().fromMessage(message);
}
 
Example #6
Source File: BaseAmqpService.java    From hawkbit with Eclipse Public License 1.0 2 votes vote down vote up
/**
 * Clean message properties before sending a message.
 * 
 * @param message
 *            the message to cleaned up
 */
protected void cleanMessageHeaderProperties(final Message message) {
    message.getMessageProperties().getHeaders().remove(AbstractJavaTypeMapper.DEFAULT_CLASSID_FIELD_NAME);
}