ThisApplication.java 7.9 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
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;
26
import com.x.query.service.processing.schedule.OptimizeIndex;
Z
zhourui 已提交
27 28 29 30 31 32
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 已提交
33 34 35

public class ThisApplication {

Z
zhourui 已提交
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());
71 72 73 74 75 76 77
            scheduleLowFreqDocument();
            scheduleLowFreqWork();
            scheduleLowFreqWorkCompleted();
            scheduleHighFreqDocument();
            scheduleHighFreqWorkCompleted();
            scheduleHighFreqWork();
            scheduleOptimizeIndex();
Z
zhourui 已提交
78 79 80 81 82 83 84 85 86 87 88 89 90 91
            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();
        }
    }

92
    private static void scheduleLowFreqDocument() throws Exception {
Z
zhourui 已提交
93 94 95 96
        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 已提交
97
            } else {
Z
zhourui 已提交
98 99
                context.schedule(LowFreqDocument.class,
                        Config.query().index().getLowFreqDocumentCron());
Z
zhourui 已提交
100 101 102 103
            }
        }
    }

104
    private static void scheduleLowFreqWorkCompleted() throws Exception {
Z
zhourui 已提交
105 106 107 108
        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 已提交
109
            } else {
Z
zhourui 已提交
110 111 112 113 114 115
                context.schedule(LowFreqWorkCompleted.class,
                        Config.query().index().getLowFreqWorkCompletedCron());
            }
        }
    }

116
    private static void scheduleLowFreqWork() throws Exception {
Z
zhourui 已提交
117 118 119 120 121 122 123
        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 已提交
124 125 126 127
            }
        }
    }

128
    private static void scheduleHighFreqDocument() throws Exception {
Z
zhourui 已提交
129 130 131 132
        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 已提交
133
            } else {
Z
zhourui 已提交
134 135
                context.schedule(HighFreqDocument.class,
                        Config.query().index().getHighFreqDocumentCron());
Z
zhourui 已提交
136 137 138 139
            }
        }
    }

140
    private static void scheduleHighFreqWorkCompleted() throws Exception {
Z
zhourui 已提交
141 142 143 144 145 146 147 148 149 150 151
        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());
            }
        }
    }

152
    private static void scheduleHighFreqWork() throws Exception {
Z
zhourui 已提交
153 154 155 156
        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 已提交
157
            } else {
Z
zhourui 已提交
158 159
                context.schedule(HighFreqWork.class,
                        Config.query().index().getHighFreqWorkCompletedCron());
Z
zhourui 已提交
160 161 162 163
            }
        }
    }

164 165 166 167 168 169 170 171 172 173 174 175
    private static void scheduleOptimizeIndex() throws Exception {
        if (BooleanUtils.isTrue(Config.query().index().getOptimizeIndexEnable())) {
            if (StringUtils.equals(Config.query().index().getMode(), Query.Index.MODE_LOCALDIRECTORY)) {
                context.scheduleLocal(OptimizeIndex.class,
                        Config.query().index().getOptimizeIndexCron());
            } else {
                context.schedule(OptimizeIndex.class,
                        Config.query().index().getOptimizeIndexCron());
            }
        }
    }

Z
zhourui 已提交
176 177 178 179 180 181 182
    public static void destroy() {
        try {
            CacheManager.shutdown();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
R
roo00 已提交
183
}