모니터 인 터 페 이 스 를 이용 하여 자바 의 정시 기능 을 실현 합 니 다.

1553 단어 자바xmlWebservlet
최근 맥주 공장 의 j2ee 프로젝트 를 만 들 었 는데, 그 중 하 나 는 매달 1 일이 다. 지난달 보고 서 를 자동 으로 생 성하 고, 보 는 사람 이 있 든 없 든 자동 으로 생 성 해 야 한다.
우선 servlet 모니터 구현

public class MyListener implements ServletContextListener {
    private Timer timer = null;

    public void contextInitialized(ServletContextEvent servletContextEvent) {
        timer = new Timer(true);
        //     :      ,     :        ,     :      (     )
        timer.schedule(new MyTask(), 0, 86400000);
    }

    public void contextDestroyed(ServletContextEvent servletContextEvent) {
        timer.cancel();  
    }
}

그 다음 에 구체 적 인 임무 유형 을 실현 한다.

public class MyTask extends TimerTask {
    public void run() {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
        sdf.format(date);
        String currentTime = sdf.format(date);
        if(currentTime.split("-")[2].equals("01")) {
            doSomeThing();
        }
    }
    private void doSomeThing() {
        //To change body of created methods use File | Settings | File Templates.
    }
}

마지막 으로 웹 xml 설정

    <listener>
        <listener-class>time.MyListener</listener-class>
    </listener>

매일 한 번 씩 검사 하 라 고 하 세 요. 매달 1 일 인지, 보고 서 를 만 드 는 것 입 니 다.

좋은 웹페이지 즐겨찾기