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);
}
*/
}
AppMain的更多相关文章
- 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" ...
- JVM学习(1)——通过实例总结Java虚拟机的运行机制
俗话说,自己写的代码,6个月后也是别人的代码……复习!复习!复习!涉及到的知识点总结如下: JVM的历史 JVM的运行流程简介 JVM的组成(基于 Java 7) JVM调优参数:-Xmx和-Xms ...
- 学习Spring——两个你熟悉的不能再熟悉的场景使用
最近公众号受邀获取了留言和赠送模板的权限,小开心(欢迎去公众号JackieZheng围观). 我们大致的了解了Spring这个框架对于依赖注入的使用和诠释可谓是淋漓尽致.因为有了Spring的这个IO ...
- Java正则速成秘籍(一)之招式篇
导读 正则表达式是什么?有什么用? 正则表达式(Regular Expression)是一种文本规则,可以用来校验.查找.替换与规则匹配的文本. 又爱又恨的正则 正则表达式是一个强大的文本匹配工具,但 ...
- java.net.SocketException: Connection reset
java.net.SocketException: Connection reset at java.net.SocketInputStream.read(SocketInputStream.java ...
- 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 ...
- JAVA-堆区,栈区,方法区。
转载:http://blog.csdn.net/wangxin1982314/article/details/50293241 堆区: 村线程操纵的数据(对象形式存放) 1 存储的全部是对象,每个对象 ...
- 【译】Spring 4 基于TaskScheduler实现定时任务(注解)
前言 译文链接:http://websystique.com/spring/spring-job-scheduling-with-scheduled-enablescheduling-annotati ...
- 【译】Spring 4 + Hibernate 4 + Mysql + Maven集成例子(注解 + XML)
前言 译文链接:http://websystique.com/spring/spring4-hibernate4-mysql-maven-integration-example-using-annot ...
随机推荐
- iOS技术面试01:多线程与网络
1. 多线程的底层实现? 1> 首先搞清楚什么是线程.什么是多线程.多线程的使用场合(线程有时被称为轻量级进程,是程序执行流的最小单元.多线程是指软件或者硬件实现多个线程并发执行的技术.多线程的 ...
- iOS-ASI异步下载图片
异步下载图片 异步下载图片ASIHTTPRequest *requestX = [ASIHTTPRequest requestWithURL:url]; self.re ...
- sed练习,一些sed常用方法
1.复制/etc/rc.d/rc.local 文件至/tmp目录,将/tmp/rc.sysinit文件中的以至少一个空白字符开头的行的行首加#. sed -ri 's/^ +/#/g' rc.loc ...
- linux中的pvmove,pvremove,pvs,pvscan
PV,VG,LV的关系和操作 PV,VG,LV构成了一种易于管理拥有一个或多个硬盘的主机的文件系统,这些硬盘可能只有一个分区也可能有多个.通过将这些物理存在的分区(或称为卷)PV(physical v ...
- 【C++札记】指针函数与函数指针
指针函数 指针函数是一个函数,只不过指针函数返回的类型是某一类型的指针. 格式: 类型名* 函数名(函数参数列表) 如下代码存在问题 void test(char *p) { p = (char*)m ...
- STM32启动文件详解
启动文件使用的 ARM 汇编指令汇总 启动程序源码注释(点此下载) 1. Stack—栈 Stack_Size EQU 0x00000400 AREA STACK, NOINIT, READWRITE ...
- S03_CH05_AXI_DMA_HDMI图像输出
S03_CH05_AXI_DMA_HDMI图像输出 5.1概述 本课程是在前面课程基础上添加HDMI IP 实现HDMI视频图像的输出.本课程出了多了HDMI输出接口,其他内容和<S03_CH0 ...
- Linux下PHP7.2扩展
前言 由于公司某项目需要连接oracle数据库,该项目使用的开发语言为PHP,故需要对PHP进行扩展 环境说明 服务器:Centos7 PHP:7.2, 源码安装;安装路径:/usr/local/xx ...
- CTS & APIO 2019 游记
写在前面 算是省选后的第一轮大考. 去年因为某些原因并没有参加 CTSC 以及 APIO,还是有些遗憾,所以希望今年能有所收获. 也希望今年的 CTS 能延续去年的出题风格,这样我还能苟一两个题. 然 ...
- 关于spring中配置文件路径的那些事儿
在项目中我们经常会需要读一些配置文件来获取配置信息,然而对于这些配置文件在项目中存放的位置以及获取这些配置文件的存放路径却经常搞不清楚,自己研究了一下,记录下来以备后用. 测试代码如下 package ...