View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.hc.core5.reactor;
29  
30  import java.io.IOException;
31  import java.util.concurrent.ThreadFactory;
32  
33  import org.apache.hc.core5.concurrent.DefaultThreadFactory;
34  import org.apache.hc.core5.function.Callback;
35  import org.apache.hc.core5.function.Decorator;
36  import org.apache.hc.core5.io.CloseMode;
37  import org.apache.hc.core5.util.Args;
38  import org.apache.hc.core5.util.TimeValue;
39  
40  /**
41   * Multi-core I/O reactor that can act as {@link ConnectionInitiator} Internally
42   * this I/O reactor distributes newly created I/O session equally across multiple
43   * I/O worker threads for a more optimal resource utilization and a better
44   * I/O performance. Usually it is recommended to have one worker I/O reactor
45   * per physical CPU core.
46   *
47   * @since 4.0
48   */
49  public class DefaultConnectingIOReactor extends AbstractIOReactorBase {
50  
51      private final int workerCount;
52      private final SingleCoreIOReactor[] workers;
53      private final MultiCoreIOReactor ioReactor;
54      private final IOWorkers.Selector workerSelector;
55  
56      private final static ThreadFactory THREAD_FACTORY = new DefaultThreadFactory("I/O client dispatch", true);
57  
58      public DefaultConnectingIOReactor(
59              final IOEventHandlerFactory eventHandlerFactory,
60              final IOReactorConfig ioReactorConfig,
61              final ThreadFactory threadFactory,
62              final Decorator<IOSession> ioSessionDecorator,
63              final Callback<Exception> exceptionCallback,
64              final IOSessionListener sessionListener,
65              final Callback<IOSession> sessionShutdownCallback) {
66          Args.notNull(eventHandlerFactory, "Event handler factory");
67          this.workerCount = ioReactorConfig != null ? ioReactorConfig.getIoThreadCount() : IOReactorConfig.DEFAULT.getIoThreadCount();
68          this.workers = new SingleCoreIOReactor[workerCount];
69          final Thread[] threads = new Thread[workerCount];
70          for (int i = 0; i < this.workers.length; i++) {
71              final SingleCoreIOReactoractor.html#SingleCoreIOReactor">SingleCoreIOReactor dispatcher = new SingleCoreIOReactor(
72                      exceptionCallback,
73                      eventHandlerFactory,
74                      ioReactorConfig != null ? ioReactorConfig : IOReactorConfig.DEFAULT,
75                      ioSessionDecorator,
76                      sessionListener,
77                      sessionShutdownCallback);
78              this.workers[i] = dispatcher;
79              threads[i] = (threadFactory != null ? threadFactory : THREAD_FACTORY).newThread(new IOReactorWorker(dispatcher));
80          }
81          this.ioReactor = new MultiCoreIOReactor(this.workers, threads);
82          this.workerSelector =  IOWorkers.newSelector(workers);
83      }
84  
85      public DefaultConnectingIOReactor(
86              final IOEventHandlerFactory eventHandlerFactory,
87              final IOReactorConfig config,
88              final Callback<IOSession> sessionShutdownCallback) {
89          this(eventHandlerFactory, config, null, null, null, null, sessionShutdownCallback);
90      }
91  
92      /**
93       * Creates an instance of DefaultConnectingIOReactor with default configuration.
94       *
95       * @since 5.0
96       */
97      public DefaultConnectingIOReactor(final IOEventHandlerFactory eventHandlerFactory) {
98          this(eventHandlerFactory, null, null);
99      }
100 
101     @Override
102     public void start() {
103         ioReactor.start();
104     }
105 
106     @Override
107     public IOReactorStatus getStatus() {
108         return ioReactor.getStatus();
109     }
110 
111     @Override
112     IOWorkers.Selector getWorkerSelector() {
113         return workerSelector;
114     }
115 
116     @Override
117     public void initiateShutdown() {
118         ioReactor.initiateShutdown();
119     }
120 
121     @Override
122     public void awaitShutdown(final TimeValue waitTime) throws InterruptedException {
123         ioReactor.awaitShutdown(waitTime);
124     }
125 
126     @Override
127     public void close(final CloseMode closeMode) {
128         ioReactor.close(closeMode);
129     }
130 
131     @Override
132     public void close() throws IOException {
133         ioReactor.close();
134     }
135 
136 }