Monday, March 9, 2009

Quartz Implementation

1. Implent the batch job you need to run as the class that extends the class: QuartzJobBean implements StatefulJob.
This should override the method executeInternal. This should contain the stuff you need to cron.
2. Define the bean : org.springframework.scheduling.quartz.JobDetailBean
set the property ''jobClass' which it your implementation of cron that you want to run
3. Point the above bean to org.springframework.scheduling.quartz.CronTriggerBean
as the property jobDetail.
Another parameter it takes is the cron string. This is similar to unix cron
4.Place the above bean in the org.springframework.scheduling.quartz.SchedulerFactoryBean.
as content of the property 'trigger'


<bean name="helloWorld" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass" value="com.test.eip.viva.batch.HelloWorld" />
</bean>
<bean id="helloWorldBatchTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="helloWorld" />
<property name="cronExpression" value="0 * * * * ?" />
</bean>
<bean id="eipSchedulerFactoryBean"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="helloWorldBatchTrigger" />
</list>
</property>
</bean>




If your cron bean requires additional beans. This is the way to inject the bean. Provide the beans required by the jobClass in a jobDataAsMap property

<bean name="helloWorld" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass" value="com.test.eip.viva.batch.HelloWorld" />
<property name="jobDataAsMap">
<map>
<entry key="message">
<ref bean="messageProvider" />
</entry>
</map>
</property>
</bean>

Tuesday, March 3, 2009

Contextual sessions with Hibernate3 and Spring2.5

DataSource and SessionFactory
The blog describes how to effectively use hibernate 3 with spring2.5 without using hibernateTemplate or HibernateDaoSupport.
Blog will walk you through setting up spring and hibernate. Using SessionFactory and session to access tables. Then it will show you how to use contextual sessions.
You need to start with a spring project. Create a Spring context.xml
Below is the header:


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">


Create session factory

<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:/hibernate.cfg.xml" />
<property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.generate_statistics">true</prop>
<prop key="hibernate.use_sql_comments">true</prop>
<prop key="hibernate.cache.provider_class">net.sf.ehcache.hibernate.SingletonEhCacheProvider</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.cache.provider_configuration">classpath:/ehcache.cfg.xml</prop>
<prop key="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
</props>
</property>
</bean>


Create DataSource:

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"><value>oracle.jdbc.driver.OracleDriver</value></property>
<property name="url">
<value>${db-url}</value>
</property>
<property name="username"><value>${db-username}</value></property>
<property name="password"><value>${db-password}</value></property>
</bean>


Using Properties File
The properties db-url, db-username, db-password are in the app.properties file. Spring will be able to read them using PropertyPlaceholderConfigurer which takes the location propety. Below is the bean definition in spring context:

<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:/app.properties</value>
</property>
</bean>


Using JNDI
You can use Jndi instead of maintaining the connection properties. Define dataSource as a JndiObjectFactoryBean instead of DriverManagerDataSource.

<bean id="dataSource"
class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="${datasource.jndi.name}" />
<property name="jndiEnvironment">
<props>
<prop key="java.naming.provider.url">${provider.url}</prop>
</props>
</property>
</bean>

Why JNDI?
1. Database url, username is not hard coaded inside in the war, so easy for deployment. Set up jndi on realted environment.
2. Connection pool size is managed by the jndi provider. If we are using DriverManager, it will allow you to create as many connections limited at the database. With Jndi provider, it is easy to figure out sessions left open.



Hibernate Configuration and Bean Annotations
Create hibernate configuration file under 'war/WEB-INF/classes/'. This file points to the beans that will be mapped to the tables.

<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<mapping class="com.test.eip.domain.ExampleBean1" />
<mapping class="com.test.eip.domain.ExampleBean2" />
</session-factory>
</hibernate-configuration>


The beans will be mapped to the tables using annotations from package 'javax.persistence.'
The annotations at the begining of the class : @Entity @Table(name='MyEntity' schema='')
Fields are mapped by the @Column(name='') annotations at the getters or at the field declarations.

@Entity
@Table(name = “ExampleBean1″, schema = “”)
public class Equipment implements java.io.Serializable {
private static final long serialVersionUID = 2959455667770311499L;
private long id;
@Id
@Column(name = “ID”, unique = true, nullable = false, precision = 38, scale = 0)
public long getId() {
return id;
}

With above 3 things, you should be able to get the entity from Database using command like session.get(MyEntity.class, id)

Using the SessionFactory
Below is the sample to get the Objects.

final Session session = sessionFactory.openSession();
try{
Equipment equipment = (Equipment) session.get(Equipment.class, id);
.
.
}finally{
session.close();
}


Below is the sample to execute the raw SQL

final Session currentSession = sessionFactory.openSession();
final Query query = currentSession.createSQLQuery("select ID_SEQ.nextval from dual");
try{
final String id = query.uniqueResult()).toPlainString();
}finally{
currentSession.close();
}


Contextual Sessions
You can use contextual sessions with sessionFactory.getCurrentSession(). For this you will have to define transactionManger.

Pass the sessionFactory to the transactionManager.

<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>


Add the tx namespace to context.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

.
.
.

<tx:annotation-driven />


Place the annotations above the methods you are going to use the sessionFactory.

@Transactional
public Equipment getEquipmentById(final long id) {
final Session session = sessionFactory.getCurrentSession();
final Equipment equipment = (Equipment) session.get(Equipment.class, id);
return equipment;
}

You no longer need to close the session or open the session with SessionFactory. getCurrentSession will check if there is an active session and return it. If there is no active session it will create new.
If the method in transaction calls other methods that use sessionFactory, all will use the same transaction provided you have '@Transaction' annotation over all the methods called. Same hibernate session will be kept alive for you by the SessionFactory.