1. @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
  2.  
  3. private final static Logger log = LoggerFactory.getLogger(AppMain.class);
  4.  
  5. private final static int retention = 86400 * 1000 * 3;
  6.  
  7. private final static List<Runnable> preHaltTasks = Lists.newArrayList();
  8.  
  9. private static ApplicationContext context;
  10.  
  11. public static ApplicationContext context() {
    return context;
    }
  12.  
  13. private static boolean halt = false;
  14.  
  15. @Autowired
    Environment environment;
  16.  
  17. @Value("${server.tomcat.accesslog.enabled}")
    boolean accessLogEnabled;
  18.  
  19. @Value("${server.tomcat.accesslog.directory}")
    String accessLogPath;
  20.  
  21. @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";
    }
  22.  
  23. @RequestMapping("/metadata/env/{prop}/")
    @ResponseBody
    String envProperty(@PathVariable String prop) {
    return environment.getProperty(prop, "");
    }
  24.  
  25. @RequestMapping("/")
    @ResponseBody
    String home() {
    return "ok";
    }
  26.  
  27. @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);
    }
    }
    }
    }
  28.  
  29. public static void addPreHaltTask(final Runnable runnable) {
    if (runnable != null) {
    preHaltTasks.add(runnable);
    }
    }
  30.  
  31. 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;
    }
    }
  32.  
  33. @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    if (AppMain.context == null) {
    AppMain.context = applicationContext;
    }
    }
  34.  
  35. /*
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
    // return super.configure(builder);
    return builder.sources(AppMain.class);
    }
  36.  
  37. @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);
    }
    */
    }

AppMain的更多相关文章

  1. Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory

    学习架构探险,从零开始写Java Web框架时,在学习到springAOP时遇到一个异常: "C:\Program Files\Java\jdk1.7.0_40\bin\java" ...

  2. JVM学习(1)——通过实例总结Java虚拟机的运行机制

    俗话说,自己写的代码,6个月后也是别人的代码……复习!复习!复习!涉及到的知识点总结如下: JVM的历史 JVM的运行流程简介 JVM的组成(基于 Java 7) JVM调优参数:-Xmx和-Xms ...

  3. 学习Spring——两个你熟悉的不能再熟悉的场景使用

    最近公众号受邀获取了留言和赠送模板的权限,小开心(欢迎去公众号JackieZheng围观). 我们大致的了解了Spring这个框架对于依赖注入的使用和诠释可谓是淋漓尽致.因为有了Spring的这个IO ...

  4. Java正则速成秘籍(一)之招式篇

    导读 正则表达式是什么?有什么用? 正则表达式(Regular Expression)是一种文本规则,可以用来校验.查找.替换与规则匹配的文本. 又爱又恨的正则 正则表达式是一个强大的文本匹配工具,但 ...

  5. java.net.SocketException: Connection reset

    java.net.SocketException: Connection reset at java.net.SocketInputStream.read(SocketInputStream.java ...

  6. Spring 4 + Quartz 2.2.1 Scheduler Integration Example

    In this post we will see how to schedule Jobs using Quartz Scheduler with Spring. Spring provides co ...

  7. JAVA-堆区,栈区,方法区。

    转载:http://blog.csdn.net/wangxin1982314/article/details/50293241 堆区: 村线程操纵的数据(对象形式存放) 1 存储的全部是对象,每个对象 ...

  8. 【译】Spring 4 基于TaskScheduler实现定时任务(注解)

    前言 译文链接:http://websystique.com/spring/spring-job-scheduling-with-scheduled-enablescheduling-annotati ...

  9. 【译】Spring 4 + Hibernate 4 + Mysql + Maven集成例子(注解 + XML)

    前言 译文链接:http://websystique.com/spring/spring4-hibernate4-mysql-maven-integration-example-using-annot ...

随机推荐

  1. mysql:服务无法启动解决

    输入mysqld --console,查看报错 Found option without preceding group in config file E:\mysql-5.7.21-winx64\m ...

  2. maven项目创建.m2文件夹

    创建为.m2.,m2前后都要有点,然后去掉后面的点 settings.xml文件如下: <?xml version="1.0" encoding="UTF-8&qu ...

  3. golang web框架 beego 学习 (五) 配置文件

    app.conf: appname = gowebProject httpport = runmode = dev copyrequestbody = true [db] host= localhos ...

  4. 高级UI-NavigationView侧滑

    NavigationView是遵循MD设计规范的侧滑模式,推荐使用 要使用NavigationView,就需要引入support-design依赖 implementation 'com.androi ...

  5. Java check是否是日期类型

    boolean checkFormate(string parm){ Pattern pattern = Pattern.compile("([0-9]{4})(0[1-9]|1[0-2]) ...

  6. ThinkPHP读取配置信息

    use think\Config; dump(Config::get()); // 或者 dump(config());示例:dump(Config::get('database.database') ...

  7. ARTS

    ARTS的初衷 Algorithm.主要是为了编程训练和学习.每周至少做一个 leetcode 的算法题(先从Easy开始,然后再Medium,最后才Hard).进行编程训练,如果不训练你看再多的算法 ...

  8. goroutine基础

    程序1: package main import ( "fmt" "time" ) func test () { var i int for { fmt.Pri ...

  9. zookeeper-data

    1. The ZooKeeper Data Model 1.1 ZNodes Znodes maintain a stat structure: The Stat structure for each ...

  10. Centos7.3 为php7 安装swoole 扩展

    今天心血来潮想在服务器上安装一下swoole扩展  下面列一下教程: xshell进入你的服务器  然后目录自选吧  反正我放在根目录了 下面是扩展链接: wget https://github.co ...