All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.codebal.cache.catcher.CacheRunner Maven / Gradle / Ivy

There is a newer version: 0.2.1
Show newest version
package com.codebal.cache.catcher;

import com.codebal.cache.catcher.logger.CacheLogger;

import java.util.Date;
import java.util.function.Supplier;

public class CacheRunner implements Runnable {

    Catcher catcher;
    Supplier supplier;

    public Date start_dt;
    public CacheData cacheData;

    public CacheRunner(Catcher catcher, CacheData cacheData, Supplier supplier){
        this.catcher = catcher;
        this.cacheData = cacheData;
        this.supplier = supplier;
    }

    public boolean isTooLong(){
        boolean tooLong = System.currentTimeMillis() - this.start_dt.getTime() > 1000 * 10;
        if(tooLong){
            CacheLogger.error(this.getClass(), "캐시 생성 너무 오래 걸림 : " + cacheData.key);
        }
        return tooLong;
    }

    @Override
    public void run() {
        //CacheLogger.debug("thread name : " + Thread.currentThread().getName());
        start_dt = new Date(System.currentTimeMillis());

        try{
            Object data = supplier.get();
            cacheData.setData(data);
        }
        catch(Exception e){
            e.printStackTrace();
            //CacheLogger.error(this.getClass(), "thread name : " + Thread.currentThread().getName());
            CacheLogger.error(this.getClass(), e);
        }
        finally {
            catcher.cacheResourceSetter.apply(cacheData);
            catcher.endCreatingCache(cacheData);
            //CacheLogger.debug(cacheData);
        }
    }
}