My problem is that I have a function implemented on my website which searches for Particular Tweet when I press the button. I want it to make it automatic such that, that function is called again and again after every two minutes, regardless some one uses the website or not.. How to do this in java?? Spring Solution is better
You could use Spring scheduling (see documentation here)
From the doc:
XML config
<context:component-scan base-package="some.package" />
<task:annotation-driven executor="myExecutor" scheduler="myScheduler"/>
<task:executor id="myExecutor" pool-size="5"/>
<task:scheduler id="myScheduler" pool-size="10"/>
Java bean
#Component
public class SomeBean {
#Scheduled(fixedRate=5000)
public void doSomething() {
// something that should execute periodically
}
}
Related
I have problem with asynchronus execution of my code.
Method startConversion() should be called asynchronusly. call goes trought AOP proxy.
Everything separated works fine. Problem occurs when I put together #Async and #MonitoredWithSpring annotations
javamelody is defined in webxml in first xml - OK
Spring async support is defiend in xml - OK
Code works well without #MonitoredWithSpring. But i need this bean to be monitored.
Sample code:
#MonitoredWithSpring //L1 - if this line removed #async will work
public interface IOfficeConversionService {
void startConversion ();
}
Implementing class
#Service
public class COfficeConversionSevice implements IOfficeConversionService {
#Override
#Transactional
#Async ("officeFileConversionExecutor")
public void startConversion () {
//ASYNC work
}
}
With L1 present, code will call method startConversion() synchronusly.
Without L1 everything works fine, and method startConversion() is called asynchronusly in new thread
In stacktrace don't even create async pointcut.
#Async annotation is never processed by its postProcessor. #Transactional works, and transaction is created. No error in initialization
Spring configuration
<context:annotation-config />
<aop:aspectj-autoproxy />
<task:executor id="mailexecutor" pool-size="25-100" queue-capacity="1000" rejection-policy="ABORT" />
<task:executor id="pnpexecutor" pool-size="5" />
<task:executor id="officeFileConversionExecutor" pool-size="5" />
<task:scheduler id="systemScheduler" pool-size="5" />
<task:annotation-driven executor="mailexecutor" scheduler="systemScheduler"/>
Can you help me? Some suggestion ?
I am out of ideas.
I am working on a Spring-MVC application in which I want to use #Async at-least for the methods which are fire-and-forget. When I try to use #Async and I have used #EnableAsync annotation too for class, the actions inside the method are not performed. When I add task executor in servlet-context.xml, then I get an error bean is getting currently created. I am new to Async, can anyone tell me how I can use it.
I am not using Eager loading btw.
Error log :
Caused by: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'groupNotesService': Bean with name 'groupNotesService' has been injected into other beans [mattachService] in its raw version as part of a circular reference, but has eventually been wrapped. This means that said other beans do not use the final version of the bean. This is often the result of over-eager type matching - consider using 'getBeanNamesOfType' with the 'allowEagerInit' flag turned off, for example.
Code :
GroupNotesServiceImpl :
#Service
#Transactional
#EnableAsync
public class GroupNotesServiceImpl implements GroupNotesService {
#Override
#Async
public void editGroupNote(GroupNotes mnotes, int msectionId) {
//Code to be executed, which is not getting executed
}
}
Servlet-context.xml :
<task:annotation-driven executor="executor" />
<task:executor id="executor" pool-size="20"/>
<mvc:annotation-driven />
<mvc:default-servlet-handler />
<resources mapping="/resources/" location="/resources/" />
If I remove any of the mvc lines above, I get a servlet.init() threw load exception error.
Also, Is it possible to use Async where I am returning int? I checked out the Future tag, but I don't know what modifications are required.
Here is the method that returns int.
#Override
public int saveGroupNoteAndReturnId(GroupNotes mnotes, int msectionid) {
// saves note and returns its id.
}
MattachService bean :
<beans:bean id="mattachDAO"
class="com.journaldev.spring.dao.GroupAttachmentsDAOImpl">
<beans:property name="sessionFactory"
ref="hibernate4AnnotatedSessionFactory" />
</beans:bean>
<beans:bean id="mattachService"
class="com.journaldev.spring.service.GroupAttachmentsServiceImpl">
<beans:property name="groupAttachmentsDAO" ref="mattachDAO" />
</beans:bean>
Edit
I checked out that there is a problem to run #Transactional and #Async both in one class. Jira SPR-7147. The workaround suggested there was to introduce a normal facade, and I really don't know what that means.
#EnableAsync should be in configuration, not the service itself. Since you seem to use xml configuration, check this https://stackoverflow.com/a/20127051/562721. It recommends to declare org.springframework.scheduling.annotation.AsyncAnnotationBeanPostProcessor to take care of #Async annotations
I have defined various scheduler in my configuration file as follows:
<task:executor id="xxxxxExecutor" pool-size="${async.executor.pool.size}"/>
<task:scheduler id="xxxxwwwScheduler" pool-size="1" />
<task:scheduler id="qqqqSchedular" pool-size="1" />
<task:scheduler id="lastScheduler" pool-size="1" />
My controller has been annotated via #Controller annotation. How do I specify a particular Scheduler in #Scheduled annotation in Spring?
p.s. I am trying to schedule a method in a controller using #Scheduled annotation.
Using multiple schedulers and pointing to them via the #Scheduled annotation is unfortunately not possible.
However, if you really do need that flexibility, you can define the jobs in XML:
<task:scheduled-tasks scheduler="myScheduler">
<task:scheduled ref="beanA" method="methodA" fixed-delay="5000"/>
</task:scheduled-tasks>
That allows you to specify the exact id of the scheduler you need to use, and then simply reference the actual task.
Hope this helps.
I have a class MessageProcessor being called by another method in another class (i.e. Caller).
public class Caller {
#Scheduled(filxedDelay=10)
public void poll(){
//do stuff
messageProcessor.process(msg);
}
}
public class MessageProcessor{
#Async(value="abcExecutor")
public void process(String msg){
//do stuff here.
}
}
Spring file looks like:
<task:executor id="abcExecutor" pool-size="9" rejection-policy-"CALLER_RUNS"/>
I want to add another #Async executor:
#Async(value="defExecutor")
public void remove(String msg){
//do stuff here.
}
#Scheduled(filxedDelay=10)
public void kill(){
//do stuff
messageProcessor.remove(msg);
}
By adding another executor in spring file:
<task:executor id="defExecutor" pool-size="9" rejection-policy="CALLER_RUNS"/>
But how to add multiple executors in <task:annotation-driven executor="abcExecutor" scheduler="scheduler" mode="proxy" proxy-target-class="true"/>
How can I make these multiple executors run with annotation?
PS: Obviously, I don't want to have the same pool being used for both the
#Async methods
The #Async("defExecutor") is sufficient to designate the method to be handled by the 2nd executor. The xml declaration specifies only the default executor, which will be used whenever no value is specified in #Async.
See the explanation of Chris Beams in this issue:
So, is it a right XML ?
<task:executor id="abcExecutor" pool-size="9" rejection-policy-"CALLER_RUNS"/>
<task:executor id="defExecutor" pool-size="9" rejection-policy="CALLER_RUNS"/>
<task:annotation-driven executor="abcExecutor" scheduler="scheduler" mode="proxy" proxy-target-class="true"/>
Within my web application, I am trying to create a directory polling bean using Java SDK7 WatchService. What I would like to achieve is to run this bean in its own thread so that it does not block the application. Something like:
<bean id="directoryPoller" class="org...MyDirectoryPoller" scope="thread"/>
I am afraid you will have to create this thread manually with Spring:
<bean id="pollThread" class="java.lang.Thread" init-method="start" destroy-method="interrupt">
<constructor-arg ref="watchServiceRunnableWrapper"/>
</bean>
<bean id="watchServiceRunnableWrapper" class="WatchServiceRunnableWrapper">
<constructor-arg ref="watchService"/>
</bean>
<bean id="WatchService" class="java.nio.file.WatchService" destroy-method="close"/>
The WatchServiceRunnableWrapper is simple:
public class WatchServiceRunnableWrapper implements Runnable {
private WatchService WatchService;
public WatchServiceRunnableWrapper(WatchService watchService) {
this.watchService = watchService;
}
public void run() {
watchService.poll();
//
}
}
I haven't tested it, but it more-or-less should work and shutdown gracefully.
I'm not familiar with Java 7's WatchService, but you could use Springs' scheduling support for this. Here's yet another tutorial and googling for something like Spring Scheduled probably finds loads more.