📄 autobill.java
字号:
package com.onet.autobill;
import java.io.File;
import java.io.IOException;
import java.util.List;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import com.onet.serviceframework.common.BussinessService;
import com.onet.util.tools.Locker;
/**
* 自动倒库3.0,程序入口类
*
* @author mengwei
*
*/
public class AutoBill {
private static Logger logger = Logger.getLogger(AutoBill.class);
private List<BussinessService> bussinessServices;
public AutoBill() {
};
/**
* 启动客户端框架,如果某一个执行动作失败,则认为启动客户端失败, 程序将停止运行,并退出,具体错误信息将输出到日志文件中。
* 启动动作具体包括:加载配置项、启动业务层面的服务、启动通讯层服务。
*/
public void start() {
if (false == initService()) {
logger.fatal("初始化服务失败,程序无法正确启动!");
System.exit(0);
}
if (false == startService()) {
logger.fatal("启动业务层服务失败,程序无法正确启动!");
System.exit(0);
}
logger.info("程序启动成功。");
}
/**
* 关闭客户端框架 具体包括:关闭通讯层服务、关闭业务层面的服务、将某些相关信息写入文件,以备下次启动加载
*/
public void shutDown() {
shutDownService();
logger.info("程序停止运行。");
}
/**
* 初始化服务
*
* @return 初始化是否成功
*/
private boolean initService() {
if (bussinessServices == null) {
logger.error("没有任何服务模块注册到主类,初始化程序失败!");
return false;
}
for (BussinessService bs : bussinessServices) {
if (bs.initService() == false) {
logger.fatal("初始化服务[" + bs.getServiceName() + "]失败!");
return false;
} else {
logger.info("初始化服务[" + bs.getServiceName() + "]成功。");
}
}
return true;
}
/**
* 启动框架的业务服务,例如数据库服务或算法服务等,这取决于配置项
*
* @return 启动是否成功
*/
private boolean startService() {
if (bussinessServices == null) {
logger.error("没有任何服务模块注册到客户端主类,启动程序失败!");
return false;
}
for (BussinessService bs : bussinessServices) {
if (bs.startService() == false) {
logger.fatal("启动服务[" + bs.getServiceName() + "]失败!");
return false;
} else {
logger.info("启动服务[" + bs.getServiceName() + "]成功。");
}
}
return true;
}
/**
* 关闭所有服务
*/
private void shutDownService() {
for (BussinessService bs : bussinessServices) {
try {
bs.shutDownService();
} catch (Exception e) {
logger.error("关闭服务[" + bs.getServiceName() + "]时出现异常", e);
}
}
}
public List<BussinessService> getBussinessServices() {
return bussinessServices;
}
public void setBussinessServices(List<BussinessService> bussinessServices) {
this.bussinessServices = bussinessServices;
}
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) {
PropertyConfigurator.configure("config/log4j.properties");
// 确保当前目录下只有一个进程在运行
Locker locker = new Locker();
if (!locker.lock()) {
logger.fatal("程序已经启动了!请按任意键退出窗口...");
try {
System.in.read();
} catch (IOException e) {
}
System.exit(-1);
}
ApplicationContext appContext = new FileSystemXmlApplicationContext(
"config/applicationContext.xml");
AutoBill autoBill = (AutoBill) appContext.getBean("autoBill");
autoBill.start();
// 关闭批处理文件通过判断一个文件是否存在来控制程序是否要退出。
File running = new File(System.getProperty("user.dir") + "/"
+ "running");
try {
running.createNewFile();
} catch (IOException e) {
logger.fatal("创建running文件失败!", e);
System.exit(-1);
}
long runningLastModified = running.lastModified();
// 判断running文件是否存在,如果不存在了,就退出主线程,关闭程序。
while (false == Thread.currentThread().isInterrupted()) {
if (runningLastModified != running.lastModified()) {
logger.info("running文件被删除。");
autoBill.shutDown();
break;
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
logger.info("主线程被中断", e);
autoBill.shutDown();
break;
}
}
System.exit(0);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -