001/*
002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     bstefanescu
018 */
019package org.nuxeo.ecm.automation.client.jaxrs.spi;
020
021import java.util.concurrent.ExecutorService;
022import java.util.concurrent.Executors;
023import java.util.concurrent.ThreadFactory;
024import java.util.concurrent.TimeUnit;
025
026import org.apache.commons.logging.Log;
027import org.apache.commons.logging.LogFactory;
028
029/**
030 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
031 */
032public abstract class AsyncAutomationClient extends AbstractAutomationClient {
033
034    private static final Log log = LogFactory.getLog(AsyncAutomationClient.class);
035
036    protected ExecutorService async;
037
038    /**
039     * Timeout in milliseconds for the wait of the asynchronous thread pool termination. Default value: 2 seconds.
040     */
041    protected long asyncAwaitTerminationTimeout = 2000;
042
043    /**
044     * Instantiates a new asynchronous automation client with the default timeout for the wait of the asynchronous
045     * thread pool termination: 2 seconds.
046     */
047    public AsyncAutomationClient(String url) {
048        this(url, Executors.newCachedThreadPool(new ThreadFactory() {
049            public Thread newThread(Runnable r) {
050                return new Thread("AutomationAsyncExecutor");
051            }
052        }));
053    }
054
055    /**
056     * Instantiates a new asynchronous automation client with the given asynchronous executor and the default timeout
057     * for the wait of the asynchronous thread pool termination: 2 seconds.
058     */
059    public AsyncAutomationClient(String url, ExecutorService executor) {
060        super(url);
061        async = executor;
062    }
063
064    /**
065     * Instantiates a new asynchronous automation client with the given timeout in milliseconds for the wait of the
066     * asynchronous thread pool termination.
067     *
068     * @since 5.7
069     */
070    public AsyncAutomationClient(String url, long asyncAwaitTerminationTimeout) {
071        this(url);
072        this.asyncAwaitTerminationTimeout = asyncAwaitTerminationTimeout;
073    }
074
075    /**
076     * Instantiates a new asynchronous automation client with the given asynchronous executor and the given timeout in
077     * milliseconds for the wait of the asynchronous thread pool termination.
078     *
079     * @since 5.7
080     */
081    public AsyncAutomationClient(String url, ExecutorService executor, long asyncAwaitTerminationTimeout) {
082        this(url, executor);
083        this.asyncAwaitTerminationTimeout = asyncAwaitTerminationTimeout;
084    }
085
086    @Override
087    public void asyncExec(Runnable runnable) {
088        async.execute(runnable);
089    }
090
091    @Override
092    public synchronized void shutdown() {
093        try {
094            async.awaitTermination(asyncAwaitTerminationTimeout, TimeUnit.MILLISECONDS);
095        } catch (InterruptedException e) {
096            log.error(e, e);
097        }
098        super.shutdown();
099        async = null;
100    }
101
102}