001package gu.simplemq.activemq;
002
003import gu.simplemq.Constant;
004import gu.simplemq.pool.BaseMQInstance;
005
006/**
007 * redis对象工厂类用于获取producer/consumer,publisher/subscriber,table对象<br>
008 * 应用程序结束时会自动清除所有subscriber和consumer
009 * @author guyadong
010 *
011 */
012public class ActivemqFactory implements Constant {
013
014        private ActivemqFactory() {}
015        private static  final BaseMQInstance<ActivemqConsumer,ActivemqPoolLazy> CONSUMERS = 
016                        new BaseMQInstance<ActivemqConsumer,ActivemqPoolLazy>(){
017                @Override
018                protected void beforeDelete(ActivemqConsumer r) {
019                        try {
020                                r.close();
021                        } catch (Exception e) {
022                                e.printStackTrace();
023                        }
024        }};
025        private static final BaseMQInstance<ActivemqSubscriber,ActivemqPoolLazy> SUBSCRIBERS = 
026                        new BaseMQInstance<ActivemqSubscriber,ActivemqPoolLazy>(){
027                @Override
028                protected void beforeDelete(ActivemqSubscriber r) {
029                        try {
030                                r.close();
031                        } catch (Exception e) {
032                                e.printStackTrace();
033                        }
034        }};
035        private static final BaseMQInstance<ActivemqProducer,ActivemqPoolLazy> PRODUCERS = 
036                        new BaseMQInstance<ActivemqProducer,ActivemqPoolLazy>(){
037                @Override
038                protected void beforeDelete(ActivemqProducer r) {
039                        r.close();
040        }};
041        private static final BaseMQInstance<ActivemqPublisher,ActivemqPoolLazy> PUBLISHERS = 
042                        new BaseMQInstance<ActivemqPublisher,ActivemqPoolLazy>(){};
043        /**
044         * 返回 {@link ActivemqPoolLazy}对应的{@link ActivemqConsumer}实例
045         * @param jedisPoolLazy
046         * @return
047         * @see gu.simplemq.pool.BaseMQInstance#getInstance(gu.simplemq.activemq.ActivemqPoolLazy)
048         */
049        public static ActivemqConsumer getConsumer(ActivemqPoolLazy jedisPoolLazy) {
050                return CONSUMERS.getInstance(jedisPoolLazy);
051        }
052        
053        /** 
054         * 返回{@link ActivemqPoolLazy}默认实例对应的{@link ActivemqConsumer}实例
055         * @see  {@link ActivemqPoolLazy#getDefaultInstance()}
056         */
057        public static ActivemqConsumer getConsumer() {
058                return CONSUMERS.getInstance(ActivemqPoolLazys.getDefaultInstance());
059        }
060        /**
061         * 返回 {@link ActivemqPoolLazy}对应的{@link ActivemqSubscriber}实例
062         * @param jedisPoolLazy
063         * @return
064         * @see gu.simplemq.pool.BaseMQInstance#getInstance(gu.simplemq.activemq.ActivemqPoolLazy)
065         */
066        public static ActivemqSubscriber getSubscriber(ActivemqPoolLazy jedisPoolLazy) {
067                return SUBSCRIBERS.getInstance(jedisPoolLazy);
068        }
069        /** 
070         * 返回{@link ActivemqPoolLazy}默认实例对应的{@link ActivemqSubscriber}实例
071         * @see  {@link ActivemqPoolLazy#getDefaultInstance()}
072         */
073        public static ActivemqSubscriber getSubscriber() {
074                return SUBSCRIBERS.getInstance(ActivemqPoolLazys.getDefaultInstance());
075        }
076        /**
077         * 返回 {@link ActivemqPoolLazy}对应的{@link ActivemqProducer}实例
078         * @param jedisPoolLazy
079         * @return
080         * @see gu.simplemq.pool.BaseMQInstance#getInstance(gu.simplemq.activemq.ActivemqPoolLazy)
081         */
082        public static ActivemqProducer getProducer(ActivemqPoolLazy jedisPoolLazy) {
083                return PRODUCERS.getInstance(jedisPoolLazy);
084        }
085        /** 
086         * 返回{@link ActivemqPoolLazy}默认实例对应的{@link ActivemqProducer}实例
087         * @see  {@link ActivemqPoolLazy#getDefaultInstance()}
088         */
089        public static ActivemqProducer getProducer() {
090                return PRODUCERS.getInstance(ActivemqPoolLazys.getDefaultInstance());
091        }
092        /**
093         * 返回 {@link ActivemqPoolLazy}对应的{@link ActivemqPublisher}实例
094         * @param jedisPoolLazy
095         * @return
096         * @see gu.simplemq.pool.BaseMQInstance#getInstance(gu.simplemq.activemq.ActivemqPoolLazy)
097         */
098        public static ActivemqPublisher getPublisher(ActivemqPoolLazy jedisPoolLazy) {
099                return PUBLISHERS.getInstance(jedisPoolLazy);
100        }
101        /** 
102         * 返回{@link ActivemqPoolLazy}默认实例对应的{@link ActivemqPublisher}实例
103         * @see  {@link ActivemqPoolLazy#getDefaultInstance()}
104         */
105        public static ActivemqPublisher getPublisher() {
106                return PUBLISHERS.getInstance(ActivemqPoolLazys.getDefaultInstance());
107        }
108        /**
109         * 关闭并删除所有consumer,subscriber,producer
110         */
111        public synchronized static void closeAll(){
112                CONSUMERS.clearInstances();
113                SUBSCRIBERS.clearInstances();
114                PRODUCERS.clearInstances();
115        }
116        static {
117                // JVM 结束时自动清除所有consumer和subscriber,producer对象
118                Runtime.getRuntime().addShutdownHook(new Thread(){
119
120                        @Override
121                        public void run() {
122                                try {
123                                        AdvisoryMessageManager.closeAll();
124                                        closeAll();
125                                        // 毁连接池对象
126                                        ActivemqPoolLazys.closeAll();
127                                } catch (Exception e) {
128                                        logger.error(e.toString());
129                                }
130                        }                       
131                });
132        }
133}