java 通过配置监听器(Listener)设置项目启动给LED推送数据

来源:互联网 发布:dijkstra算法 matlab 编辑:程序博客网 时间:2024/06/05 11:14
web.xml 配置


<listener>
    <listener-class>com.***.***.listenvision.LedContextListener</listener-class>
</listener>


LedContextListener 文件


package com.***.***.listenvision;


import com.***.***.model.AttDetail;
import com.***.***.service.AttDetailService;
import com.***.***.service.FaceService;
import com.***.pay.common.exceptions.ValidationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;


import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;


public class LedContextListener implements ServletContextListener{
    private static Logger logger = LoggerFactory.getLogger(LedContextListener.class);
    public static ResourceBundle faceSdk = PropertyResourceBundle.getBundle("face-sdk");
    public static String LEDSwitch = faceSdk.getString("led.switch");


    private AttDetailService attDetailService;
    private FaceService faceService;


    /**
     * 项目启动给LED推送数据
     * @param sce
     */
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        logger.info("获取******信息推送给LED(项目启动给LED推送数据)");
        WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(sce.getServletContext());
        attDetailService = webApplicationContext.getBean(AttDetailService.class);
        faceService = webApplicationContext.getBean(FaceService.class);
        try {
            if("ON".equals(LEDSwitch)){
                //获取最近一名***的信息
                AttDetail attDetail = attDetailService.getLatestEmp();
                if(!StringUtils.isEmpty(attDetail)){
                    faceService.attendanceFace(attDetail.getEmpUuid(),attDetail.getInOut().toString());
                }
            }
        } catch (ValidationException ve) {
            logger.error(ve.getMessage(), ve);
            throw ve;
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw e;
        }
    }


    @Override
    public void contextDestroyed(ServletContextEvent sce) {


    }
}
阅读全文
0 0
原创粉丝点击