Skip to main content

Java Quartz with Spring

Quartz is a full-featured, open source job scheduling system that can be integrated with, or used along side virtually any J2EE or J2SE application - from the smallest stand-alone application to the largest e-commerce system. Quartz is freely usable, licensed under the Apache 2.0 license.
So to configure Quartz with Sring you should first create plain POJO class that will do the job that you want to schedule. Next in spring configuration (applicationContext.xml) you should add your bean:

1
2
3
4
<bean id="onTimeServiceImpl" class="com.company.DatabaseSetupServiceImpl">
  <constructor-arg ref="companyService" />
  <property name="xmlFolderPath" value="${xml.target}" />
</bean>

OnTimeService just uses some other service (company) and some xml folder path, but you can defined your own servoces or remove this lines.
then you should add quartz job method invoking and specify target obj and target method so OnTimeServiceImpl must have that method "fullReload".

1
2
3
4
<bean id="reloadDatabaseJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
  <property ref="onTimeServiceImpl" name="targetObject" />
  <property name="targetMethod" value="fullReload" />
</bean>

after that define quartz triger for example we used cron triger

1
2
3
4
<bean id="reloadDatabaseTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
  <property name="jobDetail" ref="reloadDatabaseJob" />
  <property name="cronExpression" value="0 0 6 * * ?"/>
</bean>

and finally schedule your trigered
1
2
3
4
5
6
<bean id="schQuartz" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
  <property name="triggers" >
  <list >
    <ref bean="reloadDatabaseTrigger" />
  </list>
</bean>

and that is that.
Problem that you can also face is that you need to run the same functionality that you schedule from your web application but in separate tread. We used and for this purpose quartz.
Define in your action (struts 2) scheduler factory bean
/**
* Handle for scheduler.
*/
private SchedulerFactoryBean schQuartz;

inject it with spirng with this spring cconfig
<bean id="adminAction" class="com.company.AdminAction" scope="prototype">
<constructor-arg ref="schQuartz" />
</bean>
and in action
/**
* Constructor.
*
* @param service
* Handle to the index service (injected via spring).
* @param schQuartz
* Handle to the scheduler for (re-)populating the db.service.
*/
public AdminAction (SchedulerFactoryBean schQuartz) {

this.schQuartz = schQuartz;
}


/**
* Trigger a load in DB from xmls.
*
* @return The action forward - back to the admin page
*/
public String rebuildDatabase() {

StdScheduler std = (StdScheduler) schQuartz.getObject();
try {
std.triggerJob("reloadDatabaseJob", "DEFAULT");
} catch (SchedulerException se) {
String msg = "Error triggering database reload";
log.error(msg, se);
throw new RuntimeException? (msg, se);
}
return execute();
}

And that is all.

Comments

Popular posts from this blog

Javascript REST client

Note: I still work on text, code example should be fine.



REST is the one of the most popular interfaces on the web today. One part to its success it owes to its simplicity.

How number of sites that support REST grows we will need some fast and good solution to use those services on client side. Even if its possible to do it all, since REST is based on http, with old AJAX calls, it would be nice to have some little more...

This is one approach that i choose. I create in javascript new object that has six methods five for REST methods, four for POST, PUT, GET and REMOVE plus one more to GET all resources, and one more to get html template to display your data.

This code is based on jquery and json js libs.

function RestServiceJs(newurl) { this.myurl = newurl; this.add = function(model, callback) { $.ajax({ type: 'POST', url: this.myurl, data: JSON.stringify(model), // '{"name":"' + model.name + '"}', dataType: …

Use JPA with MongoDb and Datanucleus

Web applications nowadays have huge demand for data processing, and they need to get them, process them, and displayed them fast. Traditional relational tables and big SQL engines fail to serve that purpose.

NoSQL movement is on the rise. There are huge numbers of alternatives to SQL like BigTable, HBase, CouchDB, Cassandra and MongoDB. They are all fast but you'll need to implement new way to work with them, since all of them have they own query languages. It would be nice that we can use them in similar or same way in our projects, after all ORM layer is there for that to decouple our objects from underlying storage.

MongoDB is one of most popular NoSQL solutions and easiest one to install. For HBase i would had to install cygwin or virtual linux, and still will be hard to confgiure it right. MongoDb is easy, it plays out of the box, just unzip it, and it still offers some nice features like MapReduce, Replication and Sharding.

Datanucleus is one of the leading ORM providers …

Is Java the right language for Micro Services?

[work in progress]
Micro Server for Micro services

Micro services are becoming slowly but steady mainstream programming architecture. People often compare them to web services or rest services witch gives more problems than understanding. And when you look at implementation you'll see that developers are running away from Java to Scala, Nodejs or Python or name it. Is Java old? Is it time to close Java book and learn something new? Learning new things is always good, but I think reason why developers run away from Java is not a language problem, but Java legacy.

Biggest problem why developers avoid Java to make micro services is that there are not so many good tools and servers for micro services in Java or put it better there are many old wrong tools and server to start with. If you are old Java developer you will have problems to grasp this.

When you start to create micro service your focus should be on micro. Project should compile in less than 10s.Your service should have onl…