Category Archives: Spring Integration

Quartz Cron with Spring Framework

Quartz

Quartz is an open source for job scheduling wich may be easily plugged in any working java project.

  • Step 1

In the applicationContext.xml, add the job details (class and target method). Target method should be a public method without parameters.


<bean name="aBackgroundJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" class="net.slm.BackgroundWorkerClass"/>
<property name="targetMethod" value="methodToCallOnQuartzEvent"/>
<!-- This job maybe concurrent (default) or exclusive -->
<property name="concurrent" value="false"/>

 </bean>

  • Step 2

Now, there are two ways to run this job using Quartz library, either a simple cron scheduled after server start and having a repeat interval (simpleTrigger) :


<bean id="dailyCronTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail" ref="aBackgroundJob"/>
<!-- 2 minutes after server starting -->
<property name="startDelay" value="120000"/>
<!-- repeat every 24 hours (1000*60*60*24)-->
<property name="repeatInterval" value="86400000"/>
 </bean>

The second option is to use quartz specific regular expressions to schedule fire time. Those regular expressions are explained in details here.


<bean id="dailyCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="aBackgroundJob"/>
<!-- run every day at 9:30 AM -->
<property name="cronExpression" value="0 30 9 * * ?"/>
<property name="concurrent" value="false"/>
 </bean>

  • Step 3

To get those events fired, you must subscribe crons to a schedular:


<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="dailyCronTrigger" />
</list>
</property>
 </bean>

  • Step 4

Some java code for the job.


package net.slm;

import java.util.Date;

public class BackgroundWorkerClass {

public void methodToCallOnQuartzEvent(){
System.out.println("Quartz job started at "+new Date());
//or do whatever you want
}

}

There are similar libraries here but I think that quartz is the richest.

The annoying transaction journey

When moving to production environment, I encoutered some problems with a specific patch of IBM WAS (7.0.0.25). Then I discovered it was not really related to that particualr patch because that makes no sens. Later in the same week, when we went back to development environment,  we encountered the same errors… That was a bit wierd because the whole application was tested for more than one month without throwing anyone of those errors. Error List contains:

  • org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
  • java.lang.IllegalArgumentException: Cannot convert value of type […] no matching editors or conversion strategy found
  • org.hibernate.TransactionException: Transaction not successfully started
  • [BlazeDs] serilization: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role

At the same time, I discovered prezi and I wanted to test it. Here’s the result*:

 

* To integrate prezi presentation, please go here.

Spring/Hibernate Search on WAS 7: The Impossible Jar Combination



Instead of spending hours and hours in jars conflicts resolution, I suggest this combination which is working under Websphere:

Sping Framework 2.5.6 (with dependencies)
Hibernate 3.6.8
Hibernate Search 3.4.1
Lucene 3.1

This might be insignificant at first sight, but once you start getting

Error creating bean with name ‘propertyConfigurer’ defined in ServletContext resource [/WEB-INF/classes/webApplicationContext.xml]: Initialization of bean failed; nested exception is java.lang.NullPointerException (Solution)

and

Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: org.hibernate.annotations.common.reflection.MetadataProvider(Solution)

you will start focusing on this listing :

antlr-2.7.6.jar
antlr-runtime-3.0.jar
aopalliance.jar
apache-solr-core-1.4.0.jar
asm-2.2.3.jar
asm-attrs.jar
asm.jar
backport-util-concurrent.jar
c3p0-0.9.1.jar
cfgatewayadapter.jar
cglib-2.2.jar
cglib-nodep-2.1_3.jar
commons-beanutils-1.8.3.jar
commons-beanutils-1.8.3-sources.jar
commons-beanutils-bean-collections-1.8.3.jar
commons-beanutils-core-1.8.3.jar
commons-codec-1.3.jar
commons-collections-3.1.jar
commons-dbcp.jar
commons-digester-2.0.jar
commons-digester-2.0-sources.jar
commons-discovery-0.4.jar
commons-fileupload-1.2.1.jar
commons-httpclient-3.0.1.jar
commons-io-1.4.jar
commons-lang.jar
commons-logging1.1.jar
commons-pool.jar
compass-2.2.0.jar
concurrent.jar
dom4j-1.6.1.jar
ehcache-1.2.3.jar
flex-messaging-common.jar
flex-messaging-core.jar
flex-messaging-opt.jar
flex-messaging-proxy.jar
flex-messaging-remoting.jar
hibernate3.jar
hibernate-commons-annotations-3.2.0.Final.jar
hibernate-core-3.6.7.Final.jar
hibernate-entitymanager.jar
hibernate-search-3.4.1.Final.jar
iText-2.1.7.jar
jasperreports-3.7.2.jar
jasperreports-applet-3.7.2.jar
jasperreports-fonts-3.7.2.jar
jasperreports-javaflow-3.7.2.jar
javassist-3.9.0.GA.jar
javassist.jar
javax.persistence-2.0.0.jar//under websphere should be moved somewhere else (see http://bit.ly/JprRim)
jdbc2_0-stdext.jar
jgroups-2.8.0.GA.jar
jms.jar
jsr173_1.0_api.jar
jsr250-api.jar
jstl-1.2.jar
jta-1.1.jar
junit-4.4.jar
log4j-1.2.15.jar
lucene-analyzers-3.1.0.jar
lucene-core-3.1.0.jar
lucene-highlighter-3.1.0.jar
lucene-memory-3.1.0.jar
lucene-misc-3.1.0.jar
lucene-queries-3.1.0.jar
lucene-spellchecker-3.1.0.jar
mysql-connector-java-5.1.6-bin.jar
naming-resources.jar
ojdbc14.jar
poi-3.0.1-FINAL-20070705.jar
quartz.jar
richfaces-api-3.2.2-SNAPSHOT.jar
richfaces-impl-3.2.2-SNAPSHOT.jar
richfaces-ui-3.2.2-SNAPSHOT.jar
servlet-api.jar
slf4j-api-1.5.8.jar
slf4j-api.jar
slf4j-log4j12-1.5.0.jar
solr-common.jar
solr-core-1.4.0.jar
solr-solrj-1.4.0.jar
spring-aop.jar
spring-beans.jar
spring-context.jar
spring-context-support.jar
spring-core.jar
spring.jar
spring-jdbc.jar
spring-jms.jar
spring-orm.jar
spring-test.jar
spring-tx.jar
spring-web.jar
spring-webmvc.jar
spring-webmvc-portlet.jar
spring-webmvc-struts.jar
sqljdbc.jar
standard.jar
xalan.jar
xml-apis-1.0.b2.jar

Hope that helps and avoid humanity torture on jar conflicts resolution 😀

OrphanRemoval on WAS 7

WAS 7

While migrating from Apache Tomcat 6 to IBM Websphere 7, in a Spring Hibernate project, you will certainly face this error:

nested exception is java.lang.NoSuchMethodError: javax/persistence/OneToMany.orphanRemoval()Z

There are many posts on the net about it. But, when dealing with IBM Websphere, it seems to be a bit different from other environments. All the issue is about persistence. All you have to do is :

1) Move your javax.persistence-2.0.0.jar to ${WAS_INSTALL_DIR}/APPSERVER/java/jre/lib/ext

2) Open configuration console and navigate to Applications->Application Types-> Websphere Entreprise Applications -> ${youApplication}

3) Click on the link “Class loading and update detection”

Webpshere 7 project details

4) In Class Loader Order, select “Classes loaded with local class loader first (parent last)”

Class loading and update detection

5) Restart WAS 7 and check if the  javax.persistence-2.0.0.jar was correctly loaded.

Websphere view - lodade classes

In the WAS 7 Redbook(WebSphere Application Server V7 Administration and Configuration Guide), they say :

The default value for class loading mode is Classes loaded with parent class
loader first. This mode causes the class loader to first delegate the loading of
classes to its parent class loader before attempting to load the class from its local
class path. This is the default policy for standard Java class loaders.
If the class loading policy is set to Classes loaded with local class loader first
(parent last), the class loader attempts to load classes from its local class path
before delegating the class loading to its parent. This policy allows an application
class loader to override and provide its own version of a class that exists in the
parent class loader.

Looks like Websphere 7, then, contains old JPA definition, without OneToMany.OrphanRemoval (which is specified in JPA 2.0).

Mykong also suggested adding hibernate-jpa-2.0-api-1.0.0.Final.jar instead of javax.persistence-2.0.0.jar

Spring Flex3 Quick Integration

Prerequisite:

Eclipse 3.3 (Europa) + WTP plugin
Flex Builder 3 Plugin Installed on Eclipse
MySql Server
Apache Tomcat 6
Spring

Step By Step:

1) Create New Dynamic Web Application:

2) Right click on the new created application, go to Flex Project Nature > Add Flex Project Nature

It's not really LiveCycle Data Cycle, rather BlazeDs

Choose the J2EE Application server Type. Then check the “use remote object access service” and keep LCDS selected. It’s not really LCDS, but its Open Source version (BlazeDs).

3) Click Next, Browse, Navigate to the blazeds.war:

then click Finish.

4) In the problem’s view, you may see this error:
the solution is quite there 😉

5) Go to project properties > Flex Server > Context Root and change  the “/WebContent” to “/[YourProjectName]”

6) Go to the WebContent/WEB-INF, add a directory(config) and create an xml file (webApplicationContext.xml)

7)  a- Add the datasource bean to your configuration file:

b- Then the sessionFactory one:

c- the most important part in the integration is the servelet mapping, and you can do it as described here:


8)  Then go the web.xml file and put the current configuration:


Keep in mind the /slmDem/* url template. we’ll need it later.

9) Now, unzip the spring integration with dependencies files and copy all the jars into your lib directory. You’ll find some double version jars, so just choose the newest.

10)  Add a java entity to your src dir. In this project, we start with a small sample class “Document”:
domain model for document database entity and then create a database table called Document with three fields (id:Integer,type:varchar(20), title:varchar(50));

11) Create the DTO class that extends the HibernateDaoSupport. Here is my DTO:

That’s all with Spring and hibernate code. we need just to add those entities to our configuration file:
add the annotated class to your session factory:

Click to enlarge
and  then declare and expose your  service (DTO) to the flex consumers:

12)Now move to the Flex part and create the corresponding AS3(ActionScript 3) entity to your Java ‘Document’ class.
Note the meta-tag RemoteClass that describes the corresponding class on the Java side. It’s crucial to keep the same names of attributes, but don’t care that much about the scope (private/public/…) of the AS class attribute.

13) Create a simple form with WISYWIG designer of FlexBuilder with 3 fields (id,type,title) and a button to insert a “Document”.

and then declare the mx:RemoteObject and some code for insert operation:

14) Go to WebContent\WEB-INF\flex and open the services-config.xml. You still remember the /slmDemo/pattern. We need it here to correctly route our request:

you need to add a default channel to you services-config.xml file for general use.
15) One final Step. You don’t need your application to be re loadable, but for some reason, leaving it re loadable by default give me some perm-gen space errors.  So, run your project as >run on server. Choose the tomcat 6 server and then click finish. Stop your application and go to a second project in your workspace called Servers. Open the server.xml file  of this application, and scroll to the last line; Change the reloadable property to false:
Your project hierarchy should look like :
This is the result:
Send Me an email to (salemba@live.fr) with the title of the post as subject to get source code.