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 */
017package org.apache.activemq.transport;
018
019import java.net.URI;
020
021import org.apache.activemq.ThreadPriorities;
022import org.apache.activemq.util.ServiceStopper;
023import org.slf4j.Logger;
024import org.slf4j.LoggerFactory;
025
026/**
027 * A useful base class for implementations of {@link TransportServer} which uses
028 * a background thread to accept new connections.
029 * 
030 * 
031 */
032public abstract class TransportServerThreadSupport extends TransportServerSupport implements Runnable {
033    private static final Logger LOG = LoggerFactory.getLogger(TransportServerThreadSupport.class);
034
035    private boolean daemon = true;
036    private boolean joinOnStop = true;
037    private Thread runner;
038    // should be a multiple of 128k
039    private long stackSize;
040
041    public TransportServerThreadSupport(URI location) {
042        super(location);
043    }
044
045    public boolean isDaemon() {
046        return daemon;
047    }
048
049    /**
050     * Sets whether the background read thread is a daemon thread or not
051     */
052    public void setDaemon(boolean daemon) {
053        this.daemon = daemon;
054    }
055
056    public boolean isJoinOnStop() {
057        return joinOnStop;
058    }
059
060    /**
061     * Sets whether the background read thread is joined with (waited for) on a
062     * stop
063     */
064    public void setJoinOnStop(boolean joinOnStop) {
065        this.joinOnStop = joinOnStop;
066    }
067
068    protected void doStart() throws Exception {
069        LOG.info("Listening for connections at: " + getConnectURI());
070        runner = new Thread(null, this, "ActiveMQ Transport Server: " + toString(), stackSize);
071        runner.setDaemon(daemon);
072        runner.setPriority(ThreadPriorities.BROKER_MANAGEMENT);
073        runner.start();
074    }
075
076    protected void doStop(ServiceStopper stopper) throws Exception {
077        if (runner != null && joinOnStop) {
078            runner.join();
079            runner = null;
080        }
081    }
082
083    /**
084     * @return the stackSize
085     */
086    public long getStackSize() {
087        return this.stackSize;
088    }
089
090    /**
091     * @param stackSize the stackSize to set
092     */
093    public void setStackSize(long stackSize) {
094        this.stackSize = stackSize;
095    }
096}