home | career | drupal | java | mac | mysql | perl | scala | uml | unix  

ActiveMQ example source code file (AbstractTempRegion.java)

This example ActiveMQ source code file (AbstractTempRegion.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - ActiveMQ tags/keywords

abstracttempregion, activemqdestination, cacheddestination, cacheddestination, connectioncontext, connectioncontext, destination, destination, exception, exception, failed, taskrunnerfactory, timer, timertask, util

The ActiveMQ AbstractTempRegion.java source code

 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *      http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * See the License for the specific language governing permissions and
 * limitations under the License.
package org.apache.activemq.broker.region;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;

import org.apache.activemq.broker.ConnectionContext;
import org.apache.activemq.command.ActiveMQDestination;
import org.apache.activemq.thread.TaskRunnerFactory;
import org.apache.activemq.usage.SystemUsage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class AbstractTempRegion extends AbstractRegion {
    private static final Logger LOG = LoggerFactory.getLogger(TempQueueRegion.class);

    private Map<CachedDestination, Destination> cachedDestinations = new HashMap();
    private final boolean doCacheTempDestinations;
    private final int purgeTime;
    private Timer purgeTimer;
    private TimerTask purgeTask;

     * @param broker
     * @param destinationStatistics
     * @param memoryManager
     * @param taskRunnerFactory
     * @param destinationFactory
    public AbstractTempRegion(RegionBroker broker,
            DestinationStatistics destinationStatistics,
            SystemUsage memoryManager, TaskRunnerFactory taskRunnerFactory,
            DestinationFactory destinationFactory) {
        super(broker, destinationStatistics, memoryManager, taskRunnerFactory,
        this.purgeTime = broker.getBrokerService().getTimeBeforePurgeTempDestinations();
        if (this.doCacheTempDestinations) {
            this.purgeTimer = new Timer("ActiveMQ Temp destination purge timer", true);
            this.purgeTask = new TimerTask() {
                public void run() {
            this.purgeTimer.schedule(purgeTask, purgeTime, purgeTime);

    public void stop() throws Exception {
        if (purgeTimer != null) {

    protected abstract Destination doCreateDestination(
            ConnectionContext context, ActiveMQDestination destination)
            throws Exception;

    protected synchronized Destination createDestination(
            ConnectionContext context, ActiveMQDestination destination)
            throws Exception {
        Destination result = cachedDestinations.remove(new CachedDestination(
        if (result == null) {
            result = doCreateDestination(context, destination);
        return result;

    protected final synchronized void dispose(ConnectionContext context,
            Destination dest) throws Exception {
        // add to cache
        if (this.doCacheTempDestinations) {
            cachedDestinations.put(new CachedDestination(dest
                    .getActiveMQDestination()), dest);
        }else {
            try {
            } catch (Exception e) {
                LOG.warn("Failed to dispose of " + dest, e);

    private void doDispose(Destination dest) {
        ConnectionContext context = new ConnectionContext();
        try {
        } catch (Exception e) {
            LOG.warn("Failed to dispose of " + dest, e);


    private synchronized void doPurge() {
        long currentTime = System.currentTimeMillis();
        if (cachedDestinations.size() > 0) {
            Set<CachedDestination> tmp = new HashSet(
            for (CachedDestination key : tmp) {
                if ((key.timeStamp + purgeTime) < currentTime) {
                    Destination dest = cachedDestinations.remove(key);
                    if (dest != null) {

    static class CachedDestination {
        long timeStamp;

        ActiveMQDestination destination;

        CachedDestination(ActiveMQDestination destination) {
            this.destination = destination;
            this.timeStamp = System.currentTimeMillis();

        public int hashCode() {
            return destination.hashCode();

        public boolean equals(Object o) {
            if (o instanceof CachedDestination) {
                CachedDestination other = (CachedDestination) o;
                return other.destination.equals(this.destination);
            return false;



Other ActiveMQ examples (source code examples)

Here is a short list of links related to this ActiveMQ AbstractTempRegion.java source code file:

new blog posts


Copyright 1998-2016 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.