Implement Quartz for Cron Scheduling with Spring Scheduler - spring-boot

#Component
public class QuartzConfig implements Job{
#Autowired
private JobService jobService;
#Override
public void execute(JobExecutionContext context) throws JobExecutionException {
System.out.println("Check Status");
jobService.checkQueueStatus();
}
In quartz.properties file I have added this detail:
# thread-pool
org.quartz.threadPool.class=org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount=2
org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread=true
# job-store
# Enable this property for RAMJobStore
org.quartz.jobStore.class=org.quartz.simpl.RAMJobStore
How can I give the details about the job and trigger for Cron Scheduling?
Please help with the detailed flow.

There are plenty of examples on the web showing you how to integrate Quartz and Spring. You may want to check one of our test web apps available on GitHub:
https://github.com/quartzdesk/quartzdesk-test-webapps/blob/master/quartzdesk-test-quartz-v2-4-x/quartzdesk-test-quartz-v2-4-x-logback/src/main/webapp/WEB-INF/spring/applicationContext.xml
Search for "quartzScheduler" to find the Quartz scheduler bean and then scroll down to see jobDetails and triggers attributes.

Related

spring #Scheduled annotaion works on Local Websphere but does not work on Websphere installed on Severs

Spring #Scheduled(cron = "${cron expression}") works on the websphere in local machine but not on the non prod servers.
You should check the Thread pool in the different Webshere to check whats the difference I asume your problem might be there.
Else, a more complicated solution is to pass in a custom threadpool for your schedule tasks.
To do this create a new configuration class for the Scheduler and add a custom Threadpool
#Configuration
public class SchedulerConfig implements SchedulingConfigurer {
private final int POOL_SIZE = 10;
#Override
public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler();
threadPoolTaskScheduler.setPoolSize(POOL_SIZE);
threadPoolTaskScheduler.setThreadNamePrefix("my-scheduled-task-pool-");
threadPoolTaskScheduler.initialize();
scheduledTaskRegistrar.setTaskScheduler(threadPoolTaskScheduler);
}
}
You can find more references here https://www.callicoder.com/spring-boot-task-scheduling-with-scheduled-annotation/

Does a stand-by Quartz Scheduler require some kind of refresh for starting?

I'm using Quartz with Spring Boot, specifically using it's conveniences through the SchedulerFactoryBean abstraction. See https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-quartz.html.
I need to start the Scheduler manually instead, for which I think of using schedulerFactoryBean.setAutoStartup(false) and then Scheduler.start() in a class with an Scheduler instance autowired. See example code below.
#Configuration
public class QuartzConfig implements SchedulerFactoryBeanCustomizer {
public static final int STARTUP_DELAY = 5;
#Autowired
private DataSource dataSource;
#Override
public void customize(SchedulerFactoryBean schedulerFactoryBean) {
schedulerFactoryBean.setDataSource(dataSource);
schedulerFactoryBean.setStartupDelay(STARTUP_DELAY);
schedulerFactoryBean.setAutoStartup(false);
schedulerFactoryBean.setWaitForJobsToCompleteOnShutdown(TRUE);
}
}
#Service
public class SchedulerService {
private final Scheduler scheduler;
#Autowired
public SchedulerService(Scheduler scheduler) {
this.scheduler = scheduler;
}
public void startScheduler() {
this.scheduler.start();
}
}
However, there will be other instances in other nodes running on the database. My question is, does this not-started Scheduler instance refresh or becomes aware of all the work done by the other instance? Does a stand-by Quartz Scheduler require some kind of refresh manually triggered for starting?
For example, if a trigger, job or something else is added, changed or deleted will the not-started one be aware (cache refresh?) of it after the code calls .start()?
I have read several pages of quartz and I couldnt really find an aswer for this. I couldnt find a place that explains if or how an instance sincronizes itself with external changes done to its database.

Can spring provide concept like job queue

Example like I have a 25 job and i want to execute 3 job concurrent and after one of the three job complete then next one pick-up from queue.
You can do this with classes in the standard Java library - you don't need Spring for this. Use an ExecutorService, for example:
class MyJob implements Runnable {
private final String message;
MyJob(String message) {
this.message = message;
}
#Override
public void run() {
System.out.println(message);
}
}
public class Example {
public static void main(String[] args) {
// Executor service with 3 threads
ExecutorService executorService = Executors.newFixedThreadPool(3);
// Submit jobs to be executed
executorService.execute(new MyJob("testing"));
executorService.execute(new MyJob("one"));
executorService.execute(new MyJob("two"));
executorService.execute(new MyJob("three"));
// ...
}
}
Yes Spring provides support for Job scheduling through Quartz Scheduler. For more information about how Spring is using Quartz, you can go through the official spring documentation.
Apart from this, if you want some ready made example, you can go through, Spring 3 + Quartz Scheduler and Spring 4 + Quartz Scheduler.
I suggest you to use the Spring boot:
Here is a good start with scheduling using Annotation with Spring
https://spring.io/guides/gs/scheduling-tasks/
Here is a nice Introduction for Spring Boot with Quartz: http://de.slideshare.net/davidkiss/spring-boot-with-quartz
Good Luck!

Spring boot acitvemq keep receiver running periodically

I have configured a spring boot application which when run reads messages from the queue and processes them accordingly.
I also have configured the concurrency flag to run multiple such readers.
However in an ideal world i would like the receiver to keep running like a thread and keep checking for any messages.
My question is that whether there is any way i can configure this in spring boot or i have to fallback to using threading mechanism using executor or anything else.
Thanks,
- Vaibhav
I found a nice way from Spring Boot, the concurrency was of course taken case by concurrent attribute e.g.
#JmsListener(destination = "myqueue", concurrency="2-10")
However for the Thread part below was something which is a neat way:
#SpringBootApplication
#EnableAutoConfiguration(exclude={MongoAutoConfiguration.class, MongoDataAutoConfiguration.class})
#EnableJms
public class MyApplication implements CommandLineRunner{
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
#Override
public void run(String... arg0) throws Exception {
// TODO Auto-generated method stub
System.out.println("Joining Thread ctrl+c to bring down application");
Thread.currentThread().join();
}
}

Scheduled Tasks in Liferay with Autowired

im trying to create scheduled task in liferay portlet.
Liferay: 6.2
Spring: 3.1.4.RELEASE
With
<scheduler-entry>
<scheduler-description>test-scheduler</scheduler-description>
<scheduler-event-listener-class>
project.ScheduledProcesser
</scheduler-event-listener-class>
<trigger>
<simple>
<simple-trigger-value>
1
</simple-trigger-value>
<time-unit>minute</time-unit>
</simple>
</trigger>
</scheduler-entry>
and the corrensponding class
#Component
public class ScheduledProcesser implements MessageListener {
private static Log log = LogFactoryUtil
.getLog(ScheduledProcesser.class);
#Autowired
#Qualifier("myRequestService")
private RequestService service;
#Override
public void receive(Message message) throws MessageListenerException {
log.info("Starting");
Request req = service.get("AAA746");
if (req!=null)
log.info("REQ -" + req.getId());
log.info("Finished");
}
The method is firing. But the service component is null. Normally is the service in other parts working well.
I have tried to find the solution, but maybe there is some settings missing.
Thanx,
MessageListener is not instantiated by Spring, but by Liferay (see the implementation of QuartzSchedulerEngine.getMessageListener(String, ClassLoader)). And Liferay just instantiates the class. So you can't autowire anything into a MessageListener that is defined in the liferay-portlet.xml.
But you could use PortalBeanLocatorUtil.locate instead, if your service is defined in the portal application context.

Resources