View Javadoc
1   /*
2    * Copyright 2012 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package org.jboss.netty.channel;
17  
18  import java.util.concurrent.Executor;
19  
20  import org.jboss.netty.channel.group.ChannelGroup;
21  import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
22  import org.jboss.netty.util.ExternalResourceReleasable;
23  
24  
25  /**
26   * The main interface to a transport that creates a {@link Channel} associated
27   * with a certain communication entity such as a network socket.  For example,
28   * the {@link NioServerSocketChannelFactory} creates a channel which has a
29   * NIO-based server socket as its underlying communication entity.
30   * <p>
31   * Once a new {@link Channel} is created, the {@link ChannelPipeline} which
32   * was specified as a parameter in the {@link #newChannel(ChannelPipeline)}
33   * is attached to the new {@link Channel}, and starts to handle all associated
34   * {@link ChannelEvent}s.
35   *
36   * <h3>Graceful shutdown</h3>
37   * <p>
38   * To shut down a network application service which is managed by a factory.
39   * you should follow the following steps:
40   * <ol>
41   * <li>close all channels created by the factory and their child channels
42   *     usually using {@link ChannelGroup#close()}, and</li>
43   * <li>call {@link #releaseExternalResources()}.</li>
44   * </ol>
45   * <p>
46   * For detailed transport-specific information on shutting down a factory,
47   * please refer to the Javadoc of {@link ChannelFactory}'s subtypes, such as
48   * {@link NioServerSocketChannelFactory}.
49   *
50   * @apiviz.landmark
51   * @apiviz.has        org.jboss.netty.channel.Channel oneway - - creates
52   *
53   * @apiviz.exclude ^org\.jboss\.netty\.channel\.([a-z]+\.)+.*ChannelFactory$
54   */
55  public interface ChannelFactory extends ExternalResourceReleasable {
56  
57      /**
58       * Creates and opens a new {@link Channel} and attaches the specified
59       * {@link ChannelPipeline} to the new {@link Channel}.
60       *
61       * @param pipeline the {@link ChannelPipeline} which is going to be
62       *                 attached to the new {@link Channel}
63       *
64       * @return the newly open channel
65       *
66       * @throws ChannelException if failed to create and open a new channel
67       */
68      Channel newChannel(ChannelPipeline pipeline);
69  
70      /**
71       * Shudown the ChannelFactory and all the resource it created internal.
72       */
73      void shutdown();
74  
75      /**
76       * Releases the external resources that this factory depends on to function.
77       * An external resource is a resource that this factory didn't create by
78       * itself.  For example, {@link Executor}s that you specified in the factory
79       * constructor are external resources.  You can call this method to release
80       * all external resources conveniently when the resources are not used by
81       * this factory or any other part of your application.  An unexpected
82       * behavior will be resulted in if the resources are released when there's
83       * an open channel which is managed by this factory.
84       *
85       * This will also call {@link #shutdown()} before do any action
86       */
87      void releaseExternalResources();
88  }