Java tutorial
/** * Copyright (c) Acroquest Technology Co, Ltd. All Rights Reserved. * Please read the associated COPYRIGHTS file for more details. * * THE SOFTWARE IS PROVIDED BY Acroquest Technolog Co., Ltd., * WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDER BE LIABLE FOR ANY * CLAIM, DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING * OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. */ package acromusashi.stream.component.rabbitmq; import java.text.MessageFormat; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import org.apache.commons.beanutils.BeanUtils; import org.apache.commons.lang.StringUtils; import org.springframework.amqp.core.AmqpTemplate; import org.springframework.amqp.rabbit.connection.AbstractConnectionFactory; import org.springframework.amqp.rabbit.connection.ConnectionFactory; import org.springframework.amqp.rabbit.core.RabbitTemplate; /** * RabbitMQ??<br> * <br> * ?????? */ public class AmqpTemplateFactory { /** ?????????? */ private AbstractContextBuilder contextBuilder; /** ??? */ private Map<String, AmqpTemplate> amqpTemplateMap = new ConcurrentHashMap<String, AmqpTemplate>(); /** * ????? */ public AmqpTemplateFactory() { } /** * ?????????????? * * @param contextBuilder ?????????? */ public AmqpTemplateFactory(AbstractContextBuilder contextBuilder) { setContextBuilder(contextBuilder); } /** * ?????<br> * ??????????????????? * * @param queueName ?? * @return ??? * @throws RabbitmqCommunicateException ?????? */ public AmqpTemplate getAmqpTemplate(String queueName) throws RabbitmqCommunicateException { if (queueName == null) { String message = "QueueName is not defined."; throw new RabbitmqCommunicateException(message); } AmqpTemplate template = this.amqpTemplateMap.get(queueName); if (template == null) { template = createAmqpTemplate(queueName); this.amqpTemplateMap.put(queueName, template); } return template; } /** * ????? * * @param queueName ?? * @return ?????? * @throws RabbitmqCommunicateException ?????????? */ protected AmqpTemplate createAmqpTemplate(String queueName) throws RabbitmqCommunicateException { List<String> processList = getProcessList(queueName); // ConnectionFactory?????RabbitMQ ConnectionFactory connectionFactory = getCachingConnectionFactory(queueName, processList); // AmqpTemplateRabbitTemplate?ConnectionFactory??? RabbitTemplate template = getRabbitTemplate(queueName, connectionFactory); return template; } /** * RabbitMQ?? * * @param queueName ?? * @return RabbitMQ * @throws RabbitmqCommunicateException ?????????? */ private List<String> getProcessList(String queueName) throws RabbitmqCommunicateException { List<String> processList = getContextBuilder().getProcessList(queueName); if (processList == null || processList.size() == 0) { String messageFmt = "QueueName's ProcessList is not defined. QueueName={0}"; String message = MessageFormat.format(messageFmt, queueName); throw new RabbitmqCommunicateException(message); } return processList; } /** * ConnectionFactory?? * * @param queueName ?? * @param processList RabbitMQ * @return ConnectionFactory * @throws RabbitmqCommunicateException connectionFactory????????? */ private AbstractConnectionFactory getCachingConnectionFactory(String queueName, List<String> processList) throws RabbitmqCommunicateException { AbstractConnectionFactory connectionFactory = null; try { connectionFactory = (AbstractConnectionFactory) getContextBuilder().getConnectionFactory(queueName); } catch (Exception ex) { throw new RabbitmqCommunicateException(ex); } String adresses = StringUtils.join(processList, ','); connectionFactory.setAddresses(adresses); return connectionFactory; } /** * RabbitTemplate?? * * @param queueName ?? * @param connectionFactory ConnectionFactory * @return RabbitTemplate * @throws RabbitmqCommunicateException amqpTemplate????????? */ private RabbitTemplate getRabbitTemplate(String queueName, ConnectionFactory connectionFactory) throws RabbitmqCommunicateException { // AmqpTemplate????????? RabbitTemplate template = null; try { template = (RabbitTemplate) BeanUtils.cloneBean(getContextBuilder().getAmqpTemplate(queueName)); } catch (Exception ex) { throw new RabbitmqCommunicateException(ex); } template.setConnectionFactory(connectionFactory); template.setExchange(queueName); template.setQueue(queueName); return template; } /** * @return the contextBuilder */ public AbstractContextBuilder getContextBuilder() { return this.contextBuilder; } /** * @param contextBuilder the contextBuilder to set */ public void setContextBuilder(AbstractContextBuilder contextBuilder) { this.contextBuilder = contextBuilder; } }