001/* 002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the Eclipse Public License v1.0 006 * which accompanies this distribution, and is available at 007 * http://www.eclipse.org/legal/epl-v10.html 008 * 009 * Contributors: 010 * bstefanescu 011 */ 012package org.nuxeo.ecm.automation.client.jaxrs.spi; 013 014import java.util.concurrent.ExecutorService; 015import java.util.concurrent.Executors; 016import java.util.concurrent.ThreadFactory; 017import java.util.concurrent.TimeUnit; 018 019import org.apache.commons.logging.Log; 020import org.apache.commons.logging.LogFactory; 021 022/** 023 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> 024 */ 025public abstract class AsyncAutomationClient extends AbstractAutomationClient { 026 027 private static final Log log = LogFactory.getLog(AsyncAutomationClient.class); 028 029 protected ExecutorService async; 030 031 /** 032 * Timeout in milliseconds for the wait of the asynchronous thread pool termination. Default value: 2 seconds. 033 */ 034 protected long asyncAwaitTerminationTimeout = 2000; 035 036 /** 037 * Instantiates a new asynchronous automation client with the default timeout for the wait of the asynchronous 038 * thread pool termination: 2 seconds. 039 */ 040 public AsyncAutomationClient(String url) { 041 this(url, Executors.newCachedThreadPool(new ThreadFactory() { 042 public Thread newThread(Runnable r) { 043 return new Thread("AutomationAsyncExecutor"); 044 } 045 })); 046 } 047 048 /** 049 * Instantiates a new asynchronous automation client with the given asynchronous executor and the default timeout 050 * for the wait of the asynchronous thread pool termination: 2 seconds. 051 */ 052 public AsyncAutomationClient(String url, ExecutorService executor) { 053 super(url); 054 async = executor; 055 } 056 057 /** 058 * Instantiates a new asynchronous automation client with the given timeout in milliseconds for the wait of the 059 * asynchronous thread pool termination. 060 * 061 * @since 5.7 062 */ 063 public AsyncAutomationClient(String url, long asyncAwaitTerminationTimeout) { 064 this(url); 065 this.asyncAwaitTerminationTimeout = asyncAwaitTerminationTimeout; 066 } 067 068 /** 069 * Instantiates a new asynchronous automation client with the given asynchronous executor and the given timeout in 070 * milliseconds for the wait of the asynchronous thread pool termination. 071 * 072 * @since 5.7 073 */ 074 public AsyncAutomationClient(String url, ExecutorService executor, long asyncAwaitTerminationTimeout) { 075 this(url, executor); 076 this.asyncAwaitTerminationTimeout = asyncAwaitTerminationTimeout; 077 } 078 079 @Override 080 public void asyncExec(Runnable runnable) { 081 async.execute(runnable); 082 } 083 084 @Override 085 public synchronized void shutdown() { 086 try { 087 async.awaitTermination(asyncAwaitTerminationTimeout, TimeUnit.MILLISECONDS); 088 } catch (InterruptedException e) { 089 log.error(e, e); 090 } 091 super.shutdown(); 092 async = null; 093 } 094 095}