com.zonekey.ssm.common.log.queue.QueuesHolder.java Source code

Java tutorial

Introduction

Here is the source code for com.zonekey.ssm.common.log.queue.QueuesHolder.java

Source

/**
 * Copyright (c) 2005-2009 springside.org.cn
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * 
 * $Id: QueuesHolder.java 1192 2010-09-03 15:42:13Z calvinxiu $
 */
package com.zonekey.ssm.common.log.queue;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.LinkedBlockingQueue;

import org.springframework.jmx.export.annotation.ManagedOperation;
import org.springframework.jmx.export.annotation.ManagedOperationParameter;
import org.springframework.jmx.export.annotation.ManagedOperationParameters;
import org.springframework.jmx.export.annotation.ManagedResource;

import com.google.common.collect.MapMaker;

/**
 * BlockingQueue Map?.
 * 
 * @author calvin
 */
@SuppressWarnings("unchecked")
@ManagedResource(objectName = QueuesHolder.QUEUEHOLDER_MBEAN_NAME, description = "Queues Holder Bean")
public class QueuesHolder {
    /**
     * QueueManager??.
     */
    public static final String QUEUEHOLDER_MBEAN_NAME = "Disrec:type=QueueManagement,name=queueHolder";

    private static ConcurrentMap<String, BlockingQueue> queueMap = new MapMaker().concurrencyLevel(32).makeMap();//?

    private static int queueSize = Integer.MAX_VALUE;

    /**
     * ?queueName???.
     * ??, .
     */
    public static <T> BlockingQueue<T> getQueue(String queueName) {
        BlockingQueue queue = queueMap.get(queueName);

        if (queue == null) {
            BlockingQueue newQueue = new LinkedBlockingQueue(queueSize);

            //???,Null.??.
            queue = queueMap.putIfAbsent(queueName, newQueue);
            if (queue == null) {
                queue = newQueue;
            }
        }
        return queue;
    }

    /**
     * ?queueName????,?JMX.
     */
    @ManagedOperation(description = "Get message count in queue")
    @ManagedOperationParameters({ @ManagedOperationParameter(name = "queueName", description = "Queue name") })
    public static int getQueueLength(String queueName) {
        return getQueue(queueName).size();
    }

    /**
     * ?, Integer, ??.
     */
    public void setQueueSize(int queueSize) {
        QueuesHolder.queueSize = queueSize; //NOSONAR
    }
}