1 /* 2 * Copyright (c) 2002-2022, City of Paris 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright notice 10 * and the following disclaimer. 11 * 12 * 2. Redistributions in binary form must reproduce the above copyright notice 13 * and the following disclaimer in the documentation and/or other materials 14 * provided with the distribution. 15 * 16 * 3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its 17 * contributors may be used to endorse or promote products derived from 18 * this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE 24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 * 32 * License 1.0 33 */ 34 package fr.paris.lutece.plugins.appointment.service; 35 36 import java.util.concurrent.ExecutorService; 37 import java.util.concurrent.Executors; 38 import java.util.concurrent.Future; 39 import java.util.concurrent.RejectedExecutionException; 40 import java.util.concurrent.TimeUnit; 41 42 import org.springframework.scheduling.concurrent.CustomizableThreadFactory; 43 44 import fr.paris.lutece.portal.service.util.AppLogService; 45 import fr.paris.lutece.portal.service.util.AppPropertiesService; 46 47 public enum AppointmentExecutorService 48 { 49 INSTANCE; 50 private static final String PROPERTY_THREAD_APPOINTMENT_POOL_MAX_SIZE = "appointment.executor.thread.pool.max.size"; 51 private final ExecutorService _executorService = Executors.newFixedThreadPool( 52 AppPropertiesService.getPropertyInt( PROPERTY_THREAD_APPOINTMENT_POOL_MAX_SIZE, Runtime.getRuntime( ).availableProcessors( ) ), 53 new CustomizableThreadFactory( "Lutece-AppointmentExecutor-thread-" ) ); 54 55 /** 56 * Executes the given command at some time in the future. The command may execute in a new thread, in a pooled thread, or in the calling thread, at the 57 * discretion of the {@code Executor} implementation. 58 * 59 * @param command 60 * the runnable task 61 * @throws RejectedExecutionException 62 * if this task cannot be accepted for execution 63 * @throws NullPointerException 64 * if command is null 65 */ 66 public void execute( Runnable task ) 67 { 68 _executorService.execute( task ); 69 } 70 71 /** 72 * Submits a Runnable task for execution and returns a Future representing that task. The Future's ge} method will return null upon <em>successful</em> 73 * completion. 74 * 75 * @param task 76 * the task to submit 77 * @return a Future representing pending completion of the task 78 * @throws RejectedExecutionException 79 * if the task cannot be scheduled for execution 80 * @throws NullPointerException 81 * if the task is null 82 */ 83 public Future<?> submit( Runnable task ) 84 { 85 86 return _executorService.submit( task ); 87 } 88 89 /** 90 * The following method shuts down the _executorService in two phases, first by calling shutdown to reject incoming tasks, and then calling shutdownNow, if 91 * necessary, to cancel any lingering tasks: 92 */ 93 public void shutdown( ) 94 { 95 96 _executorService.shutdown( ); 97 try 98 { 99 if ( !_executorService.awaitTermination( 60, TimeUnit.SECONDS ) ) 100 { 101 _executorService.shutdownNow( ); 102 } 103 } 104 catch( InterruptedException e ) 105 { 106 // (Re-)Cancel if current thread also interrupted 107 AppLogService.error( e.getMessage( ), e ); 108 _executorService.shutdownNow( ); 109 Thread.currentThread().interrupt(); 110 } 111 } 112 113 }