001/* 002 * (C) Copyright 2006-2008 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 * Nuxeo - initial API and implementation 018 * 019 * $Id$ 020 * 021 */ 022package org.nuxeo.ecm.platform.pictures.tiles.service; 023 024import org.apache.commons.logging.Log; 025import org.apache.commons.logging.LogFactory; 026import org.nuxeo.ecm.platform.pictures.tiles.api.PictureTilingService; 027import org.nuxeo.runtime.api.Framework; 028 029/** 030 * Task for GC dedicated Thread 031 * 032 * @author tiry 033 */ 034public class GCTask implements Runnable { 035 036 protected static volatile boolean GCEnabled = false; 037 038 private static final String GCINTERVAL_KEY = "GCInterval"; 039 040 private static long GC_INTERVAL = 0; 041 042 private static final Log log = LogFactory.getLog(GCTask.class); 043 044 public static long getGCIntervalInMinutes() { 045 if (GC_INTERVAL == 0) { 046 PictureTilingComponent ptc = (PictureTilingComponent) Framework.getService(PictureTilingService.class); 047 GC_INTERVAL = Long.parseLong(ptc.getEnvValue(GCINTERVAL_KEY, Long.toString(10))); 048 log.debug("GC interval set to " + GC_INTERVAL); 049 } 050 return GC_INTERVAL; 051 } 052 053 public static void setGCIntervalInMinutes(long interval) { 054 GC_INTERVAL = interval; 055 } 056 057 public void run() { 058 log.debug("starting GC thread"); 059 while (GCEnabled) { 060 PictureTilingCacheGCManager.gcIfNeeded(); 061 try { 062 long gcInterval = getGCIntervalInMinutes(); 063 064 if (gcInterval < 0) { 065 // for tests 066 log.debug("GC sleeps for " + (-gcInterval)); 067 Thread.sleep(-gcInterval); 068 } else { 069 log.debug("GC sleeps for " + (gcInterval * 60 * 1000)); 070 // GC Interval is stored in minutes 071 Thread.sleep(gcInterval * 60 * 1000); 072 } 073 } catch (InterruptedException e) { 074 Thread.currentThread().interrupt(); 075 GCEnabled = false; 076 } 077 } 078 } 079 080}