ThisApplication.java 7.3 KB
Newer Older
R
roo00 已提交
1 2
package com.x.query.service.processing;

R
Ray 已提交
3 4 5 6 7 8
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

R
roo00 已提交
9
import org.apache.commons.lang3.BooleanUtils;
Z
zhourui 已提交
10
import org.apache.commons.lang3.StringUtils;
R
roo00 已提交
11

R
Ray 已提交
12
import com.google.common.util.concurrent.ThreadFactoryBuilder;
R
roo00 已提交
13
import com.x.base.core.project.Context;
Z
zhourui 已提交
14
import com.x.base.core.project.cache.CacheManager;
R
roo00 已提交
15
import com.x.base.core.project.config.Config;
Z
zhourui 已提交
16
import com.x.base.core.project.config.Query;
R
roo00 已提交
17 18 19
import com.x.query.service.processing.schedule.CrawlCms;
import com.x.query.service.processing.schedule.CrawlWork;
import com.x.query.service.processing.schedule.CrawlWorkCompleted;
Z
zhourui 已提交
20 21 22 23 24 25 26 27 28 29 30 31
import com.x.query.service.processing.schedule.HighFreqDocument;
import com.x.query.service.processing.schedule.HighFreqWork;
import com.x.query.service.processing.schedule.HighFreqWorkCompleted;
import com.x.query.service.processing.schedule.LowFreqDocument;
import com.x.query.service.processing.schedule.LowFreqWork;
import com.x.query.service.processing.schedule.LowFreqWorkCompleted;
import com.x.query.service.processing.schedulelocal.HighFreqDocumentLocal;
import com.x.query.service.processing.schedulelocal.HighFreqWorkCompletedLocal;
import com.x.query.service.processing.schedulelocal.HighFreqWorkLocal;
import com.x.query.service.processing.schedulelocal.LowFreqDocumentLocal;
import com.x.query.service.processing.schedulelocal.LowFreqWorkCompletedLocal;
import com.x.query.service.processing.schedulelocal.LowFreqWorkLocal;
R
roo00 已提交
32 33 34

public class ThisApplication {

Z
zhourui 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
    private ThisApplication() {
        // nothing
    }

    public static final IndexWriteQueue indexWriteQueue = new IndexWriteQueue();

    private static ExecutorService threadPool;

    public static ExecutorService threadPool() {
        return threadPool;
    }

    private static void initThreadPool() {
        int maximumPoolSize = Runtime.getRuntime().availableProcessors() + 1;
        ThreadFactory threadFactory = new ThreadFactoryBuilder()
                .setNameFormat(ThisApplication.class.getPackageName() + "-threadpool-%d").build();
        threadPool = new ThreadPoolExecutor(0, maximumPoolSize, 120, TimeUnit.SECONDS, new ArrayBlockingQueue<>(1000),
                threadFactory);
    }

    protected static Context context;

    public static Context context() {
        return context;
    }

    protected static void setContext(Context context) {
        ThisApplication.context = context;
    }

    public static void init() {
        try {
            initThreadPool();
            context.startQueue(indexWriteQueue);
            CacheManager.init(context.clazz().getSimpleName());
            scheduleLowFrequencyDocument();
Z
zhourui 已提交
71
            scheduleLowFrequencyWork();
Z
zhourui 已提交
72 73 74
            scheduleLowFrequencyWorkCompleted();
            scheduleHighFrequencyDocument();
            scheduleHighFrequencyWorkCompleted();
Z
zhourui 已提交
75
            scheduleHighFrequencyWork();
Z
zhourui 已提交
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
            if (BooleanUtils.isTrue(Config.query().getCrawlWork().getEnable())) {
                context.schedule(CrawlWork.class, Config.query().getCrawlWork().getCron());
            }
            if (BooleanUtils.isTrue(Config.query().getCrawlWorkCompleted().getEnable())) {
                context.schedule(CrawlWorkCompleted.class, Config.query().getCrawlWorkCompleted().getCron());
            }
            if (BooleanUtils.isTrue(Config.query().getCrawlCms().getEnable())) {
                context.schedule(CrawlCms.class, Config.query().getCrawlCms().getCron());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void scheduleLowFrequencyDocument() throws Exception {
Z
zhourui 已提交
91 92 93 94
        if (BooleanUtils.isTrue(Config.query().index().getLowFreqDocumentEnable())) {
            if (StringUtils.equals(Config.query().index().getMode(), Query.Index.MODE_LOCALDIRECTORY)) {
                context.scheduleLocal(LowFreqDocumentLocal.class,
                        Config.query().index().getLowFreqDocumentCron());
Z
zhourui 已提交
95
            } else {
Z
zhourui 已提交
96 97
                context.schedule(LowFreqDocument.class,
                        Config.query().index().getLowFreqDocumentCron());
Z
zhourui 已提交
98 99 100 101 102
            }
        }
    }

    private static void scheduleLowFrequencyWorkCompleted() throws Exception {
Z
zhourui 已提交
103 104 105 106
        if (BooleanUtils.isTrue(Config.query().index().getLowFreqWorkCompletedEnable())) {
            if (StringUtils.equals(Config.query().index().getMode(), Query.Index.MODE_LOCALDIRECTORY)) {
                context.scheduleLocal(LowFreqWorkCompletedLocal.class,
                        Config.query().index().getLowFreqWorkCompletedCron());
Z
zhourui 已提交
107
            } else {
Z
zhourui 已提交
108 109 110 111 112 113 114 115 116 117 118 119 120 121
                context.schedule(LowFreqWorkCompleted.class,
                        Config.query().index().getLowFreqWorkCompletedCron());
            }
        }
    }

    private static void scheduleLowFrequencyWork() throws Exception {
        if (BooleanUtils.isTrue(Config.query().index().getLowFreqWorkEnable())) {
            if (StringUtils.equals(Config.query().index().getMode(), Query.Index.MODE_LOCALDIRECTORY)) {
                context.scheduleLocal(LowFreqWorkLocal.class,
                        Config.query().index().getLowFreqWorkCompletedCron());
            } else {
                context.schedule(LowFreqWork.class,
                        Config.query().index().getLowFreqWorkCompletedCron());
Z
zhourui 已提交
122 123 124 125 126
            }
        }
    }

    private static void scheduleHighFrequencyDocument() throws Exception {
Z
zhourui 已提交
127 128 129 130
        if (BooleanUtils.isTrue(Config.query().index().getHighFreqDocumentEnable())) {
            if (StringUtils.equals(Config.query().index().getMode(), Query.Index.MODE_LOCALDIRECTORY)) {
                context.scheduleLocal(HighFreqDocumentLocal.class,
                        Config.query().index().getHighFreqDocumentCron());
Z
zhourui 已提交
131
            } else {
Z
zhourui 已提交
132 133
                context.schedule(HighFreqDocument.class,
                        Config.query().index().getHighFreqDocumentCron());
Z
zhourui 已提交
134 135 136 137 138
            }
        }
    }

    private static void scheduleHighFrequencyWorkCompleted() throws Exception {
Z
zhourui 已提交
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
        if (BooleanUtils.isTrue(Config.query().index().getHighFreqWorkCompletedEnable())) {
            if (StringUtils.equals(Config.query().index().getMode(), Query.Index.MODE_LOCALDIRECTORY)) {
                context.scheduleLocal(HighFreqWorkCompletedLocal.class,
                        Config.query().index().getHighFreqWorkCompletedCron());
            } else {
                context.schedule(HighFreqWorkCompleted.class,
                        Config.query().index().getHighFreqWorkCompletedCron());
            }
        }
    }

    private static void scheduleHighFrequencyWork() throws Exception {
        if (BooleanUtils.isTrue(Config.query().index().getHighFreqWorkEnable())) {
            if (StringUtils.equals(Config.query().index().getMode(), Query.Index.MODE_LOCALDIRECTORY)) {
                context.scheduleLocal(HighFreqWorkLocal.class,
                        Config.query().index().getHighFreqWorkCron());
Z
zhourui 已提交
155
            } else {
Z
zhourui 已提交
156 157
                context.schedule(HighFreqWork.class,
                        Config.query().index().getHighFreqWorkCompletedCron());
Z
zhourui 已提交
158 159 160 161 162 163 164 165 166 167 168
            }
        }
    }

    public static void destroy() {
        try {
            CacheManager.shutdown();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
R
roo00 已提交
169
}