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() {
042    }
043
044    public TransportServerThreadSupport(URI location) {
045        super(location);
046    }
047
048    public boolean isDaemon() {
049        return daemon;
050    }
051
052    /**
053     * Sets whether the background read thread is a daemon thread or not
054     */
055    public void setDaemon(boolean daemon) {
056        this.daemon = daemon;
057    }
058
059    public boolean isJoinOnStop() {
060        return joinOnStop;
061    }
062
063    /**
064     * Sets whether the background read thread is joined with (waited for) on a
065     * stop
066     */
067    public void setJoinOnStop(boolean joinOnStop) {
068        this.joinOnStop = joinOnStop;
069    }
070
071    protected void doStart() throws Exception {
072        LOG.info("Listening for connections at: " + getConnectURI());
073        runner = new Thread(null, this, "ActiveMQ Transport Server: " + toString(), stackSize);
074        runner.setDaemon(daemon);
075        runner.setPriority(ThreadPriorities.BROKER_MANAGEMENT);
076        runner.start();
077    }
078
079    protected void doStop(ServiceStopper stopper) throws Exception {
080        if (runner != null && joinOnStop) {
081            runner.join();
082            runner = null;
083        }
084    }
085
086    /**
087     * @return the stackSize
088     */
089    public long getStackSize() {
090        return this.stackSize;
091    }
092
093    /**
094     * @param stackSize the stackSize to set
095     */
096    public void setStackSize(long stackSize) {
097        this.stackSize = stackSize;
098    }
099}