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-UITextField和UITextView隐藏键盘
UITextField和UITextView隐藏键盘 71 views, IOS DEV, by admin. self._textField.returnKeyType=UIReturnKeyDon ...
- 连载三:RobotFramework+Selenium+Jenkins分布式构建
目标:Jenkins安装在服务器上,而使用Jenkins调用本机的脚本并在本机执行. 步骤: (1)需要有RobotFrameWork+Selenium的运行环境: python2.7,Robotfr ...
- charles 开始/暂停记录
本文参考:charles 开始/暂停记录 1.1. stop/start recording 和 2.1 recording settings 是常用的功能了:这里需要注意就是后面的session1代 ...
- 海量无损高音质音乐文件分享180TB(持续更新)
海量无损高音质音乐文件分享180TBWAV,flac,ape格式(持续更新),由于本人是音乐发烧爱好者,收集海量的无损音乐,已经分类好了,比较方便查找,但是本地没法存储,所有放在网盘中,并且我这边还会 ...
- 白嫖百度 Tesla V100 笔记(在 AI Studio 上使用 tensorflow 和 pytorch 的方法)
登陆百度 AI Studio 并按照教程创建新项目 启动项目并进入控制台 下载 Anaconda3/Miniconda3 安装脚本 安装在 ~/work/*conda3 目录 输入命令 source ...
- Matlab给曲线添加加参考线
声明:引用请注明出处http://blog.csdn.net/lg1259156776/ 对于Matlab的使用情况常常是这样子的,很多零碎的函数名字很难记忆,经常用过后过一段时间就又忘记了,又得去网 ...
- Windows服务操作帮助类
/// <summary> /// 打开系统服务 /// </summary> /// <param name="serviceName">系统 ...
- windows服务器入门 mysql的安装
钱的问题 只能安装mysql了 下载MySQL 5.5(其他的版本都可以的 只是在这里以mysql5.5为例)安装包(下载地址:http://dev.mysql.com/downloads/my ...
- TCP通信的文件上传案例
- [转帖]从光刻机的发展,看懂ASML为何是不可取替
从光刻机的发展,看懂ASML为何是不可取替 http://mini.eastday.com/mobile/171230223351249.html# 2017-12-30 22:33 来源:半导 ...