2012-08-01 73 views
0

我有一个查询是,我想我的应用程序上下文中得到刷新,每2 minutes..rite现在我得到我的应用程序的应用程序上下文后..刷新应用程序上下文每隔2分钟后

public class App { 
    public static void main(String[] args) { 
     ApplicationContext context = new ClassPathXmlApplicationContext(
       "Spring-Module.xml"); 

     HelloWorld obj = (HelloWorld) context.getBean("helloBean"); 
     obj.printHello(); 
    } 

请告知如何每2分钟刷新应用程序上下文

回答

0

其中一种方式是调用一个具有睡眠时间的线程。像下面

  for (int i = 0;i < howmanytime;i++) { 
      //Pause for 2 seconds 
      Thread.sleep(2000); 
      //Your logic 
     } 
+0

它应该是的Thread.sleep(120000)2分钟(如要求由OP )? – Manish 2012-08-01 04:23:10

+0

@Manish。是。睡眠方法接受以毫秒为单位的时间。 – kosa 2012-08-01 04:28:42

2

请参阅此链接的东西

http://hsenidians.blogspot.in/2007/07/reloading-spring-context-dynamically.html
http://techdive.in/spring/spring-refresh-application-context

试试这个

public class RefreshSpringContext { 

    public static void main(String args[]) { 
     SpringThread t = new SpringThread(); 
     new Thread(t).start(); 
    } 
} 

class SpringThread implements Runnable { 

    public SpringThread() { 
    } 

    public void run() { 
     try { 
      ApplicationContext context = = new ClassPathXmlApplicationContext("Spring-Module.xml"); 
      ((ConfigurableApplicationContext) context).refresh(); 
      Thread.sleep(12000); 

      HelloWorld obj = (HelloWorld) context.getBean("helloBean"); 
      obj.printHello(); 
     } catch (Exception e) { 
     } 
    } 
} 
相关问题