Search code examples
oracle-databasejakarta-eeservicewebspherevs-web-application-project

Create a Service with Java that runs when the app is deoployed ?


I have a Web Application with J2EE and Spring, related to an Oracle 10g Database. I want to create a Service in Java that will poll statistics from the Database and send mail every 5 min. This Service should start when the application is deployed under Tomcat or Web-sphere. Any Ideas How this could be done ?? Thanks


Solution

  • Since use Spring, its Time execution and scheduling classes seem a natural choice. They work both in Tomcat and Websphere, just create your task as a POJO and schedule it:

    <bean id="PollingTask" class="com.sth.PollingPOJO">
             <!-- properties, if any -->
    </bean>
    
    <task:scheduler id="scheduler" pool-size="1" />
    
    <task:scheduled-tasks scheduler="scheduler">
        <!-- runs every 30 minutes -->
        <task:scheduled ref="PollingTask" method="run" fixed-delay="#{ 30*60*1000 }" />
    </task:scheduled-tasks>
    

    The PollingTask looks like (note that it doesn't have to implement Runnable, "run" method is just a convention):

    class PollingTask() {
        public void run() {
            // entry point
        }
    }