Search in sources :

Example 41 with Executor

use of java.util.concurrent.Executor in project robovm by robovm.

the class Linker method generateMachineCode.

private void generateMachineCode(final Config config, ModuleBuilder[] mbs, final List<File> objectFiles) throws IOException {
    /*
         * Make sure the tmpDir exists before we launch the worker threads. This
         * is to prevent a race between threads to create this folder. See #631.
         */
    config.getTmpDir().mkdirs();
    Executor executor = config.getThreads() <= 1 ? AppCompiler.SAME_THREAD_EXECUTOR : Executors.newFixedThreadPool(config.getThreads());
    final List<Throwable> errors = Collections.synchronizedList(new ArrayList<Throwable>());
    for (int i = 0; i < mbs.length; i++) {
        final ModuleBuilder mb = mbs[i];
        final int num = i;
        executor.execute(new Runnable() {

            public void run() {
                try {
                    File linkerO = generateMachineCode(config, mb, num);
                    synchronized (objectFiles) {
                        objectFiles.add(linkerO);
                    }
                } catch (Throwable t) {
                    errors.add(t);
                }
            }
        });
    }
    // Shutdown the executor and wait for running tasks to complete.
    if (executor instanceof ExecutorService) {
        ExecutorService executorService = (ExecutorService) executor;
        executorService.shutdown();
        try {
            executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);
        } catch (InterruptedException e) {
        }
    }
    if (!errors.isEmpty()) {
        Throwable t = errors.get(0);
        if (t instanceof IOException) {
            throw (IOException) t;
        }
        if (t instanceof RuntimeException) {
            throw (RuntimeException) t;
        }
        throw new CompilerException(t);
    }
}
Also used : IOException(java.io.IOException) Executor(java.util.concurrent.Executor) ExecutorService(java.util.concurrent.ExecutorService) File(java.io.File)

Example 42 with Executor

use of java.util.concurrent.Executor in project jetty.project by eclipse.

the class HttpClientTransportOverHTTP2Test method testExternalServer.

@Ignore
@Test
public void testExternalServer() throws Exception {
    HTTP2Client http2Client = new HTTP2Client();
    SslContextFactory sslContextFactory = new SslContextFactory();
    HttpClient httpClient = new HttpClient(new HttpClientTransportOverHTTP2(http2Client), sslContextFactory);
    Executor executor = new QueuedThreadPool();
    httpClient.setExecutor(executor);
    httpClient.start();
    //        ContentResponse response = httpClient.GET("https://http2.akamai.com/");
    ContentResponse response = httpClient.GET("https://webtide.com/");
    Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
    httpClient.stop();
}
Also used : SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) Executor(java.util.concurrent.Executor) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpClient(org.eclipse.jetty.client.HttpClient) HTTP2Client(org.eclipse.jetty.http2.client.HTTP2Client) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 43 with Executor

use of java.util.concurrent.Executor in project jetty.project by eclipse.

the class ProxyServlet method onContinue.

@Override
protected void onContinue(HttpServletRequest clientRequest, Request proxyRequest) {
    super.onContinue(clientRequest, proxyRequest);
    Runnable action = (Runnable) proxyRequest.getAttributes().get(CONTINUE_ACTION_ATTRIBUTE);
    Executor executor = getHttpClient().getExecutor();
    executor.execute(action);
}
Also used : Executor(java.util.concurrent.Executor)

Example 44 with Executor

use of java.util.concurrent.Executor in project elasticsearch by elastic.

the class MockTcpTransport method connectToChannels.

@Override
protected NodeChannels connectToChannels(DiscoveryNode node, ConnectionProfile profile) throws IOException {
    final MockChannel[] mockChannels = new MockChannel[1];
    // we always use light here
    final NodeChannels nodeChannels = new NodeChannels(node, mockChannels, LIGHT_PROFILE);
    boolean success = false;
    final MockSocket socket = new MockSocket();
    try {
        Consumer<MockChannel> onClose = (channel) -> {
            final NodeChannels connected = connectedNodes.get(node);
            if (connected != null && connected.hasChannel(channel)) {
                try {
                    executor.execute(() -> {
                        disconnectFromNode(node, channel, "channel closed event");
                    });
                } catch (RejectedExecutionException ex) {
                    logger.debug("failed to run disconnectFromNode - node is shutting down");
                }
            }
        };
        final InetSocketAddress address = node.getAddress().address();
        // we just use a single connections
        configureSocket(socket);
        final TimeValue connectTimeout = profile.getConnectTimeout();
        try {
            socket.connect(address, Math.toIntExact(connectTimeout.millis()));
        } catch (SocketTimeoutException ex) {
            throw new ConnectTransportException(node, "connect_timeout[" + connectTimeout + "]", ex);
        }
        MockChannel channel = new MockChannel(socket, address, "none", onClose);
        channel.loopRead(executor);
        mockChannels[0] = channel;
        success = true;
    } finally {
        if (success == false) {
            IOUtils.close(nodeChannels, socket);
        }
    }
    return nodeChannels;
}
Also used : CancellableThreads(org.elasticsearch.common.util.CancellableThreads) Socket(java.net.Socket) BufferedInputStream(java.io.BufferedInputStream) BigArrays(org.elasticsearch.common.util.BigArrays) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) CircuitBreakerService(org.elasticsearch.indices.breaker.CircuitBreakerService) BufferedOutputStream(java.io.BufferedOutputStream) ServerSocket(java.net.ServerSocket) HashSet(java.util.HashSet) InputStreamStreamInput(org.elasticsearch.common.io.stream.InputStreamStreamInput) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) NetworkService(org.elasticsearch.common.network.NetworkService) SocketException(java.net.SocketException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) Settings(org.elasticsearch.common.settings.Settings) NamedWriteableRegistry(org.elasticsearch.common.io.stream.NamedWriteableRegistry) TimeValue(org.elasticsearch.common.unit.TimeValue) SocketTimeoutException(java.net.SocketTimeoutException) Map(java.util.Map) ThreadPool(org.elasticsearch.threadpool.ThreadPool) ExecutorService(java.util.concurrent.ExecutorService) ByteSizeValue(org.elasticsearch.common.unit.ByteSizeValue) OutputStream(java.io.OutputStream) EsExecutors(org.elasticsearch.common.util.concurrent.EsExecutors) MockSocket(org.elasticsearch.mocksocket.MockSocket) Executor(java.util.concurrent.Executor) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) IOUtils(org.apache.lucene.util.IOUtils) Set(java.util.Set) IOException(java.io.IOException) BytesReference(org.elasticsearch.common.bytes.BytesReference) InetSocketAddress(java.net.InetSocketAddress) Executors(java.util.concurrent.Executors) MockServerSocket(org.elasticsearch.mocksocket.MockServerSocket) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) CountDownLatch(java.util.concurrent.CountDownLatch) AbstractRunnable(org.elasticsearch.common.util.concurrent.AbstractRunnable) List(java.util.List) Version(org.elasticsearch.Version) StreamInput(org.elasticsearch.common.io.stream.StreamInput) Closeable(java.io.Closeable) Collections(java.util.Collections) MockSocket(org.elasticsearch.mocksocket.MockSocket) SocketTimeoutException(java.net.SocketTimeoutException) InetSocketAddress(java.net.InetSocketAddress) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) TimeValue(org.elasticsearch.common.unit.TimeValue)

Example 45 with Executor

use of java.util.concurrent.Executor in project jetty.project by eclipse.

the class HTTP2ClientConnectionFactory method newConnection.

@Override
public Connection newConnection(EndPoint endPoint, Map<String, Object> context) throws IOException {
    HTTP2Client client = (HTTP2Client) context.get(CLIENT_CONTEXT_KEY);
    ByteBufferPool byteBufferPool = (ByteBufferPool) context.get(BYTE_BUFFER_POOL_CONTEXT_KEY);
    Executor executor = (Executor) context.get(EXECUTOR_CONTEXT_KEY);
    Scheduler scheduler = (Scheduler) context.get(SCHEDULER_CONTEXT_KEY);
    Session.Listener listener = (Session.Listener) context.get(SESSION_LISTENER_CONTEXT_KEY);
    @SuppressWarnings("unchecked") Promise<Session> promise = (Promise<Session>) context.get(SESSION_PROMISE_CONTEXT_KEY);
    Generator generator = new Generator(byteBufferPool);
    FlowControlStrategy flowControl = client.getFlowControlStrategyFactory().newFlowControlStrategy();
    HTTP2ClientSession session = new HTTP2ClientSession(scheduler, endPoint, generator, listener, flowControl);
    Parser parser = new Parser(byteBufferPool, session, 4096, 8192);
    HTTP2ClientConnection connection = new HTTP2ClientConnection(client, byteBufferPool, executor, endPoint, parser, session, client.getInputBufferSize(), promise, listener);
    connection.addListener(connectionListener);
    return customize(connection, context);
}
Also used : ByteBufferPool(org.eclipse.jetty.io.ByteBufferPool) Scheduler(org.eclipse.jetty.util.thread.Scheduler) Parser(org.eclipse.jetty.http2.parser.Parser) FlowControlStrategy(org.eclipse.jetty.http2.FlowControlStrategy) Promise(org.eclipse.jetty.util.Promise) Executor(java.util.concurrent.Executor) Session(org.eclipse.jetty.http2.api.Session) ISession(org.eclipse.jetty.http2.ISession) Generator(org.eclipse.jetty.http2.generator.Generator)

Aggregations

Executor (java.util.concurrent.Executor)302 Test (org.junit.Test)127 ArrayList (java.util.ArrayList)35 CountDownLatch (java.util.concurrent.CountDownLatch)32 IOException (java.io.IOException)29 List (java.util.List)27 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)22 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)17 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)16 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)16 Map (java.util.Map)15 Timeouts (org.neo4j.cluster.timeout.Timeouts)15 ExecutorService (java.util.concurrent.ExecutorService)13 InstanceId (org.neo4j.cluster.InstanceId)13 Config (org.neo4j.kernel.configuration.Config)13 File (java.io.File)12 ObjectInputStreamFactory (org.neo4j.cluster.protocol.atomicbroadcast.ObjectInputStreamFactory)12 ObjectOutputStreamFactory (org.neo4j.cluster.protocol.atomicbroadcast.ObjectOutputStreamFactory)12 HeartbeatContext (org.neo4j.cluster.protocol.heartbeat.HeartbeatContext)12 InetSocketAddress (java.net.InetSocketAddress)10