001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.activemq.plugin;
018    
019    import java.io.File;
020    import java.io.FileInputStream;
021    import java.io.FileOutputStream;
022    import java.io.IOException;
023    import java.io.ObjectInputStream;
024    import java.io.ObjectOutputStream;
025    import java.util.concurrent.ConcurrentHashMap;
026    
027    import org.apache.activemq.broker.Broker;
028    import org.apache.activemq.broker.BrokerFilter;
029    import org.apache.activemq.broker.ConnectionContext;
030    import org.apache.activemq.broker.region.Subscription;
031    import org.apache.activemq.command.ConsumerInfo;
032    import org.slf4j.Logger;
033    import org.slf4j.LoggerFactory;
034    
035    /**
036     * A plugin which allows the caching of the selector from a subscription queue.
037     * <p/>
038     * This stops the build-up of unwanted messages, especially when consumers may
039     * disconnect from time to time when using virtual destinations.
040     * <p/>
041     * This is influenced by code snippets developed by Maciej Rakowicz
042     *
043     * @author Roelof Naude roelof(dot)naude(at)gmail.com
044     * @see https://issues.apache.org/activemq/browse/AMQ-3004
045     * @see http://mail-archives.apache.org/mod_mbox/activemq-users/201011.mbox/%3C8A013711-2613-450A-A487-379E784AF1D6@homeaway.co.uk%3E
046     */
047    public class SubQueueSelectorCacheBroker extends BrokerFilter implements Runnable {
048        private static final Logger LOG = LoggerFactory.getLogger(SubQueueSelectorCacheBroker.class);
049    
050        /**
051         * The subscription's selector cache. We cache compiled expressions keyed
052         * by the target destination.
053         */
054        private ConcurrentHashMap<String, String> subSelectorCache = new ConcurrentHashMap<String, String>();
055    
056        private final File persistFile;
057    
058        private boolean running = true;
059        private Thread persistThread;
060        private static final long MAX_PERSIST_INTERVAL = 600000;
061        private static final String SELECTOR_CACHE_PERSIST_THREAD_NAME = "SelectorCachePersistThread";
062    
063        /**
064         * Constructor
065         */
066        public SubQueueSelectorCacheBroker(Broker next, final File persistFile) {
067            super(next);
068            this.persistFile = persistFile;
069            LOG.info("Using persisted selector cache from[" + persistFile + "]");
070    
071            readCache();
072    
073            persistThread = new Thread(this, SELECTOR_CACHE_PERSIST_THREAD_NAME);
074            persistThread.start();
075        }
076    
077        @Override
078        public void stop() throws Exception {
079            running = false;
080            if (persistThread != null) {
081                persistThread.interrupt();
082                persistThread.join();
083            } //if
084        }
085    
086        @Override
087        public Subscription addConsumer(ConnectionContext context, ConsumerInfo info) throws Exception {
088            LOG.debug("Caching consumer selector [" + info.getSelector() + "] on a " + info.getDestination().getQualifiedName());
089            String selector = info.getSelector();
090    
091            // As ConcurrentHashMap doesn't support null values, use always true expression
092            if (selector == null) {
093                selector = "TRUE";
094            }
095    
096            subSelectorCache.put(info.getDestination().getQualifiedName(), selector);
097    
098            return super.addConsumer(context, info);
099        }
100    
101        private void readCache() {
102            if (persistFile != null && persistFile.exists()) {
103                try {
104                    FileInputStream fis = new FileInputStream(persistFile);
105                    try {
106                        ObjectInputStream in = new ObjectInputStream(fis);
107                        try {
108                            subSelectorCache = (ConcurrentHashMap<String, String>) in.readObject();
109                        } catch (ClassNotFoundException ex) {
110                            LOG.error("Invalid selector cache data found. Please remove file.", ex);
111                        } finally {
112                            in.close();
113                        } //try
114                    } finally {
115                        fis.close();
116                    } //try
117                } catch (IOException ex) {
118                    LOG.error("Unable to read persisted selector cache...it will be ignored!", ex);
119                } //try
120            } //if
121        }
122    
123        /**
124         * Persist the selector cache.
125         */
126        private void persistCache() {
127            LOG.debug("Persisting selector cache....");
128            try {
129                FileOutputStream fos = new FileOutputStream(persistFile);
130                try {
131                    ObjectOutputStream out = new ObjectOutputStream(fos);
132                    try {
133                        out.writeObject(subSelectorCache);
134                    } finally {
135                        out.flush();
136                        out.close();
137                    } //try
138                } catch (IOException ex) {
139                    LOG.error("Unable to persist selector cache", ex);
140                } finally {
141                    fos.close();
142                } //try
143            } catch (IOException ex) {
144                LOG.error("Unable to access file[" + persistFile + "]", ex);
145            } //try
146        }
147    
148        /**
149         * @return The JMS selector for the specified {@code destination}
150         */
151        public String getSelector(final String destination) {
152            return subSelectorCache.get(destination);
153        }
154    
155        /**
156         * Persist the selector cache every {@code MAX_PERSIST_INTERVAL}ms.
157         *
158         * @see java.lang.Runnable#run()
159         */
160        public void run() {
161            while (running) {
162                try {
163                    Thread.sleep(MAX_PERSIST_INTERVAL);
164                } catch (InterruptedException ex) {
165                } //try
166    
167                persistCache();
168            }
169        }
170    }
171