⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 abstractioservice.java

📁 mina是以Java实现的一个开源的网络程序框架
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        }        IoFuture disposalFuture;        synchronized (disposalLock) {            disposalFuture = this.disposalFuture;            if (!disposing) {                disposing = true;                try {                    this.disposalFuture = disposalFuture = dispose0();                } catch (Exception e) {                    ExceptionMonitor.getInstance().exceptionCaught(e);                } finally {                    if (disposalFuture == null) {                        disposed = true;                    }                }            }        }                if (disposalFuture != null) {            disposalFuture.awaitUninterruptibly();        }        if (createdExecutor) {            ExecutorService e = (ExecutorService) executor;            e.shutdown();            while (!e.isTerminated()) {                try {                    e.awaitTermination(Integer.MAX_VALUE, TimeUnit.SECONDS);                } catch (InterruptedException e1) {                    // Ignore; it should end shortly.                }            }        }        disposed = true;    }    /**     * Implement this method to release any acquired resources.  This method     * is invoked only once by {@link #dispose()}.     */    protected abstract IoFuture dispose0() throws Exception;    /**     * {@inheritDoc}     */    public final Map<Long, IoSession> getManagedSessions() {        return listeners.getManagedSessions();    }    /**     * {@inheritDoc}     */    public final int getManagedSessionCount() {        return listeners.getManagedSessionCount();    }    /**     * {@inheritDoc}     */    public final IoHandler getHandler() {        return handler;    }    /**     * {@inheritDoc}     */    public final void setHandler(IoHandler handler) {        if (handler == null) {            throw new NullPointerException("handler cannot be null");        }        if (isActive()) {            throw new IllegalStateException(                    "handler cannot be set while the service is active.");        }        this.handler = handler;    }    /**     * {@inheritDoc}     */    public IoSessionConfig getSessionConfig() {        return sessionConfig;    }    /**     * {@inheritDoc}     */    public final IoSessionDataStructureFactory getSessionDataStructureFactory() {        return sessionDataStructureFactory;    }    /**     * {@inheritDoc}     */    public final void setSessionDataStructureFactory(            IoSessionDataStructureFactory sessionDataStructureFactory) {        if (sessionDataStructureFactory == null) {            throw new NullPointerException("sessionDataStructureFactory");        }        if (isActive()) {            throw new IllegalStateException(                    "sessionDataStructureFactory cannot be set while the service is active.");        }        this.sessionDataStructureFactory = sessionDataStructureFactory;    }    /**     * {@inheritDoc}     */    public IoServiceStatistics getStatistics() {        return stats;    }    /**     * {@inheritDoc}     */    public final long getActivationTime() {        return listeners.getActivationTime();    }    /**     * {@inheritDoc}     */    public final Set<WriteFuture> broadcast(Object message) {        // Convert to Set.  We do not return a List here because only the        // direct caller of MessageBroadcaster knows the order of write        // operations.        final List<WriteFuture> futures = IoUtil.broadcast(message,                getManagedSessions().values());        return new AbstractSet<WriteFuture>() {            @Override            public Iterator<WriteFuture> iterator() {                return futures.iterator();            }            @Override            public int size() {                return futures.size();            }        };    }    public final IoServiceListenerSupport getListeners() {        return listeners;    }    protected final void executeWorker(Runnable worker) {        executeWorker(worker, null);    }    protected final void executeWorker(Runnable worker, String suffix) {        String actualThreadName = threadName;        if (suffix != null) {            actualThreadName = actualThreadName + '-' + suffix;        }        executor.execute(new NamePreservingRunnable(worker, actualThreadName));    }    // TODO Figure out make it work without causing a compiler error / warning.    @SuppressWarnings("unchecked")    protected final void initSession(IoSession session,            IoFuture future, IoSessionInitializer sessionInitializer) {        // Update lastIoTime if needed.        if (stats.getLastReadTime() == 0) {            ((IoServiceStatistics)stats).setLastReadTime(getActivationTime());        }                if (stats.getLastWriteTime() == 0) {            ((IoServiceStatistics)stats).setLastWriteTime(getActivationTime());        }        // Every property but attributeMap should be set now.        // Now initialize the attributeMap.  The reason why we initialize        // the attributeMap at last is to make sure all session properties        // such as remoteAddress are provided to IoSessionDataStructureFactory.        try {            ((AbstractIoSession) session).setAttributeMap(session.getService()                    .getSessionDataStructureFactory().getAttributeMap(session));        } catch (IoSessionInitializationException e) {            throw e;        } catch (Exception e) {            throw new IoSessionInitializationException(                    "Failed to initialize an attributeMap.", e);        }        try {            ((AbstractIoSession) session).setWriteRequestQueue(session                    .getService().getSessionDataStructureFactory()                    .getWriteRequestQueue(session));        } catch (IoSessionInitializationException e) {            throw e;        } catch (Exception e) {            throw new IoSessionInitializationException(                    "Failed to initialize a writeRequestQueue.", e);        }        if ((future != null) && (future instanceof ConnectFuture)) {            // DefaultIoFilterChain will notify the future. (We support ConnectFuture only for now).            session.setAttribute(DefaultIoFilterChain.SESSION_CREATED_FUTURE,                    future);        }        if (sessionInitializer != null) {            sessionInitializer.initializeSession(session, future);        }        finishSessionInitialization0(session, future);    }    /**     * Implement this method to perform additional tasks required for session     * initialization. Do not call this method directly;     * {@link #finishSessionInitialization(IoSession, IoFuture, IoSessionInitializer)} will call     * this method instead.     */    protected void finishSessionInitialization0(IoSession session,            IoFuture future) {    }    protected static class ServiceOperationFuture extends DefaultIoFuture {        public ServiceOperationFuture() {            super(null);        }        public final boolean isDone() {            return getValue() == Boolean.TRUE;        }        public final void setDone() {            setValue(Boolean.TRUE);        }        public final Exception getException() {            if (getValue() instanceof Exception) {                return (Exception) getValue();            } else {                return null;            }        }        public final void setException(Exception exception) {            if (exception == null) {                throw new NullPointerException("exception");            }            setValue(exception);        }    }    /**     * {@inheritDoc}     */    public int getScheduledWriteBytes() {        return stats.getScheduledWriteBytes();    }    /**     * {@inheritDoc}     */    public int getScheduledWriteMessages() {        return stats.getScheduledWriteMessages();    }    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -