2014-07-04 1 views
1

오류를 피하기 위해 SQL 데이터베이스에서 값을 확인하고 수정하는 Java 기능이 있습니다. 서버를 시작할 때뿐만 아니라 재시작 할 때도 자동으로 실행해야합니다. 나는이 함수를 jsp 지원 "setInterval"로 호출하기 위해 jsp 페이지를 만들었다.이 함수는 3 분마다 자동으로 데이터베이스에서 오류를 제거하고 서버 시작시 자동으로 실행해야한다. 아무도 이것에 나를 안내 할 수 있습니까?Apache Tomcat 서버가 시작될 때 주기적으로 My Java 기능을 자동으로 실행하는 방법은 무엇입니까?

setInterval(function(){Autolf();},60000); 

function Autolf() 
{ 

$.post('autolgfn.jsp', 
     { 
    abc:1 
     }, 
     function(response,status,xhr) 
     { 
      alert(response.trim()); 

     }); 

} 

위의 코드는 데이터베이스에 연결되어있는 자바 페이지에서 함수를 호출

다음 내 JSP 코드입니다. 3 분마다 서버를 시작할 때 자동으로 실행하고 계속 실행하십시오. 당신은 contextInitialized 방법에 ScheduledExecutorService (또는 Timer) 시작하는 프로세스를 사용하고 contextDestroyed 방법을 멈추는 ServletContextListener을 작성할 수 있습니다 사전

+0

하나의 솔루션 : jsf 관리 bean 확인 @PostConstruct 의미 – maress

+0

어쩌면 당신은 [quartz-scheduler] (http://quartz-scheduler.org/)를 볼 수 있을까? –

답변

7

에 감사합니다.

그것은이 같은 것을 볼 수 있었다 : 브렛의 대답에 정교하게

private volatile ScheduledExecutorService executor; 

public void contextInitialized(ServletContextEvent sce) 
{ 
    executor = Executors.newScheduledThreadPool(2); 
    executor.scheduleAtFixedRate(myRunnable, 0, 3, TimeUnit.MINUTES); 
} 

public void contextDestroyed(ServletContextEvent sce) 
{ 
    final ScheduledExecutorService executor = this.executor; 

    if (executor != null) 
    { 
     executor.shutdown(); 
     this.executor = null; 
    } 
} 
+0

컨텍스트 구축함에서 작성해야 할 내용을 알려주시겠습니까? – jasim

+0

좋아,하지만 내 자바 코드를 넣어야합니까? – jasim

+0

당신의 전쟁에 - 직접 또는 전쟁에서 항아리에 –

3

:

당신의 web.xml을 구성

public class MyContextListener implements ServletContextListener { 
    // where the work happens 
    final Runnable myRunnable = new Runnable() { 
     public void run() { System.out.println("hello world"); } 
    }; 

    // Brett's code here 
    //.... 

} 
:

<servlet> 
    ...... 
    <!-- force initialization as soon as Tomcat starts --> 
    <load-at-startup>1</load-at-startup> 
</servlet> 

<!-- configure the context listener --> 
<listener> 
    <listener-class>com.acme.MyContextListener</listener-class> 
</listener> 

MyContextListener이 같이 보입니다

관련 문제