有些需要在项目启动时执行的方法,如设置全局环境语言变量等,需要用到@PostConstruct注解,在此记录一下。
1.@PostConstruct说明
? ? ?被@PostConstruct修饰的方法会在服务器加载Servlet的时候运行,并且只会被服务器调用一次,类似于Servlet的inti()方法。被@PostConstruct修饰的方法会在构造函数之后,init()方法之前运行。
2.@PreDestroy说明
? ? ?被@PreDestroy修饰的方法会在服务器卸载Servlet的时候运行,并且只会被服务器调用一次,类似于Servlet的destroy()方法。被@PreDestroy修饰的方法会在destroy()方法之后运行,在Servlet被彻底卸载之前。 ?
package com.oceansite.intelligentmonitor;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure;
import com.oceansite.common.redis.service.RedisService;
import com.oceansite.common.security.annotation.EnableRyFeignClients;
import com.oceansite.intelligentmonitor.constant.Constant;
import com.oceansite.intelligentmonitor.entity.NvrInfo;
import com.oceansite.intelligentmonitor.mapper.NvrInfoMapper;
import com.oceansite.intelligentmonitor.service.HCNetSDK;
import com.oceansite.intelligentmonitor.thread.CameraThread;
import com.oceansite.intelligentmonitor.utils.CameraPush;
import com.oceansite.intelligentmonitor.utils.NET_DVR_DEVICEINFO_V30;
import com.sun.jna.NativeLong;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.ApplicationContext;
import org.springframework.scheduling.annotation.EnableScheduling;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.util.List;
/**
* @ClassName: ShipRecordApplication
* @Description:
* @author: kouyang
* @Date: 2021/10/26 11:00
* @Version: 1.0
*/
@SpringBootApplication(exclude = DruidDataSourceAutoConfigure.class)
@EnableDiscoveryClient
@EnableRyFeignClients
@EnableRabbit
@EnableScheduling
public class intelligentMonitor {
@Autowired
NvrInfoMapper nvrInfoMapper;
HCNetSDK hCNetSDK = HCNetSDK.INSTANCE;
@Autowired
RedisService redisService;
private Logger logger = LoggerFactory.getLogger(intelligentMonitor.class);
public static void main(String[] args) throws Exception {
final ApplicationContext applicationContext = SpringApplication.run(intelligentMonitor.class, args);
CameraPush.setApplicationContext(applicationContext);
}
@PreDestroy
public void destory() {
System.err.println("释放空间...");
// 关闭线程池
CameraThread.MyRunnable.es.shutdownNow();
//删除redis中登录的nvr的luserid
redisService.deleteObjects(Constant.SYS_ALARMCHAN_KEY);
}
@PostConstruct
public void init(){
List<NvrInfo> nvrInfos = nvrInfoMapper.selectNvrInfoList(null);
if (ObjectUtil.isNotEmpty(nvrInfos)){
boolean initSuc = hCNetSDK.NET_DVR_Init();
if (initSuc != true) {
logger.info("初始化失败");
}
hCNetSDK.NET_DVR_SetLogToFile(3,"C:\\Users\\Administrator\\Desktop\\oceansite\\App_Server\\oceansite-intelligentmonitor\\sdklogs",false);
for (NvrInfo nvrInfo : nvrInfos) {
//NativeLong NET_DVR_Login(String sDVRIP,short wDVRPort,String sUserName,String sPassword,NET_DVR_DEVICEINFO lpDeviceInfo);
NET_DVR_DEVICEINFO_V30 m_strDeviceInfo = null;
NativeLong lUserID = hCNetSDK.NET_DVR_Login_V30(nvrInfo.getNvrPublicNetworkIp(),
(short) nvrInfo.getNvrPort().intValue(), nvrInfo.getNvrUserName(), nvrInfo.getNvrPassWord(), m_strDeviceInfo);
redisService.setCacheObject(Constant.SYS_ALARMCHAN_KEY+nvrInfo.getNvrId(),lUserID.intValue());
}
}
}
}
|