zoukankan      html  css  js  c++  java
  • AppMain

    @Controller
    @ComponentScan
    @Configuration
    @EnableScheduling
    @EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class, RedisAutoConfiguration.class, MybatisAutoConfiguration.class})
    @ImportResource(locations = {"classpath*:app.xml"})
    public class AppMain implements ApplicationContextAware {//extends SpringBootServletInitializer

    private final static Logger log = LoggerFactory.getLogger(AppMain.class);

    private final static int retention = 86400 * 1000 * 3;

    private final static List<Runnable> preHaltTasks = Lists.newArrayList();

    private static ApplicationContext context;

    public static ApplicationContext context() {
    return context;
    }

    private static boolean halt = false;

    @Autowired
    Environment environment;

    @Value("${server.tomcat.accesslog.enabled}")
    boolean accessLogEnabled;

    @Value("${server.tomcat.accesslog.directory}")
    String accessLogPath;

    @RequestMapping("/ok.htm")
    @ResponseBody
    String ok(@RequestParam(defaultValue = "false") String down, final HttpServletResponse response) {
    if (halt) {
    response.setStatus(HttpStatus.SERVICE_UNAVAILABLE.value());
    return "halting";
    }
    if (Boolean.parseBoolean(down) && !halt) {
    log.warn("prehalt initiated and further /ok.htm request will return with status 503");
    halt = true;
    for (final Runnable r : preHaltTasks) {
    try {
    r.run();
    } catch (Exception e) {
    log.error("prehalt task failed", e);
    }
    }
    }
    return "ok";
    }

    @RequestMapping("/metadata/env/{prop}/")
    @ResponseBody
    String envProperty(@PathVariable String prop) {
    return environment.getProperty(prop, "");
    }

    @RequestMapping("/")
    @ResponseBody
    String home() {
    return "ok";
    }

    @Scheduled(cron = " 0 5 0 * * ? ") //runs every day 00:05:00
    public void accessLogCleaner() {
    if (accessLogEnabled) {
    if (StringUtils.isEmpty(accessLogPath)) {
    return;
    }
    log.warn("now cleaning access log in dir {}", accessLogPath);
    final Collection<File> files = FileUtils.listFiles(new File(accessLogPath), new String[]{"log"}, false);
    if (CollectionUtils.isEmpty(files)) {
    log.warn("no log found and nothing to do");
    return;
    }
    for (final File f : files) {
    if (f.getName().startsWith("access_log") && System.currentTimeMillis() - f.lastModified() > retention) {
    final boolean b = f.delete();
    log.warn("deleting old log {} ... {}", f.getName(), b);
    }
    }
    }
    }

    public static void addPreHaltTask(final Runnable runnable) {
    if (runnable != null) {
    preHaltTasks.add(runnable);
    }
    }

    public static void main(String[] args) throws Exception {
    log.warn("samaritan started");
    try {
    SpringApplication.run(AppMain.class, args);
    } catch (Throwable e) {
    e.printStackTrace();
    throw e;
    }
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    if (AppMain.context == null) {
    AppMain.context = applicationContext;
    }
    }

    /*
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
    // return super.configure(builder);
    return builder.sources(AppMain.class);
    }

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
    servletContext.setInitParameter("logSystem","log4j,logback");
    servletContext.setInitParameter("loggingLevel", "INFO");
    servletContext.setInitParameter("loggingCharset", "UTF-8");
    servletContext.setInitParameter("contextConfigLocation", "<NONE>");
    super.onStartup(servletContext);
    }
    */
    }
  • 相关阅读:
    【Sqoop】介绍、安装、使用(列出MySQL中数据库/表 + mysql数据导入到Hive表 + Hive表数据导出到mysql表)
    【异常】MISCONF Redis is configured to save RDB snapshots, but it is currently not able to persist on di
    【异常】Flume启动卡主异常:Agent configuration for 'a2' does not contain any valid channels. Marking it as invalid.
    【异常】转载 kafka.common.MessageSizeTooLargeException
    【异常】转载 ERROR KafkaProducer
    【异常】转载 如何优雅地关闭kafka
    【异常】转载 KAFKA生产者数据丢失问题的排查
    【异常】java.lang.ClassCastException: org.apache.spark.rdd.ShuffledRDD cannot be cast to org.apache.spark.streaming.kafka010.HasOffsetRanges
    mmap
    链表
  • 原文地址:https://www.cnblogs.com/exmyth/p/11093470.html
Copyright © 2011-2022 走看看