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.HashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; import org.springframework.amqp.core.AmqpTemplate; import org.springframework.amqp.rabbit.connection.ConnectionFactory; /** * RabbitMQ??<br> * <br> * ????????????? */ public abstract class AbstractContextBuilder { /** ?????*/ protected Map<String, RabbitmqClusterContext> contextMap; /** ????RabbitMQ (??????*/ protected Map<String, List<String>> processLists; /** * ????? * * @param contextList ? * @throws RabbitmqCommunicateException ?????????? */ public AbstractContextBuilder(List<RabbitmqClusterContext> contextList) throws RabbitmqCommunicateException { this.contextMap = initContextMap(contextList); this.processLists = initProcessLists(contextList); } /** * ???? * * @param queueName ?? * @return ?? * @throws RabbitmqCommunicateException ??????????? */ public abstract String getClientId(String queueName) throws RabbitmqCommunicateException; /** * ??????Map?? * * @param contextList ? * @return ??????Map * @throws RabbitmqCommunicateException ?????????? */ protected Map<String, RabbitmqClusterContext> initContextMap(List<RabbitmqClusterContext> contextList) throws RabbitmqCommunicateException { if (contextList.size() == 0) { String message = "RabbitMqClusterContext is empty."; throw new RabbitmqCommunicateException(message); } Map<String, RabbitmqClusterContext> contextMap = new HashMap<String, RabbitmqClusterContext>(); for (RabbitmqClusterContext context : contextList) { for (String queueName : context.getQueueList()) { //???????????? if (contextMap.containsKey(queueName)) { String messageFmt = "QueueName is not unique. QueueName={0}"; String message = MessageFormat.format(messageFmt, queueName); throw new RabbitmqCommunicateException(message); } contextMap.put(queueName, context); } } return contextMap; } /** * ????????????RabbitMQ?? * * @param contextList ? * @return RabbitMQ * @throws RabbitmqCommunicateException ?????????? */ protected Map<String, List<String>> initProcessLists(List<RabbitmqClusterContext> contextList) throws RabbitmqCommunicateException { if (this.contextMap == null) { this.contextMap = initContextMap(contextList); } Map<String, List<String>> processLists = new HashMap<String, List<String>>(); LinkedList<String> processList = null; String connectionProcess = null; int processIndex = 0; for (String queueName : this.contextMap.keySet()) { //??????? RabbitmqClusterContext context = this.contextMap.get(queueName); processList = new LinkedList<String>(context.getMqProcessList()); //RabbitMQ???????? //??(0) processIndex = 0; connectionProcess = context.getConnectionProcessMap().get(getClientId(queueName)); if (connectionProcess != null) { processIndex = processList.indexOf(connectionProcess); } //RabbitMQ?????RabbitMQ?? LinkedList<String> backwardProcesses = new LinkedList<String>(processList.subList(0, processIndex)); LinkedList<String> forwardProcesses = new LinkedList<String>( processList.subList(processIndex, processList.size())); forwardProcesses.addAll(backwardProcesses); processList = new LinkedList<String>(forwardProcesses); processLists.put(queueName, processList); } return processLists; } /** * ????RabbitMQ?? * * @param queueName ?? * @return RabbitMQ */ public List<String> getProcessList(String queueName) { // ??????? // this#initProcessLists()??processLists???????? if (this.processLists == null) { return null; } return getProcessLists().get(queueName); } /** * ????ConnectionFactory?? * * @param queueName ?? * @return ConnectionFactory */ public ConnectionFactory getConnectionFactory(String queueName) { // ??????? // this#initContextMap()??contextMap???????? if (this.contextMap == null) { return null; } RabbitmqClusterContext context = this.contextMap.get(queueName); if (context == null) { return null; } return context.getConnectionFactory(); } /** * ????AmqpTemplate?? * * @param queueName ?? * @return AmqpTemplate */ public AmqpTemplate getAmqpTemplate(String queueName) { // ??????? // this#initContextMap()??contextMap???????? if (this.contextMap == null) { return null; } RabbitmqClusterContext context = this.contextMap.get(queueName); if (context == null) { return null; } return context.getAmqpTemplate(); } /** * @return the processLists */ public Map<String, List<String>> getProcessLists() { return this.processLists; } /** * @return the contextMap */ public Map<String, RabbitmqClusterContext> getContextMap() { return this.contextMap; } }