All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.bitsensor.plugins.shaded.org.springframework.amqp.rabbit.core.RabbitMessagingTemplate Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2002-2017 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.bitsensor.plugins.shaded.io.bitsensor.plugins.shaded.org.springframework.amqp.rabbit.core;

import java.util.Map;

import io.bitsensor.plugins.shaded.io.bitsensor.plugins.shaded.org.springframework.amqp.core.MessageProperties;
import io.bitsensor.plugins.shaded.io.bitsensor.plugins.shaded.org.springframework.amqp.support.converter.MessageConverter;
import io.bitsensor.plugins.shaded.io.bitsensor.plugins.shaded.org.springframework.amqp.support.converter.MessagingMessageConverter;
import io.bitsensor.plugins.shaded.org.springframework.beans.factory.InitializingBean;
import io.bitsensor.plugins.shaded.org.springframework.messaging.Message;
import io.bitsensor.plugins.shaded.org.springframework.messaging.MessagingException;
import io.bitsensor.plugins.shaded.org.springframework.messaging.converter.MessageConversionException;
import io.bitsensor.plugins.shaded.org.springframework.messaging.core.AbstractMessagingTemplate;
import io.bitsensor.plugins.shaded.org.springframework.messaging.core.MessagePostProcessor;
import io.bitsensor.plugins.shaded.org.springframework.util.Assert;

/**
 * An implementation of {@link RabbitMessageOperations}.
 *
 * @author Stephane Nicoll
 * @author Gary Russell
 * @since 1.4
 */
public class RabbitMessagingTemplate extends AbstractMessagingTemplate
		implements RabbitMessageOperations, InitializingBean {

	private RabbitTemplate rabbitTemplate;

	private MessageConverter amqpMessageConverter = new MessagingMessageConverter();

	private boolean converterSet;


	/**
	 * Constructor for use with bean properties.
	 * Requires {@link #setRabbitTemplate} to be called.
	 */
	public RabbitMessagingTemplate() {
	}

	/**
	 * Create an instance with the {@link RabbitTemplate} to use.
	 * @param rabbitTemplate the template.
	 */
	public RabbitMessagingTemplate(RabbitTemplate rabbitTemplate) {
		Assert.notNull(rabbitTemplate, "'rabbitTemplate' must not be null");
		this.rabbitTemplate = rabbitTemplate;
	}


	/**
	 * Set the {@link RabbitTemplate} to use.
	 * @param rabbitTemplate the template.
	 */
	public void setRabbitTemplate(RabbitTemplate rabbitTemplate) {
		this.rabbitTemplate = rabbitTemplate;
	}

	/**
	 * @return the configured {@link RabbitTemplate}.
	 */
	public RabbitTemplate getRabbitTemplate() {
		return this.rabbitTemplate;
	}

	/**
	 * Set the {@link MessageConverter} to use to convert a {@link Message} from
	 * the messaging to and from a {@link io.bitsensor.plugins.shaded.io.bitsensor.plugins.shaded.org.springframework.amqp.core.Message}.
	 * By default, a {@link MessagingMessageConverter} is defined using the provided
	 * {@link RabbitTemplate}'s message converter (a
	 * {@link io.bitsensor.plugins.shaded.io.bitsensor.plugins.shaded.org.springframework.amqp.support.converter.SimpleMessageConverter}
	 * by default) to convert the payload of the message.
	 * 

Consider configuring a {@link MessagingMessageConverter} with a different * {@link MessagingMessageConverter#setPayloadConverter(MessageConverter) payload converter} * for more advanced scenarios. * @param amqpMessageConverter the message converter. * @see MessagingMessageConverter */ public void setAmqpMessageConverter(MessageConverter amqpMessageConverter) { this.amqpMessageConverter = amqpMessageConverter; this.converterSet = true; } /** * @return the {@link MessageConverter} to use to convert a {@link io.bitsensor.plugins.shaded.org.springframework.messaging.Message} * from the messaging to and from a {@link io.bitsensor.plugins.shaded.io.bitsensor.plugins.shaded.org.springframework.amqp.core.Message}. */ public MessageConverter getAmqpMessageConverter() { return this.amqpMessageConverter; } @Override public void afterPropertiesSet() { Assert.notNull(getRabbitTemplate(), "Property 'rabbitTemplate' is required"); Assert.notNull(getAmqpMessageConverter(), "Property 'amqpMessageConverter' is required"); if (!this.converterSet && this.rabbitTemplate.getMessageConverter() != null) { ((MessagingMessageConverter) this.amqpMessageConverter) .setPayloadConverter(this.rabbitTemplate.getMessageConverter()); } } @Override public void send(String exchange, String routingKey, Message message) throws MessagingException { doSend(exchange, routingKey, message); } @Override public void convertAndSend(String exchange, String routingKey, Object payload) throws MessagingException { convertAndSend(exchange, routingKey, payload, (Map) null); } @Override public void convertAndSend(String exchange, String routingKey, Object payload, Map headers) throws MessagingException { convertAndSend(exchange, routingKey, payload, headers, null); } @Override public void convertAndSend(String exchange, String routingKey, Object payload, MessagePostProcessor postProcessor) throws MessagingException { convertAndSend(exchange, routingKey, payload, null, postProcessor); } @Override public void convertAndSend(String exchange, String routingKey, Object payload, Map headers, MessagePostProcessor postProcessor) throws MessagingException { Message message = doConvert(payload, headers, postProcessor); send(exchange, routingKey, message); } @Override public Message sendAndReceive(String exchange, String routingKey, Message requestMessage) throws MessagingException { return doSendAndReceive(exchange, routingKey, requestMessage); } @Override public T convertSendAndReceive(String exchange, String routingKey, Object request, Class targetClass) throws MessagingException { return convertSendAndReceive(exchange, routingKey, request, null, targetClass); } @Override public T convertSendAndReceive(String exchange, String routingKey, Object request, Map headers, Class targetClass) throws MessagingException { return convertSendAndReceive(exchange, routingKey, request, headers, targetClass, null); } @Override public T convertSendAndReceive(String exchange, String routingKey, Object request, Class targetClass, MessagePostProcessor requestPostProcessor) throws MessagingException { return convertSendAndReceive(exchange, routingKey, request, null, targetClass, requestPostProcessor); } @SuppressWarnings("unchecked") @Override public T convertSendAndReceive(String exchange, String routingKey, Object request, Map headers, Class targetClass, MessagePostProcessor requestPostProcessor) throws MessagingException { Message requestMessage = doConvert(request, headers, requestPostProcessor); Message replyMessage = sendAndReceive(exchange, routingKey, requestMessage); return (replyMessage != null ? (T) getMessageConverter().fromMessage(replyMessage, targetClass) : null); } @Override protected void doSend(String destination, Message message) { try { this.rabbitTemplate.send(destination, createMessage(message)); } catch (RuntimeException ex) { throw convertAmqpException(ex); } } protected void doSend(String exchange, String routingKey, Message message) { try { this.rabbitTemplate.send(exchange, routingKey, createMessage(message)); } catch (RuntimeException ex) { throw convertAmqpException(ex); } } @Override protected Message doReceive(String destination) { try { io.bitsensor.plugins.shaded.io.bitsensor.plugins.shaded.org.springframework.amqp.core.Message amqpMessage = this.rabbitTemplate.receive(destination); return convertAmqpMessage(amqpMessage); } catch (RuntimeException ex) { throw convertAmqpException(ex); } } @Override protected Message doSendAndReceive(String destination, Message requestMessage) { try { io.bitsensor.plugins.shaded.io.bitsensor.plugins.shaded.org.springframework.amqp.core.Message amqpMessage = this.rabbitTemplate.sendAndReceive( destination, createMessage(requestMessage)); return convertAmqpMessage(amqpMessage); } catch (RuntimeException ex) { throw convertAmqpException(ex); } } protected Message doSendAndReceive(String exchange, String routingKey, Message requestMessage) { try { io.bitsensor.plugins.shaded.io.bitsensor.plugins.shaded.org.springframework.amqp.core.Message amqpMessage = this.rabbitTemplate.sendAndReceive( exchange, routingKey, createMessage(requestMessage)); return convertAmqpMessage(amqpMessage); } catch (RuntimeException ex) { throw convertAmqpException(ex); } } private io.bitsensor.plugins.shaded.io.bitsensor.plugins.shaded.org.springframework.amqp.core.Message createMessage(Message message) { try { return getAmqpMessageConverter().toMessage(message, new MessageProperties()); } catch (io.bitsensor.plugins.shaded.io.bitsensor.plugins.shaded.org.springframework.amqp.support.converter.MessageConversionException ex) { throw new MessageConversionException("Could not convert '" + message + "'", ex); } } protected Message convertAmqpMessage(io.bitsensor.plugins.shaded.io.bitsensor.plugins.shaded.org.springframework.amqp.core.Message message) { if (message == null) { return null; } try { return (Message) getAmqpMessageConverter().fromMessage(message); } catch (Exception ex) { throw new MessageConversionException("Could not convert '" + message + "'", ex); } } @SuppressWarnings("ThrowableResultOfMethodCallIgnored") protected MessagingException convertAmqpException(RuntimeException ex) { if (ex instanceof MessagingException) { return (MessagingException) ex; } if (ex instanceof io.bitsensor.plugins.shaded.io.bitsensor.plugins.shaded.org.springframework.amqp.support.converter.MessageConversionException) { return new MessageConversionException(ex.getMessage(), ex); } // Fallback return new MessagingException(ex.getMessage(), ex); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy