Announcement Announcement Module
Collapse
No announcement yet.
How to schedule a single task with spring? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to schedule a single task with spring?

    hi,

    I would like to schedule a job to run by a cron expression. What I would like to do is to see a single log entry when triggered. I am simply unable to construct a working example on gf.

    Complete source, web.xml:
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="3.0" 
    	xmlns="http://java.sun.com/xml/ns/javaee" 
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    	http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
      <display-name></display-name>	
      
      <context-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>/WEB-INF/applicationContext.xml</param-value>
      </context-param>
      <listener>
          <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>
    
      <session-config>
          <session-timeout>30</session-timeout>
          <cookie-config>
               <name>JSESSIONID_GPD</name>
           </cookie-config>
      </session-config>
      <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>
        
    </web-app>
    applicationContext.xml:
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans 
        xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:p="http://www.springframework.org/schema/p"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:context="http://www.springframework.org/schema/context" 
        xmlns:jee="http://www.springframework.org/schema/jee"
        xmlns:task="http://www.springframework.org/schema/task"
        xsi:schemaLocation="http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                            http://www.springframework.org/schema/aop     http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
                            http://www.springframework.org/schema/tx      http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
                            http://www.springframework.org/schema/jee     http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
                            http://www.springframework.org/schema/task    http://www.springframework.org/schema/task/spring-task-3.0.xsd">
                            
        
        
        
        
        <!-- Transaction manager for  JTA  -->
        <bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager" />
        <!-- tx:jta-transaction-manager /-->
        
        <!--
        Instruct Spring to perform declarative transaction management
        automatically on annotated classes.
        -->
        <tx:annotation-driven />
        <!--
        Activates various annotations to be detected in bean classes: Spring's
        @Required and @Autowired, as well as JSR 250's @PostConstruct,
        @PreDestroy and @Resource (if available) and JPA's @PersistenceContext
        and @PersistenceUnit (if available).
        -->
        <context:annotation-config/>
    
    
        <!-- Scan for Service annotations -->
    	<context:component-scan base-package="my.jobs"/>
    
    
        <task:scheduler id="scheduler" pool-size="5"/>
        <task:annotation-driven></task:annotation-driven>
    
    </beans>
    and the single class file in WEB-INF/lib/my.jar:
    Code:
    package my.jobs;
    
    import java.util.*;
    
    import org.springframework.context.annotation.Scope;
    import org.springframework.scheduling.annotation.Scheduled;
    import org.springframework.stereotype.Component;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;
    
    @Transactional
    //@Component @Scope("singleton")
    @Service
    public class TestJob {
    	
    	static private boolean isRunning;
    	
    	TestJob() {
    		TestJob.isRunning=false;
    	}
    
    	@Scheduled(cron = "0 0/2 * * * ?")
    	public void doJob(){
    		Random rnd = new Random();
    		String msg = "executing job: start testJob)..." +(Calendar.getInstance().getTimeInMillis())+", rnd="+rnd.nextInt(100);
    		
    		if (isRunning) {
    			msg += " NOT STARTED as aleardy running!";
    		} else {
    			TestJob.isRunning=true;
    			msg += " starting...";
    		}
    
    		System.out.println(msg);
    	}
    
    }
    after undeploy, and do a clean deployment, the log shows this:
    Code:
    [#|2013-07-26T09:49:57.133+0200|INFO|sun-appserver2.1|javax.enterprise.system.tools.deployment|_ThreadID=256;_ThreadName=Thread-141;|deployed with moduleid = gpd-gf|#]
    
    [#|2013-07-26T09:49:57.251+0200|WARNING|sun-appserver2.1|javax.enterprise.system.tools.admin|_ThreadID=255;_ThreadName=Thread-140;_RequestID=ed1d2a53-2f51-44b1-8c82-4629c254b76e;|Not registering AMX MBean against old MBean "com.sun.appserv:j2eeType=WebModule,name=gpd-gf,J2EEServer=server,J2EEApplication=null,category=runtime" due to malformed composite WebModule name.|#]
    
    [#|2013-07-26T09:50:11.175+0200|INFO|sun-appserver2.1|javax.enterprise.system.tools.admin|_ThreadID=128;_ThreadName=httpWorkerThread-4894-3;ModuleDeployEvent -- enable web/gpd-gf;|ADM1041:Sent the event to instance:[ModuleDeployEvent -- enable web/gpd-gf]|#]
    
    [#|2013-07-26T09:50:11.229+0200|INFO|sun-appserver2.1|javax.enterprise.system.container.web|_ThreadID=128;_ThreadName=httpWorkerThread-4894-3;|PWC1412: WebModule[/gpd-gf] ServletContext.log():Initializing Spring root WebApplicationContext|#]
    
    [#|2013-07-26T09:50:11.319+0200|INFO|sun-appserver2.1|javax.enterprise.system.container.web|_ThreadID=128;_ThreadName=httpWorkerThread-4894-3;|PWC1412: WebModule[/gpd-gf] ServletContext.log():Initializing Spring root WebApplicationContext|#]
    
    [#|2013-07-26T09:50:11.405+0200|INFO|sun-appserver2.1|javax.enterprise.system.container.web|_ThreadID=128;_ThreadName=httpWorkerThread-4894-3;|PWC1412: WebModule[/gpd-gf] ServletContext.log():Initializing Spring root WebApplicationContext|#]
    
    [#|2013-07-26T09:50:11.491+0200|INFO|sun-appserver2.1|javax.enterprise.system.container.web|_ThreadID=128;_ThreadName=httpWorkerThread-4894-3;|PWC1412: WebModule[/gpd-gf] ServletContext.log():Initializing Spring root WebApplicationContext|#]
    
    [#|2013-07-26T09:50:11.577+0200|INFO|sun-appserver2.1|javax.enterprise.system.container.web|_ThreadID=128;_ThreadName=httpWorkerThread-4894-3;|PWC1412: WebModule[/gpd-gf] ServletContext.log():Initializing Spring root WebApplicationContext|#]
    
    
    
    
    
    
    [#|2013-07-26T09:52:00.001+0200|INFO|sun-appserver2.1|javax.enterprise.system.stream.out|_ThreadID=271;_ThreadName=scheduler-1;|
    executing job: start testJob)...1374825120000, rnd=0 starting...|#]
    
    [#|2013-07-26T09:52:00.001+0200|INFO|sun-appserver2.1|javax.enterprise.system.stream.out|_ThreadID=269;_ThreadName=scheduler-1;|
    executing job: start testJob)...1374825120000, rnd=19 starting...|#]
    
    [#|2013-07-26T09:52:00.001+0200|INFO|sun-appserver2.1|javax.enterprise.system.stream.out|_ThreadID=267;_ThreadName=scheduler-1;|
    executing job: start testJob)...1374825120000, rnd=32 starting...|#]
    
    [#|2013-07-26T09:52:00.003+0200|INFO|sun-appserver2.1|javax.enterprise.system.stream.out|_ThreadID=273;_ThreadName=scheduler-1;|
    executing job: start testJob)...1374825120002, rnd=10 starting...|#]
    
    [#|2013-07-26T09:52:00.003+0200|INFO|sun-appserver2.1|javax.enterprise.system.stream.out|_ThreadID=264;_ThreadName=scheduler-1;|
    executing job: start testJob)...1374825120001, rnd=88 starting...|#]
    
    
    
    
    
    
    
    [#|2013-07-26T09:54:00.000+0200|INFO|sun-appserver2.1|javax.enterprise.system.stream.out|_ThreadID=273;_ThreadName=scheduler-1;|
    executing job: start testJob)...1374825240000, rnd=90 NOT STARTED as aleardy running!|#]
    
    [#|2013-07-26T09:54:00.000+0200|INFO|sun-appserver2.1|javax.enterprise.system.stream.out|_ThreadID=264;_ThreadName=scheduler-1;|
    executing job: start testJob)...1374825240000, rnd=40 NOT STARTED as aleardy running!|#]
    
    [#|2013-07-26T09:54:00.001+0200|INFO|sun-appserver2.1|javax.enterprise.system.stream.out|_ThreadID=271;_ThreadName=scheduler-1;|
    executing job: start testJob)...1374825240001, rnd=80 NOT STARTED as aleardy running!|#]
    
    [#|2013-07-26T09:54:00.001+0200|INFO|sun-appserver2.1|javax.enterprise.system.stream.out|_ThreadID=269;_ThreadName=scheduler-1;|
    executing job: start testJob)...1374825240000, rnd=38 NOT STARTED as aleardy running!|#]
    
    [#|2013-07-26T09:54:00.001+0200|INFO|sun-appserver2.1|javax.enterprise.system.stream.out|_ThreadID=267;_ThreadName=scheduler-1;|
    executing job: start testJob)...1374825240000, rnd=6 NOT STARTED as aleardy running!|#]


    Any idea why servlet and test-job triggered 5 times?



    Sorry creating this thread, but my earlier one does not lead to any solution: http://forum.springsource.org/showth...f-this-feature

    thx again
    Last edited by pogacsa; Jul 26th, 2013, 03:00 AM.

  • #2
    upd: tested during the day on gf2.1 on OSX, running one thread. But all linux glassfish versions running multiple. gf 2.1, gf 2.1.1 starts 5 times. Gf 3.1.2 "only" starts 4.

    Is this normal???

    Comment

    Working...
    X