There are two ways to use Quartz on Heroku:
1. Setup your own Quartz on Heroku
2. Use Heroku Quartz Addon
1. Setup your own Quartz on Heroku
Create Postgres db:
heroku addons:add heroku-postgresql:dev
Get Postgres properties:
heroku config
Use these properties to specify your:
- org.quartz.dataSource.dataSource.URL
- org.quartz.dataSource.dataSource.user
- org.quartz.dataSource.dataSource.password
Your quartz.properties file
org.quartz.scheduler.instanceName = BatchScheduler org.quartz.threadPool.threadCount = 3 org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.PostgreSQLDelegate org.quartz.jobStore.tablePrefix = QRTZ_ org.quartz.jobStore.dataSource = dataSource org.quartz.dataSource.dataSource.driver = org.postgresql.Driver org.quartz.dataSource.dataSource.URL = jdbc:postgresql://ec2-23-21-133-106.compute-1.amazonaws.com:5432/dsqpq2m83g9jc org.quartz.dataSource.dataSource.user = YOURUSERNAME org.quartz.dataSource.dataSource.password = YOURPASSWORD org.quartz.dataSource.dataSource.maxConnections = 1
Create your Quartz tables in Postgres:
heroku pg:psql
Copy your Postgres query in the console:
Quartz tables for Quartz 2.1.6
Make sure you are using the same version of tables as specified in your pom.xml in this case my pom looks like:
<dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.1.6</version> </dependency>
2. Use Heroku Quartz Addon
Add Heroku scheduler to your project:
heroku addons:add scheduler:standard
Add the Quartz class Heroku must call in your Procfile:
scheduler: java $JAVA_OPTS -cp target/classes:target/dependency/* com.example.service.SchedulerService
Example of SchedulerService:
package com.example.service; import org.quartz.*; import org.quartz.impl.StdSchedulerFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import static org.quartz.JobBuilder.newJob; import static org.quartz.SimpleScheduleBuilder.repeatSecondlyForever; import static org.quartz.TriggerBuilder.newTrigger; public class SchedulerService { final static Logger logger = LoggerFactory.getLogger(SchedulerService.class); public static void main(String[] args) throws Exception { Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler(); scheduler.start(); JobDetail jobDetail = newJob(DatabaseJob.class).build(); Trigger trigger = newTrigger() .startNow() .withSchedule(repeatSecondlyForever(5)) .build(); scheduler.scheduleJob(jobDetail, trigger); } public static class DatabaseJob implements Job { @Override public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException { try { //Add your job that needs to execute here } catch (Exception e) { logger.error(e.getMessage(), e); } } } }
Have a look at Hangfire instead of Quartz. Hangfire has a full web interface that allows you to do so much more.