Changeset 5457


Ignore:
Timestamp:
07/08/14 10:01:33 (10 years ago)
Author:
Twan Goosen
Message:

Replaced usage of Timer with ScheduledExecutorService?
See http://stackoverflow.com/questions/409932/java-timer-vs-executorservice

File:
1 edited

Legend:

Unmodified
Added
Removed
  • VirtualCollectionRegistry/trunk/VirtualCollectionRegistry/src/main/java/eu/clarin/cmdi/virtualcollectionregistry/VirtualCollectionRegistry.java

    r5441 r5457  
    1313import java.util.Date;
    1414import java.util.List;
    15 import java.util.Timer;
    16 import java.util.TimerTask;
     15import java.util.concurrent.Executors;
     16import java.util.concurrent.ScheduledExecutorService;
     17import java.util.concurrent.ThreadFactory;
     18import java.util.concurrent.TimeUnit;
    1719import java.util.concurrent.atomic.AtomicBoolean;
     20import java.util.concurrent.atomic.AtomicInteger;
    1821import javax.persistence.EntityManager;
    1922import javax.persistence.EntityTransaction;
     
    3740
    3841    @Autowired
    39     private DataStore datastore; //TODO: replace with Spring managed EM
     42    private DataStore datastore; //TODO: replace with Spring managed EM?
    4043    @Autowired
    4144    private PersistentIdentifierProvider pid_provider;
     
    4851            = LoggerFactory.getLogger(VirtualCollectionRegistry.class);
    4952    private final AtomicBoolean intialized = new AtomicBoolean(false);
    50     private final Timer timer
    51             = new Timer("VirtualCollectionRegistry-Maintenance", true);
     53    /**
     54     * Scheduled executor service for the maintenance check
     55     *
     56     * @see #maintenance(long)
     57     */
     58    private final ScheduledExecutorService maintenanceExecutor
     59            = createSingleThreadScheduledExecutor("VirtualCollectionRegistry-Maintenance");
    5260
    5361    @Override
     
    6371        logger.info("Initializing virtual collection registry ...");
    6472        try {
    65             // setup VCR maintenance task
    66             timer.schedule(new TimerTask() {
     73            maintenanceExecutor.scheduleWithFixedDelay(new Runnable() {
     74
    6775                @Override
    6876                public void run() {
    69                     maintenance(this.scheduledExecutionTime());
    70                 }
    71             }, 60000, 60000);
     77                    maintenance(new Date().getTime());
     78                }
     79            }, 60, 60, TimeUnit.SECONDS);
    7280            this.intialized.set(true);
    7381            logger.info("virtual collection registry successfully intialized");
     
    7987
    8088    @Override
    81     public void destroy() throws VirtualCollectionRegistryException {
     89    public void destroy() throws VirtualCollectionRegistryException, InterruptedException {
    8290        logger.info("Stopping Virtual Collection Registry maintenance schedule");
    83         timer.cancel();
     91        maintenanceExecutor.shutdown();
     92        if (!maintenanceExecutor.awaitTermination(60, TimeUnit.SECONDS)) {
     93            logger.warn("Timeout while waiting for maintenance thread to terminate, will try to shut down");
     94        }
    8495
    8596        logger.info("Shutting down OAI provider");
     
    608619            }
    609620            em.getTransaction().commit();
    610         } catch (Exception e) {
     621        } catch (VirtualCollectionRegistryException e) {
    611622            logger.error("error while doing maintenance", e);
    612623        } finally {
     
    628639    }
    629640
     641    /**
     642     * Creates a single thread scheduled executor with the specified thread name
     643     *
     644     * @param threadName name for new executor threads
     645     * @return
     646     */
     647    private static ScheduledExecutorService createSingleThreadScheduledExecutor(final String threadName) {
     648        return Executors.newSingleThreadScheduledExecutor(new ThreadFactory() {
     649            // decorate default thread factory so that we can provide a
     650            // custom thread name
     651            final AtomicInteger i = new AtomicInteger(0);
     652
     653            @Override
     654            public Thread newThread(Runnable r) {
     655                final Thread thread = Executors.defaultThreadFactory().newThread(r);
     656                thread.setName(threadName + "-" + i.addAndGet(1));
     657                return thread;
     658            }
     659        });
     660    }
     661
    630662} // class VirtualCollectionRegistry
Note: See TracChangeset for help on using the changeset viewer.