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

转载于:https://www.cnblogs.com/exmyth/p/11093470.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值