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);
    }
    */
    }
  • 相关阅读:
    TensorFlow_CNN_MNIST遇到的问题
    TensorFlow_CNN_MNIST问题
    TensorFlow_CNN内tf.nn.max_pool和tf.layers.max_pooling2d参数
    TensorFlow_CNN内tf.nn.conv2d和tf.layers.conv2d参数
    mysql 更新语句中加判断条件
    数据库 数据去重并取id最大的数据sql
    elasticsearch------java操作之QueryBuilders构建搜索Query
    Elasticsearch java api 基本搜索部分详解
    java 连接 elasticsearch 报错java.lang.NoClassDefFoundError: org/apache/http/auth/Credentials 解决
    java 获取文件内所有文件名
  • 原文地址:https://www.cnblogs.com/exmyth/p/11093470.html
Copyright © 2011-2022 走看看