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
018package org.apache.activemq.broker.scheduler.memory;
019
020import java.io.File;
021import java.util.HashMap;
022import java.util.Map;
023import java.util.concurrent.locks.ReentrantLock;
024
025import org.apache.activemq.broker.scheduler.JobScheduler;
026import org.apache.activemq.broker.scheduler.JobSchedulerStore;
027import org.apache.activemq.util.ServiceStopper;
028import org.apache.activemq.util.ServiceSupport;
029import org.slf4j.Logger;
030import org.slf4j.LoggerFactory;
031
032/**
033 * An in-memory JobSchedulerStore implementation used for Brokers that have persistence
034 * disabled or when the JobSchedulerStore usage doesn't require a file or DB based store
035 * implementation allowing for better performance.
036 */
037public class InMemoryJobSchedulerStore extends ServiceSupport implements JobSchedulerStore {
038
039    private static final Logger LOG = LoggerFactory.getLogger(InMemoryJobSchedulerStore.class);
040
041    private final ReentrantLock lock = new ReentrantLock();
042    private final Map<String, InMemoryJobScheduler> schedulers = new HashMap<String, InMemoryJobScheduler>();
043
044    @Override
045    protected void doStop(ServiceStopper stopper) throws Exception {
046        for (InMemoryJobScheduler scheduler : schedulers.values()) {
047            try {
048                scheduler.stop();
049            } catch (Exception e) {
050                LOG.error("Failed to stop scheduler: {}", scheduler.getName(), e);
051            }
052        }
053    }
054
055    @Override
056    protected void doStart() throws Exception {
057        for (InMemoryJobScheduler scheduler : schedulers.values()) {
058            try {
059                scheduler.start();
060            } catch (Exception e) {
061                LOG.error("Failed to start scheduler: {}", scheduler.getName(), e);
062            }
063        }
064    }
065
066    @Override
067    public JobScheduler getJobScheduler(String name) throws Exception {
068        this.lock.lock();
069        try {
070            InMemoryJobScheduler result = this.schedulers.get(name);
071            if (result == null) {
072                LOG.debug("Creating new in-memory scheduler: {}", name);
073                result = new InMemoryJobScheduler(name);
074                this.schedulers.put(name, result);
075                if (isStarted()) {
076                    result.start();
077                }
078            }
079            return result;
080        } finally {
081            this.lock.unlock();
082        }
083    }
084
085    @Override
086    public boolean removeJobScheduler(String name) throws Exception {
087        boolean result = false;
088
089        this.lock.lock();
090        try {
091            InMemoryJobScheduler scheduler = this.schedulers.remove(name);
092            result = scheduler != null;
093            if (result) {
094                LOG.debug("Removing in-memory Job Scheduler: {}", name);
095                scheduler.stop();
096                this.schedulers.remove(name);
097            }
098        } finally {
099            this.lock.unlock();
100        }
101        return result;
102    }
103
104    //---------- Methods that don't really apply to this implementation ------//
105
106    @Override
107    public long size() {
108        return 0;
109    }
110
111    @Override
112    public File getDirectory() {
113        return null;
114    }
115
116    @Override
117    public void setDirectory(File directory) {
118    }
119}