
io.vlingo.xoom.lattice.exchange.rabbitmq.MessageProducer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of vlingo-lattice-exchange-rabbitmq Show documentation
Show all versions of vlingo-lattice-exchange-rabbitmq Show documentation
The vlingo/lattice io.vlingo.lattice.exchange.Exchange extension for RabbitMQ.
The newest version!
// Copyright © 2012-2021 VLINGO LABS. All rights reserved.
//
// This Source Code Form is subject to the terms of the
// Mozilla Public License, v. 2.0. If a copy of the MPL
// was not distributed with this file, You can obtain
// one at https://mozilla.org/MPL/2.0/.
package io.vlingo.xoom.lattice.exchange.rabbitmq;
import java.io.IOException;
import io.vlingo.xoom.lattice.exchange.ExchangeException;
import io.vlingo.xoom.lattice.exchange.MessageParameters;
/**
* A message producer that facilitates sending messages to a
* BrokerChannel, either a BrokerExchange or a BrokerQueue.
*/
class MessageProducer {
/** My broker connection, which is where I send messages. */
private final BrokerConnection brokerConnection;
/**
* Answer a new instance of a MessageProducer.
* @param brokerChannel the BrokerChannel where messages are to be sent
* @return MessageProducer
*/
public static MessageProducer instance(final BrokerConnection brokerChannel) {
return new MessageProducer(brokerChannel);
}
/**
* Close me, which closes my broker channel.
*/
public void close() {
brokerConnection.close();
}
/**
* Answers the receiver after sending binaryMessage to my channel. This is a
* producer ignorance way to use either an exchange or a queue channel without
* requiring it to pass specific parameters. By answering myself I allow for
* sending message bursts.
* @param binaryMessage the byte[] binary message to send
* @param messageParameters the MessageParameters
* @return MessageProducer
*/
public MessageProducer send(final byte[] binaryMessage, final MessageParameters messageParameters) {
check(messageParameters);
try {
brokerConnection.channel().basicPublish(brokerConnection.exchangeName(), brokerConnection.queueName(),
MessageParametersMapper.from(messageParameters), binaryMessage);
} catch (IOException e) {
throw new ExchangeException("Failed to send message to channel because: " + e.getMessage(), e);
}
return this;
}
/**
* Answers the receiver after sending binaryMessage to my channel with
* routingKey. This is a producer ignorance way to use an exchange without
* requiring it to pass the exchange name. By answering myself I allow for
* sending message bursts.
* @param routingKey the String routing key
* @param binaryMessage the byte[] binary message to send
* @param messageParameters the MessageParameters
* @return MessageProducer
*/
public MessageProducer send(final String routingKey, final byte[] binaryMessage, final MessageParameters messageParameters) {
check(messageParameters);
try {
brokerConnection.channel().basicPublish(brokerConnection.exchangeName(), routingKey,
MessageParametersMapper.from(messageParameters), binaryMessage);
} catch (IOException e) {
throw new ExchangeException("Failed to send message to channel because: " + e.getMessage(), e);
}
return this;
}
/**
* Answers the receiver after sending binaryMessage to my channel with exchange
* and routingKey. By answering myself I allow for sending message bursts.
* @param exchange the String name of the exchange
* @param routingKey the String routing key
* @param binaryMessage the byte[] binary message to send
* @param messageParameters the MessageParameters
* @return MessageProducer
*/
public MessageProducer send(final String exchange, final String routingKey, final byte[] binaryMessage, final MessageParameters messageParameters) {
check(messageParameters);
try {
brokerConnection.channel().basicPublish(exchange, routingKey,
MessageParametersMapper.from(messageParameters), binaryMessage);
} catch (IOException e) {
throw new ExchangeException("Failed to send message to channel because: " + e.getMessage(), e);
}
return this;
}
/**
* Constructs my default state.
* @param brokerConnection the BrokerChannel to which I send messages
*/
protected MessageProducer(final BrokerConnection brokerConnection) {
this.brokerConnection = brokerConnection;
}
/**
* Checks messageParameters for validity.
* @param messageParameters the MessageParameters to check
*/
private void check(final MessageParameters messageParameters) {
if (brokerConnection.durable) {
if (!messageParameters.isDurableDeliveryMode()) {
throw new IllegalArgumentException("MessageParameters must be durable.");
}
} else {
if (messageParameters.isDurableDeliveryMode()) {
throw new IllegalArgumentException("MessageParameters must not be durable.");
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy