📄 abstractioservice.java
字号:
/* * 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, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. * */package org.apache.mina.core.service;import java.util.AbstractSet;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Set;import java.util.concurrent.Executor;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.TimeUnit;import java.util.concurrent.atomic.AtomicInteger;import org.apache.mina.core.IoUtil;import org.apache.mina.core.filterchain.DefaultIoFilterChain;import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder;import org.apache.mina.core.filterchain.IoFilterChainBuilder;import org.apache.mina.core.future.ConnectFuture;import org.apache.mina.core.future.DefaultIoFuture;import org.apache.mina.core.future.IoFuture;import org.apache.mina.core.future.WriteFuture;import org.apache.mina.core.session.AbstractIoSession;import org.apache.mina.core.session.DefaultIoSessionDataStructureFactory;import org.apache.mina.core.session.IdleStatus;import org.apache.mina.core.session.IoSession;import org.apache.mina.core.session.IoSessionConfig;import org.apache.mina.core.session.IoSessionDataStructureFactory;import org.apache.mina.core.session.IoSessionInitializationException;import org.apache.mina.core.session.IoSessionInitializer;import org.apache.mina.util.ExceptionMonitor;import org.apache.mina.util.NamePreservingRunnable;/** * Base implementation of {@link IoService}s. * * An instance of IoService contains an Executor which will handle the incoming * events. * * @author The Apache MINA Project (dev@mina.apache.org) * @version $Rev: 751504 $, $Date: 2009-03-08 20:24:58 +0100 (Sun, 08 Mar 2009) $ */public abstract class AbstractIoService implements IoService { /** * The unique number identifying the Service. It's incremented * for each new IoService created. */ private static final AtomicInteger id = new AtomicInteger(); /** * The thread name built from the IoService inherited * instance class name and the IoService Id **/ private final String threadName; /** * The associated executor, responsible for handling execution of I/O events. */ private final Executor executor; /** * A flag used to indicate that the local executor has been created * inside this instance, and not passed by a caller. * * If the executor is locally created, then it will be an instance * of the ThreadPoolExecutor class. */ private final boolean createdExecutor; /** * The IoHandler in charge of managing all the I/O Events. It is */ private IoHandler handler; /** * The default {@link IoSessionConfig} which will be used to configure new sessions. */ private final IoSessionConfig sessionConfig; private final IoServiceListener serviceActivationListener = new IoServiceListener() { public void serviceActivated(IoService service) { // Update lastIoTime. AbstractIoService s = (AbstractIoService) service; IoServiceStatistics _stats = (IoServiceStatistics) s.getStatistics(); _stats.setLastReadTime(s.getActivationTime()); _stats.setLastWriteTime(s.getActivationTime()); _stats.setLastThroughputCalculationTime(s.getActivationTime()); } public void serviceDeactivated(IoService service) { } public void serviceIdle(IoService service, IdleStatus idleStatus) { } public void sessionCreated(IoSession session) { } public void sessionDestroyed(IoSession session) { } }; /** * Current filter chain builder. */ private IoFilterChainBuilder filterChainBuilder = new DefaultIoFilterChainBuilder(); private IoSessionDataStructureFactory sessionDataStructureFactory = new DefaultIoSessionDataStructureFactory(); /** * Maintains the {@link IoServiceListener}s of this service. */ private final IoServiceListenerSupport listeners; /** * A lock object which must be acquired when related resources are * destroyed. */ protected final Object disposalLock = new Object(); private volatile boolean disposing; private volatile boolean disposed; private IoFuture disposalFuture; /** * {@inheritDoc} */ private IoServiceStatistics stats = new IoServiceStatistics(this); /** * Constructor for {@link AbstractIoService}. You need to provide a default * session configuration and an {@link Executor} for handling I/O events. If * a null {@link Executor} is provided, a default one will be created using * {@link Executors#newCachedThreadPool()}. * * @param sessionConfig * the default configuration for the managed {@link IoSession} * @param executor * the {@link Executor} used for handling execution of I/O * events. Can be <code>null</code>. */ protected AbstractIoService(IoSessionConfig sessionConfig, Executor executor) { if (sessionConfig == null) { throw new NullPointerException("sessionConfig"); } if (getTransportMetadata() == null) { throw new NullPointerException("TransportMetadata"); } if (!getTransportMetadata().getSessionConfigType().isAssignableFrom( sessionConfig.getClass())) { throw new IllegalArgumentException("sessionConfig type: " + sessionConfig.getClass() + " (expected: " + getTransportMetadata().getSessionConfigType() + ")"); } // Create the listeners, and add a first listener : a activation listener // for this service, which will give information on the service state. listeners = new IoServiceListenerSupport(this); listeners.add(serviceActivationListener); // Stores the given session configuration this.sessionConfig = sessionConfig; // Make JVM load the exception monitor before some transports // change the thread context class loader. ExceptionMonitor.getInstance(); if (executor == null) { this.executor = Executors.newCachedThreadPool(); createdExecutor = true; } else { this.executor = executor; createdExecutor = false; } threadName = getClass().getSimpleName() + '-' + id.incrementAndGet(); } /** * {@inheritDoc} */ public final IoFilterChainBuilder getFilterChainBuilder() { return filterChainBuilder; } /** * {@inheritDoc} */ public final void setFilterChainBuilder(IoFilterChainBuilder builder) { if (builder == null) { builder = new DefaultIoFilterChainBuilder(); } filterChainBuilder = builder; } /** * {@inheritDoc} */ public final DefaultIoFilterChainBuilder getFilterChain() { if (filterChainBuilder instanceof DefaultIoFilterChainBuilder) { return (DefaultIoFilterChainBuilder) filterChainBuilder; } else { throw new IllegalStateException( "Current filter chain builder is not a DefaultIoFilterChainBuilder."); } } /** * {@inheritDoc} */ public final void addListener(IoServiceListener listener) { listeners.add(listener); } /** * {@inheritDoc} */ public final void removeListener(IoServiceListener listener) { listeners.remove(listener); } /** * {@inheritDoc} */ public final boolean isActive() { return listeners.isActive(); } /** * {@inheritDoc} */ public final boolean isDisposing() { return disposing; } /** * {@inheritDoc} */ public final boolean isDisposed() { return disposed; } /** * {@inheritDoc} */ public final void dispose() { if (disposed) { return;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -