@EnableScheduling
@Service
public class BaseTask implements InitializingBean, ApplicationContextAware{
private final static Logger viewLogger = LoggerFactory.getLogger(BaseTask.class);
public static final int HOURPERDATE=24;
public static final String DATABASETABLES="ibms2";
public static final String DATABASEBEGINWITH="sgly";
public static final String DATABASEENDWITH="01";
@Autowired
private EquipmentService equipmentService;
private HashMap<String, Equipment> equipmentsMap = new HashMap<String, Equipment>();
List<String> ibms2TableNames =new ArrayList<String>();//存储表名
public static final String DRIVER_CLASS = "com.mysql.jdbc.Driver";
public static final String URL = "jdbc:mysql://10.5.210.101:3306/ibms2";
public static final String USERNAME = "root";
public static final String PASSWORD = "Zaq!2wsx";
/*"30 10 1 * * ?" 每天1点10分30秒触发任务
* //@Scheduled(cron="30 * * * * ?")
* */
@Scheduled(cron="30 55 13 * * ?")
@Transactional
public void run(){
//定时调用存储过程
String sql="{CALL p_simulate_dynamic_cursor()}";
try {
Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
CallableStatement prepareCall = connection.prepareCall(sql);//可以设置参数
prepareCall.execute();//执行存储过程。
viewLogger.info("CALL p_simulate_dynamic_cursor()执行完毕");
prepareCall.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void afterPropertiesSet() throws Exception {
equipmentsMap = new HashMap<String,Equipment>();
Iterable<Equipment> equipmentsIterator = equipmentService.findAll();
for (Equipment equipment : equipmentsIterator) {
if(!equipmentsMap.containsKey(equipment)){
equipmentsMap.put(equipment.getEquid(), equipment);
}
}
}
@Override
public void setApplicationContext(ApplicationContext arg0) throws BeansException {
AppServiceHelper.setApplicationContext(arg0);
}
}