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);
    }
    */
    }
  • 相关阅读:
    iftop 安装流程
    Centos 6.5 Tengine 安装流程
    linux 查看系统进程前十
    Centos 6.5 mongodb 安装流程
    linux 磁盘查看方式
    Linux 磁盘分区及挂载
    linux 路由添加
    rsyslog 重启
    文件上传到Web服务器
    一些链接1
  • 原文地址:https://www.cnblogs.com/exmyth/p/11093470.html
Copyright © 2011-2022 走看看