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.ArrayList;
32  import java.util.Collections;
33  import java.util.Deque;
34  import java.util.List;
35  import java.util.concurrent.ConcurrentLinkedDeque;
36  import java.util.concurrent.ThreadFactory;
37  
38  import org.apache.hc.core5.concurrent.DefaultThreadFactory;
39  import org.apache.hc.core5.function.Callback;
40  import org.apache.hc.core5.function.Decorator;
41  import org.apache.hc.core5.io.CloseMode;
42  import org.apache.hc.core5.util.Args;
43  import org.apache.hc.core5.util.TimeValue;
44  
45  /**
46   * Multi-core I/O reactor that can act as {@link ConnectionInitiator} Internally
47   * this I/O reactor distributes newly created I/O session equally across multiple
48   * I/O worker threads for a more optimal resource utilization and a better
49   * I/O performance. Usually it is recommended to have one worker I/O reactor
50   * per physical CPU core.
51   *
52   * @since 4.0
53   */
54  public class DefaultConnectingIOReactor extends AbstractIOReactorBase {
55  
56      private final Deque<ExceptionEvent> auditLog;
57      private final int workerCount;
58      private final SingleCoreIOReactor[] workers;
59      private final MultiCoreIOReactor ioReactor;
60      private final IOWorkers.Selector workerSelector;
61  
62      private final static ThreadFactory THREAD_FACTORY = new DefaultThreadFactory("I/O client dispatch", true);
63  
64      public DefaultConnectingIOReactor(
65              final IOEventHandlerFactory eventHandlerFactory,
66              final IOReactorConfig ioReactorConfig,
67              final ThreadFactory threadFactory,
68              final Decorator<IOSession> ioSessionDecorator,
69              final IOSessionListener sessionListener,
70              final Callback<IOSession> sessionShutdownCallback) {
71          Args.notNull(eventHandlerFactory, "Event handler factory");
72          this.auditLog = new ConcurrentLinkedDeque<>();
73          this.workerCount = ioReactorConfig != null ? ioReactorConfig.getIoThreadCount() : IOReactorConfig.DEFAULT.getIoThreadCount();
74          this.workers = new SingleCoreIOReactor[workerCount];
75          final Thread[] threads = new Thread[workerCount];
76          for (int i = 0; i < this.workers.length; i++) {
77              final SingleCoreIOReactoractor.html#SingleCoreIOReactor">SingleCoreIOReactor dispatcher = new SingleCoreIOReactor(
78                      auditLog,
79                      eventHandlerFactory,
80                      ioReactorConfig != null ? ioReactorConfig : IOReactorConfig.DEFAULT,
81                      ioSessionDecorator,
82                      sessionListener,
83                      sessionShutdownCallback);
84              this.workers[i] = dispatcher;
85              threads[i] = (threadFactory != null ? threadFactory : THREAD_FACTORY).newThread(new IOReactorWorker(dispatcher));
86          }
87          this.ioReactor = new MultiCoreIOReactor(this.workers, threads);
88          this.workerSelector =  IOWorkers.newSelector(workers);
89      }
90  
91      public DefaultConnectingIOReactor(
92              final IOEventHandlerFactory eventHandlerFactory,
93              final IOReactorConfig config,
94              final Callback<IOSession> sessionShutdownCallback) {
95          this(eventHandlerFactory, config, null, null, null, sessionShutdownCallback);
96      }
97  
98      /**
99       * Creates an instance of DefaultConnectingIOReactor with default configuration.
100      *
101      * @since 5.0
102      */
103     public DefaultConnectingIOReactor(final IOEventHandlerFactory eventHandlerFactory) {
104         this(eventHandlerFactory, null, null);
105     }
106 
107     @Override
108     public void start() {
109         ioReactor.start();
110     }
111 
112     @Override
113     public IOReactorStatus getStatus() {
114         return ioReactor.getStatus();
115     }
116 
117     @Override
118     IOWorkers.Selector getWorkerSelector() {
119         return workerSelector;
120     }
121 
122     @Override
123     public List<ExceptionEvent> getExceptionLog() {
124         return auditLog.isEmpty() ? Collections.<ExceptionEvent>emptyList() : new ArrayList<>(auditLog);
125     }
126 
127     @Override
128     public void initiateShutdown() {
129         ioReactor.initiateShutdown();
130     }
131 
132     @Override
133     public void awaitShutdown(final TimeValue waitTime) throws InterruptedException {
134         ioReactor.awaitShutdown(waitTime);
135     }
136 
137     @Override
138     public void close(final CloseMode closeMode) {
139         ioReactor.close(closeMode);
140     }
141 
142     @Override
143     public void close() throws IOException {
144         ioReactor.close();
145     }
146 
147 }