DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • How To Create Asynchronous and Retryable Methods With Failover Support
  • How to Set Up Magento 2 Cron Job
  • Clustered Quartz Scheduler With Spring Boot and MongoDB
  • Adding a RESTful API for the Quartz Scheduler

Trending

  • GenAI-Infused ChatGPT: A Guide To Effective Prompt Engineering
  • The Convergence of Testing and Observability
  • Spring WebFlux Retries
  • Microservices With Apache Camel and Quarkus (Part 5)
  1. DZone
  2. Software Design and Architecture
  3. Performance
  4. Keeping Your Balance: Load Balancing Quartz Cron Jobs With Red Hat JBoss Fuse

Keeping Your Balance: Load Balancing Quartz Cron Jobs With Red Hat JBoss Fuse

Want to load balance quartz Cron Jobs with Red Hat's JBoss Fuse? Here's how.

Christina Lin user avatar by
Christina Lin
CORE ·
Sep. 24, 15 · Tutorial
Like (5)
Save
Tweet
Share
6.94K Views

Join the DZone community and get the full member experience.

Join For Free

In the beginning of the year I posted a post on how to do cron jobs with Apache Camel in JBoss Fuse, the reason I am doing it is because sometimes we need to off-load the integration jobs and schedule them to run in less time during busy hours. Cron Jobs allows you to run the integration application at the certain period of time you set it to run, it will be mostly in the night time. We use the Quartz2 component in camel to achieve this.

Just to recap a little on the scenario I had. Basically it starts up two instance that runs the same cron jobs, the job simply writes a file to an FTP server. The job is implemented by Quartz2 and setting it to run twice every minute,both instances are connected to a database (In my code I used H2 for easy installation, but there is another option to use PostgreSQL database, see my previous blog). The reason we want to connect them to database is to make sure these two instance will share the loading of the the job. This is very useful if you have a long running job, and can divide them into different parts and share the loading among distributed resources.

Don't forget to setup the database beforehand, setting the database table schema, download the Quartz library, you will find the scripts to setup tables are under docs/dbTables, find the sql file of the database you are using. Run the sql file to create all the tables needed in your database.



And let's go back to the camel project itself. The setting of the database are in the properties.

<bean id="quartz2" class="org.apache.camel.component.quartz2.QuartzComponent">
    <property name="propertiesFile" value="org/blogdemo/demojob/myquartz.properties"/>

</bean>

Since we are going to deploy this with fabric, we will place the properties file under main/fabric

Inside myquartz.properties contains detail about the shared database such as driver, ID , password and also the setup detail like, number of threads, driverDelegateClass used and most importantly if this setting is clustered.

# Main Quartz configuration
org.quartz.scheduler.skipUpdateCheck = true
org.quartz.scheduler.instanceName = DatabaseClusteredScheduler
org.quartz.scheduler.instanceId = AUTO
org.quartz.scheduler.jobFactory.class = org.quartz.simpl.SimpleJobFactory
org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.StdJDBCDelegate
org.quartz.jobStore.dataSource = quartzDataSource
org.quartz.jobStore.tablePrefix = QRTZ_
org.quartz.jobStore.isClustered = true
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 10
org.quartz.jobStore.clusterCheckinInterval = 20000


# JobStore: JDBC jobStoreTX
org.quartz.dataSource.quartzDataSource.driver = org.h2.Driver
org.quartz.dataSource.quartzDataSource.URL = jdbc:h2:file:~/h2/cronjob;AUTO_SERVER=TRUE
org.quartz.dataSource.quartzDataSource.user = sa
org.quartz.dataSource.quartzDataSource.password = 

org.quartz.dataSource.quartzDataSource.maxConnections = 5

The camel route is very simple and easy, 

<route id="quartzRoute">
        <from uri="quartz2://myGroup/myTimer?cron=0/45+*+*+*+*+?"/>
        <setHeader headerName="CamelFileName">
            <simple>MyJob-${date:now:yyyyMMdd HH:mm:ss}.txt</simple>
        </setHeader>
        <setBody>
            <simple>${date:now:yyyyMMdd HH:mm:ss} Hello This is the content from Quartz2 - {{instancename}}</simple>
        </setBody>
        <to uri="sftp://demo@localhost?password=ZAQ!2wsx"/>
    </route>

Basically, it is kicked of by the cron job, on every 0 and 45 second, and then creates a file under name MyJob-YYYYMMDDHHMMSS.txt with content Hello This is the content from Quartz2 in the file. 

And to deploy the application to JBoss Fuse, I need to create a fragment bundle for connection pooling, here we use c3p0 and the JDBC driver. And then deploy both onto the same instance. 




Deploy another container with the same job profile and fragment bundle. 



Add another profile,  that prints the content received in FTP server. 





If you encounter any problem when deploying, it's probably OSGi not loading the bundles correctly, you can simply restart the container or make sure the following bundles are activated in the container. (Check by using osgi:list) 
[ 157] [Active     ] [            ] [       ] [   80] Apache ServiceMix :: Bundles :: c3p0 (0.9.1.2_1), Fragments: 148
[ 167] [Active     ] [            ] [       ] [   80] H2 Database Engine (1.4.181)
[ 172] [Active     ] [Created     ] [       ] [   80] Quartz Two Demo (1.0.0.SNAPSHOT)


Take a look at the log running quartzprint profile, you will find the jobs are clustered and loads are shared between two nodes. 




And detail of the job can also be found in console of each container.



You can find the Demo code and detail instructions here. 



career Database engine Crons Load balancing (computing) JBoss Quartz (scheduler)

Published at DZone with permission of Christina Lin, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • How To Create Asynchronous and Retryable Methods With Failover Support
  • How to Set Up Magento 2 Cron Job
  • Clustered Quartz Scheduler With Spring Boot and MongoDB
  • Adding a RESTful API for the Quartz Scheduler

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: