Showing posts with label spring. Show all posts
Showing posts with label spring. Show all posts

Sunday, May 10, 2015

Quartz Boilerplate (Spring 4) - Running Jobs with Java

I've recently had to (yet again) deal with Quartz Jobs. I don't really like the framework, but it seems that it's the best option for many job scheduling tasks. I've searched a little bit but apparently the only framework that does the job better is called Obsidian, but it has a pricing table for running on more than one node.

Now another option is to go with Java's Executors, but if you foresee having at least some degree of complexity in your scheduling and some requirements changes, I'd suggest going with something more reliable and flexible.

Spring scheduling can be pretty powerful, and does the job on number of cases, but again, if you plan on moving to DB job controlling, multiple nodes or need to fine tune your jobs execution, Quartz does have many feature to support it.

Here's a page from Quartz documentation depicting it's latest features.

I've added a base code that can be used to create a job out of the box, having just some boilerplate code for Spring 4 and Quartz to kickstart me faster should I ever need to use it again.

The most important part is contained in the Gist below and configures the three beans that represent the Job, the Trigger for it's schedule and the Scheduler itself. See the Quartz documentation for further details. This link contains all the code for it (using sbt as build tool): https://github.com/eduardohl/quartz-boiler. Code has been documented, so do a clean up if you plan on using it.

Tuesday, March 18, 2014

Environment variables and property placeholders using Spring and Tomcat

I've recently been playing on my server and then decided to remove my properties from the code. Even though I still have my properties file managed by Spring's property placeholder, I'm now using the system-properties-mode="OVERRIDE" which allows me to override the values using environment variables. It's very useful in case there are properties you want to remove from your .war (like passwords)

As I'm using Tomcat, the easiest way I found to set it was to use the setenv.sh configuration file. Just create it in Tomcat's bin file and it will be automatically read and used.

Here's an example:

spring.xml
<context:property-placeholder location="classpath:/database.properties" ignore-unresolvable="true" system-properties-mode="OVERRIDE"/>
setenv.sh
JAVA_OPTS="$JAVA_OPTS -Dmongo.db.host=<host> -Dmongo.db.port=<port> -Dmongo.db.name=<db_value> -Dmongo.db.username=<user> -Dmongo.db.password=<pwd>"
Obs: the file should be named setenv.bat on Windows

To test if it worked, after starting your Tomcat, use:
 ps aux | grep -i tomcat
You should be able to see your environments variable set!