001/** 002 * Copyright (c) 2015-2022, Michael Yang 杨福海 (fuhai999@gmail.com). 003 * <p> 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * <p> 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * <p> 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package io.jboot.components.mq; 017 018import io.jboot.Jboot; 019import io.jboot.components.mq.aliyunmq.JbootAliyunmqImpl; 020import io.jboot.components.mq.local.JbootLocalmqImpl; 021import io.jboot.components.mq.qpidmq.JbootQpidmqImpl; 022import io.jboot.components.mq.rabbitmq.JbootRabbitmqImpl; 023import io.jboot.components.mq.redismq.JbootRedismqImpl; 024import io.jboot.components.mq.rocketmq.JbootRocketmqImpl; 025import io.jboot.core.spi.JbootSpiLoader; 026import io.jboot.exception.JbootIllegalConfigException; 027import io.jboot.utils.ConfigUtil; 028 029import java.util.Map; 030import java.util.concurrent.ConcurrentHashMap; 031 032 033public class JbootmqManager { 034 035 private static JbootmqManager manager = new JbootmqManager(); 036 037 private JbootmqManager(){} 038 039 public static JbootmqManager me() { 040 return manager; 041 } 042 043 private Map<String, Jbootmq> jbootmqMap = new ConcurrentHashMap<>(); 044 045 public Jbootmq getJbootmq() { 046 return getJbootmq("default"); 047 } 048 049 050 public Jbootmq getJbootmq(String name) { 051 Jbootmq mq = jbootmqMap.get(name); 052 if (mq == null) { 053 synchronized (this) { 054 mq = jbootmqMap.get(name); 055 if (mq == null) { 056 Map<String, JbootmqConfig> configModels = ConfigUtil.getConfigModels(JbootmqConfig.class); 057 JbootmqConfig.TYPES.forEach(configModels::remove); 058 059 configModels.putIfAbsent("default", Jboot.config(JbootmqConfig.class)); 060 061 JbootmqConfig mqConfig = null; 062 if (!configModels.containsKey(name)) { 063 for (JbootmqConfig config : configModels.values()) { 064 if (name.equals(config.getTypeName())) { 065 mqConfig = config; 066 break; 067 } 068 } 069 if (mqConfig == null) { 070 throw new JbootIllegalConfigException("Please config \"jboot.mq.other" + name + ".type\" in your jboot.properties."); 071 } 072 } 073 else { 074 mqConfig = configModels.get(name); 075 } 076 077 mq = getJbootmq(mqConfig); 078 if (mq != null) { 079 jbootmqMap.put(name, mq); 080 } 081 } 082 } 083 } 084 return mq; 085 } 086 087 public Jbootmq getJbootmq(JbootmqConfig config) { 088 return buildJbootmq(config); 089 } 090 091 private Jbootmq buildJbootmq(JbootmqConfig config) { 092 if (config == null) { 093 throw new IllegalArgumentException("config must not be null"); 094 } 095 096 if (!config.isConfigOk()) { 097 return null; 098 } 099 100 switch (config.getType()) { 101 case JbootmqConfig.TYPE_REDIS: 102 return new JbootRedismqImpl(config); 103 case JbootmqConfig.TYPE_ALIYUNMQ: 104 return new JbootAliyunmqImpl(config); 105 case JbootmqConfig.TYPE_RABBITMQ: 106 return new JbootRabbitmqImpl(config); 107 case JbootmqConfig.TYPE_ROCKETMQ: 108 return new JbootRocketmqImpl(config); 109 case JbootmqConfig.TYPE_QPID: 110 return new JbootQpidmqImpl(config); 111 case JbootmqConfig.TYPE_ACTIVEMQ: 112 throw new RuntimeException("not finished!!!!"); 113 case JbootmqConfig.TYPE_LOCAL: 114 return new JbootLocalmqImpl(config); 115 default: 116 return JbootSpiLoader.load(Jbootmq.class, config.getType(), config); 117 } 118 119 } 120 121 122 public void init() { 123 jbootmqMap.values().forEach(Jbootmq::startListening); 124 } 125 126 public void stop() { 127 jbootmqMap.values().forEach(Jbootmq::stopListening); 128 } 129}