Link to home
Create AccountLog in
Avatar of Waponi
Waponi

asked on

Spring MVC background process

Hello,
In a spring mvc env.
I have a requirement to execute a method() ,
with different parameters,
every X seconds,
indapendent of any server requests.
I know spawning a thread from a servlet is considered bad practice, so what would be the best approach to make this action automatic on start up ?
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of Waponi
Waponi

ASKER

Thanks Objects,
How does this react when multiple requests are made to a class containing an @scheduled method ?
Does spring know to only execute the annotated method thread once or does a  new scheduled method get stated on each request ?
its background processing and unrelated to any requests
if you want tasks scheduled as a result of a request then you need to scedule them from your request handler
http://helpdesk.objects.com.au/java/how-to-execute-code-at-regular-interval
Avatar of Waponi

ASKER

Objects,
Amy ideas why I would get the following error in applicationsContext.xml ?

Thanks.

For method
------------------------------------------------------------------------------------
@Scheduled(fixedRate=30000)
public void doStuff(){
System.out.println(" From scheduler JobRunnerController  Running job now....");}
------------------------------------------------------------------------------------

I have made the following new entries in applicationsContext.xml, but am getting error

cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'task:annotation-driven'.

------------------------------------------------------------------------------------

<beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:util="http://www.springframework.org/schema/util"
      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/util
    http://www.springframework.org/schema/task 
    http://www.springframework.org/schema/task/spring-task-3.0.xsd
    http://www.springframework.org/schema/util/spring-util-3.0.xsd" default-lazy-init="true">
       
<task:annotation-driven executor="myExecutor" scheduler="myScheduler"></task:annotation-driven>
<task:scheduler id="myScheduler" pool-size="10"></task:scheduler>

   
Avatar of Waponi

ASKER

objects:
For the config above I dont see any activity on the consol for the scheduler or  method annotated @Scheduled(fixedRate=30000)

Do I need to do anything like this ?
<task:annotation-driven>
<context:component-scan base-package="org.webapp.services.tasks"


Thanks.
> Do I need to do anything like this ?
> <task:annotation-driven>
> <context:component-scan base-package="org.webapp.services.tasks"

yes
Avatar of Waponi

ASKER

Added
 <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
 <context:component-scan base-package="com.cme.clearing.citps.querymanager.web"/>  
  <task:annotation-driven/>

but getting

The prefix "context" for element "context:component-scan" is not bound. ?
 
you haven't defined the context namespace in your beans tag
Avatar of Waponi

ASKER

Thanks