user1538526
user1538526

Reputation: 95

Refreshing application context after every 2 minutes

I have a query is that I want my application context to be get refreshed after every 2 minutes..rite now I am getting the application context in my application ..

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

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

Please advise how to refresh the application context in every 2 minutes

Upvotes: 0

Views: 1848

Answers (2)

Rahul Agrawal
Rahul Agrawal

Reputation: 8971

Refer this link

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

Try this

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) {
        }
    }
}

Upvotes: 2

kosa
kosa

Reputation: 66637

One of the way would be calling a thread with sleep time. Something like below

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

Upvotes: 0

Related Questions