Changeset 5457
- Timestamp:
- 07/08/14 10:01:33 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
VirtualCollectionRegistry/trunk/VirtualCollectionRegistry/src/main/java/eu/clarin/cmdi/virtualcollectionregistry/VirtualCollectionRegistry.java
r5441 r5457 13 13 import java.util.Date; 14 14 import java.util.List; 15 import java.util.Timer; 16 import java.util.TimerTask; 15 import java.util.concurrent.Executors; 16 import java.util.concurrent.ScheduledExecutorService; 17 import java.util.concurrent.ThreadFactory; 18 import java.util.concurrent.TimeUnit; 17 19 import java.util.concurrent.atomic.AtomicBoolean; 20 import java.util.concurrent.atomic.AtomicInteger; 18 21 import javax.persistence.EntityManager; 19 22 import javax.persistence.EntityTransaction; … … 37 40 38 41 @Autowired 39 private DataStore datastore; //TODO: replace with Spring managed EM 42 private DataStore datastore; //TODO: replace with Spring managed EM? 40 43 @Autowired 41 44 private PersistentIdentifierProvider pid_provider; … … 48 51 = LoggerFactory.getLogger(VirtualCollectionRegistry.class); 49 52 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"); 52 60 53 61 @Override … … 63 71 logger.info("Initializing virtual collection registry ..."); 64 72 try { 65 // setup VCR maintenance task66 timer.schedule(new TimerTask() { 73 maintenanceExecutor.scheduleWithFixedDelay(new Runnable() { 74 67 75 @Override 68 76 public void run() { 69 maintenance( this.scheduledExecutionTime());70 } 71 }, 60 000, 60000);77 maintenance(new Date().getTime()); 78 } 79 }, 60, 60, TimeUnit.SECONDS); 72 80 this.intialized.set(true); 73 81 logger.info("virtual collection registry successfully intialized"); … … 79 87 80 88 @Override 81 public void destroy() throws VirtualCollectionRegistryException {89 public void destroy() throws VirtualCollectionRegistryException, InterruptedException { 82 90 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 } 84 95 85 96 logger.info("Shutting down OAI provider"); … … 608 619 } 609 620 em.getTransaction().commit(); 610 } catch ( Exception e) {621 } catch (VirtualCollectionRegistryException e) { 611 622 logger.error("error while doing maintenance", e); 612 623 } finally { … … 628 639 } 629 640 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 630 662 } // class VirtualCollectionRegistry
Note: See TracChangeset
for help on using the changeset viewer.