com.consol.citrus.endpoint.direct.DirectSyncProducer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of citrus-base Show documentation
Show all versions of citrus-base Show documentation
Citrus base and default implementation
package com.consol.citrus.endpoint.direct;
import java.util.UUID;
import com.consol.citrus.context.TestContext;
import com.consol.citrus.exceptions.MessageTimeoutException;
import com.consol.citrus.exceptions.ReplyMessageTimeoutException;
import com.consol.citrus.message.DefaultMessageQueue;
import com.consol.citrus.message.Message;
import com.consol.citrus.message.MessageQueue;
import com.consol.citrus.message.correlation.CorrelationManager;
import com.consol.citrus.message.correlation.PollingCorrelationManager;
import com.consol.citrus.messaging.ReplyConsumer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Christoph Deppisch
*/
public class DirectSyncProducer extends DirectProducer implements ReplyConsumer {
/** Logger */
private static Logger log = LoggerFactory.getLogger(DirectSyncProducer.class);
/** Store of reply messages */
private CorrelationManager correlationManager;
/** Endpoint configuration */
private final DirectSyncEndpointConfiguration endpointConfiguration;
/**
* Default constructor using endpoint configuration.
*
* @param name
* @param endpointConfiguration
*/
public DirectSyncProducer(String name, DirectSyncEndpointConfiguration endpointConfiguration) {
super(name, endpointConfiguration);
this.endpointConfiguration = endpointConfiguration;
this.correlationManager = new PollingCorrelationManager<>(endpointConfiguration, "Reply message did not arrive yet");
}
@Override
public void send(Message message, TestContext context) {
String correlationKeyName = endpointConfiguration.getCorrelator().getCorrelationKeyName(getName());
String correlationKey = endpointConfiguration.getCorrelator().getCorrelationKey(message);
correlationManager.saveCorrelationKey(correlationKeyName, correlationKey, context);
String destinationQueueName = getDestinationQueueName();
if (log.isDebugEnabled()) {
log.debug("Sending message to queue: '" + destinationQueueName + "'");
log.debug("Message to send is:\n" + message.toString());
}
log.info("Message was sent to queue: '" + destinationQueueName + "'");
MessageQueue replyQueue = getReplyQueue(message, context);
getDestinationQueue(context).send(message);
Message replyMessage = replyQueue.receive(endpointConfiguration.getTimeout());
if (replyMessage == null) {
throw new ReplyMessageTimeoutException(endpointConfiguration.getTimeout(), destinationQueueName);
} else {
log.info("Received synchronous response from reply queue");
}
correlationManager.store(correlationKey, replyMessage);
}
/**
* Reads reply queue from message header or creates a new temporary queue.
* @param message
* @param context
* @return
*/
private MessageQueue getReplyQueue(Message message, TestContext context) {
if (message.getHeader(DirectMessageHeaders.REPLY_QUEUE) == null) {
MessageQueue temporaryQueue = new DefaultMessageQueue(getName() + "." + UUID.randomUUID().toString());
message.setHeader(DirectMessageHeaders.REPLY_QUEUE, temporaryQueue);
return temporaryQueue;
}
if (message.getHeader(DirectMessageHeaders.REPLY_QUEUE) instanceof MessageQueue) {
return (MessageQueue)message.getHeader(DirectMessageHeaders.REPLY_QUEUE);
} else {
return resolveQueueName(message.getHeader(DirectMessageHeaders.REPLY_QUEUE).toString(), context);
}
}
@Override
public Message receive(TestContext context) {
return receive(correlationManager.getCorrelationKey(
endpointConfiguration.getCorrelator().getCorrelationKeyName(getName()), context), context);
}
@Override
public Message receive(String selector, TestContext context) {
return receive(selector, context, endpointConfiguration.getTimeout());
}
@Override
public Message receive(TestContext context, long timeout) {
return receive(correlationManager.getCorrelationKey(
endpointConfiguration.getCorrelator().getCorrelationKeyName(getName()), context), context, timeout);
}
@Override
public Message receive(String selector, TestContext context, long timeout) {
Message message = correlationManager.find(selector, timeout);
if (message == null) {
throw new MessageTimeoutException(timeout, getDestinationQueueName());
}
return message;
}
/**
* Gets the correlation manager.
* @return
*/
public CorrelationManager getCorrelationManager() {
return correlationManager;
}
/**
* Sets the correlation manager.
* @param correlationManager
*/
public void setCorrelationManager(CorrelationManager correlationManager) {
this.correlationManager = correlationManager;
}
}