Run Java Method from Time to Time - Run Periodically

2

How do I execute a method of a Java class from x in x time in WebLogic?

From what I've been checking, there's the possibility of using the @Schedule annotation in the (Java EE 6) method. But as I'm using Java EE 5, this annotation does not work. Is there another way to do without the @Schedule annotation?

    
asked by anonymous 02.06.2015 / 11:59

1 answer

1

I've had to do something very similar to what you're requesting. Within the WebLogic 10.3 application server of 10 in 10 seconds connect to a JMS Topic and consume messages from this topic. I had to do this within the WebLogic server, and I did not use features directly from the Java EE platform, but from SE (Java SE).

Well, there are two ways, first in Startup of your application to create a callback

or

In EJB that will call the method that initializes, if you have one. You create a method with the @PostContruct annotation , this method will be called callback from creating your EJB .

In both the callback of the WebLogic Startup and in the creation of the EJB you create a Pool with a Thread or more that will execute your logic of accessing services or executing your business methods. This Pool is a specialized pool that will periodically perform its tasks, here's the example:

public static void main(String... args){

ScheduledExecutorService scheduledExecutorService =
        Executors.newScheduledThreadPool(1);

ScheduledFuture scheduledFuture =
    scheduledExecutorService.schedule(new Callable() {
        public Object call() throws Exception {
            System.out.println("Executed!");
            return "Called!";
        }
    },
    10,
    TimeUnit.SECONDS);
}

In the code above a Pool is created with a Thread, and a task, which in this case is a Callable instance - Running every 10 seconds.

Consider the shutdown method

In the case of EJB, you can define a preDestroy method, which should be annotated with the @PreDestroy annotation - so you can stop execution. To do this, use the Shutdown method

scheduledExecutorService.shutdown();

This is the Executors Framework, a powerful Java Platform Framework, added in version 1.5 SE.

Please read the Class Javadocs: link

    
08.06.2015 / 02:47