com.dell.cpsd.common.rabbitmq.context.RabbitContext Maven / Gradle / Ivy
/**
* Copyright © 2017 Dell Inc. or its subsidiaries. All Rights Reserved.
* Dell EMC Confidential/Proprietary Information
*/
package com.dell.cpsd.common.rabbitmq.context;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.listener.MessageListenerContainer;
import org.springframework.amqp.support.converter.AbstractJsonMessageConverter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
/**
*
* Copyright © 2017 Dell Inc. or its subsidiaries. All Rights Reserved.
* Dell EMC Confidential/Proprietary Information
*
*
* @since SINCE-TBD
*/
public class RabbitContext
{
private final String contextUuid;
private final RabbitAdmin admin;
private final RabbitTemplate rabbitTemplate;
private final AbstractJsonMessageConverter messageConverter;
private final Collection exchanges = new ArrayList<>();
private final Collection queues = new ArrayList<>();
private final Collection bindings = new ArrayList<>();
private final Collection containers = new ArrayList<>();
private final Map descriptionLookup = new HashMap<>();
private final Map replyToLookup = new HashMap<>();
public RabbitContext(String contextUuid, RabbitAdmin admin, RabbitTemplate rabbitTemplate,
AbstractJsonMessageConverter messageConverter, Collection exchanges, Collection queues,
Collection bindings, Collection descriptions, Collection containers,
Map replyToLookup)
{
this.contextUuid = contextUuid;
this.admin = admin;
this.rabbitTemplate = rabbitTemplate;
this.messageConverter = messageConverter;
addAll(this.exchanges, exchanges);
addAll(this.queues, queues);
addAll(this.bindings, bindings);
addAll(this.containers, containers);
putAll(this.replyToLookup, replyToLookup);
descriptions.forEach(d -> descriptionLookup.put(d.getMessageClass(), d));
}
private void addAll(Collection source, Collection additions)
{
if (additions != null)
{
source.addAll(additions);
}
}
private void putAll(Map source, Map additions)
{
if (additions != null)
{
source.putAll(additions);
}
}
public void declare()
{
exchanges.forEach(exchange -> admin.declareExchange(exchange));
queues.forEach(queue -> admin.declareQueue(queue));
bindings.forEach(binding -> admin.declareBinding(binding));
}
public void start()
{
containers.forEach(container -> container.start());
}
public String getContextUuid()
{
return contextUuid;
}
public RabbitAdmin getAdmin()
{
return admin;
}
public RabbitTemplate getRabbitTemplate()
{
return rabbitTemplate;
}
public AbstractJsonMessageConverter getMessageConverter()
{
return messageConverter;
}
public Collection getExchanges()
{
return exchanges;
}
public Collection getQueues()
{
return queues;
}
public Collection getBindings()
{
return bindings;
}
public MessageDescription getDescription(Class messageClass)
{
return descriptionLookup.get(messageClass);
}
public Collection getContainers()
{
return containers;
}
public String getReplyTo(Class request, Class reply)
{
return replyToLookup.get(new RequestReplyKey(request, reply));
}
}