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 ...
随机推荐
- mysql:服务无法启动解决
输入mysqld --console,查看报错 Found option without preceding group in config file E:\mysql-5.7.21-winx64\m ...
- maven项目创建.m2文件夹
创建为.m2.,m2前后都要有点,然后去掉后面的点 settings.xml文件如下: <?xml version="1.0" encoding="UTF-8&qu ...
- golang web框架 beego 学习 (五) 配置文件
app.conf: appname = gowebProject httpport = runmode = dev copyrequestbody = true [db] host= localhos ...
- 高级UI-NavigationView侧滑
NavigationView是遵循MD设计规范的侧滑模式,推荐使用 要使用NavigationView,就需要引入support-design依赖 implementation 'com.androi ...
- Java check是否是日期类型
boolean checkFormate(string parm){ Pattern pattern = Pattern.compile("([0-9]{4})(0[1-9]|1[0-2]) ...
- ThinkPHP读取配置信息
use think\Config; dump(Config::get()); // 或者 dump(config());示例:dump(Config::get('database.database') ...
- ARTS
ARTS的初衷 Algorithm.主要是为了编程训练和学习.每周至少做一个 leetcode 的算法题(先从Easy开始,然后再Medium,最后才Hard).进行编程训练,如果不训练你看再多的算法 ...
- goroutine基础
程序1: package main import ( "fmt" "time" ) func test () { var i int for { fmt.Pri ...
- zookeeper-data
1. The ZooKeeper Data Model 1.1 ZNodes Znodes maintain a stat structure: The Stat structure for each ...
- Centos7.3 为php7 安装swoole 扩展
今天心血来潮想在服务器上安装一下swoole扩展 下面列一下教程: xshell进入你的服务器 然后目录自选吧 反正我放在根目录了 下面是扩展链接: wget https://github.co ...