@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);
}
*/
}

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. C/C++语言,自学资源,滚动更新中……

    以下教学视频中,缺少对“字符串”技术的讨论,大家注意看书. 一维数组,及其举例:(第四版)P77~P85,书上这部分内容写的很好,很详细,尤其是这里列出来的每一个例子都要仔细去看一看,对你会很有帮助. ...

  2. 解决 Ceph v14.2.1 RGW使能压缩时遇到不生效的问

    根据常规方法部署Ceph(14.2.1 or 14.2.2)集群+rgw后,通过命令“radosgw-admin zone placement modify --rgw-zone=default -- ...

  3. 高级UI-RecyclerView简单使用

    RecyclerView从Android 5.0开始引入,用以替代ListView.GridView控件,RecyclerView的存在,使得控件的耦合度更低,在ListView中需要使用ViewHo ...

  4. apache-phoenix-5.0.0-HBase-2.0-bin 登陆报错

    1.Error: org.apache.hadoop.hbase.DoNotRetryIOException: java.io.IOException: Unable to load configur ...

  5. org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Could

    org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exc ...

  6. 如何使用Curator监听zookeeper事件变化

    掌握zookeeper事件监听机制,非常重要,可以说是跨入了进阶的门槛,只有掌握了如何监听某个节点或路径,我们才能在节点变化后,做一些我们想做的事,包括: 1,配置文件同步 2,主从切换 3,分布式队 ...

  7. ElasticSearch总结

    转载自:https://www.cnblogs.com/WessonStar/p/8296781.html 不过我的es版本是7.2.0,然后这里要注意ik分词器放入plugins里是需要有个ik插件 ...

  8. [转帖]删除一张大表时为什么undo占用空间接近原表两倍?

    删除一张大表时为什么undo占用空间接近原表两倍? https://www.toutiao.com/i6736735016492990983/ 原创 波波说运维 2019-09-22 00:01:00 ...

  9. 深入理解 Css3 的 clip-path

    clip-path CSS 属性可以创建一个只有元素的部分区域可以显示的剪切区域.区域内的部分显示,区域外的隐藏.clip-path属性代替了现在已经弃用的剪切 clip属性.clip-path的属性 ...

  10. 《Mysql 锁 - 概述》

    一:锁类型(加锁范围区分类型) - MySQL里面的锁可以分为:全局锁.表级锁.行级锁. 二:全局锁 - 作用 -  对整个数据库实例加锁. - 加锁方式 - MySQL提供加全局读锁的方法:Flus ...