Monthly Archives: September 2014

JdbcTemplate deprecated method queryForLong() and queryForInt() in Spring 4 and in spring 3.2.x

 sunflower   Since the jdbcTemplate.queryForInt() and jdbcTemplate.queryForLong() are deprecated in new version of spring(i think,After3.2.x,they are deprecated).We will have to use  jdbcTemplate.queryForObject() for the purpose .We should take care following coding practice otherwise  unwanted exceptions can come.

1.When we will use jdbcTemplate.queryForInt() for getting some int value in a query like:

Existing use of queryForInt() for getting int value:

public int getLineNumber(String orderId, String orderType) {
String sql = "SELECT MAX(order_line_no) FROM ORDER WHERE order_id = ? and order_type = ?";
int value = jdbcTemplate.queryForInt(sql, new Object[] {orderId, orderType});

return value
}

Or

public int getLineNumber(String orderId, String orderType) {
String sql = "SELECT MAX(order_line_no) FROM ORDER WHERE order_id = ? and order_type = ?";
return jdbcTemplate.queryForInt(sql, new Object[] {orderId, orderType});

}

 

Now because queryForInt() is deprecated,we will use queryForObject() for getting int value:

public int getLineNumber(String orderId, String orderType) {
String sql = "SELECT MAX(order_line_no) FROM ORDER WHERE order_id = ? and order_type = ?";
int value = jdbcTemplate.queryForObject(sql, new Object[] {orderId, orderType }, Integer.class);

return value
}

Or

public int getLineNumber(String orderId, String orderType) {
String sql = "SELECT MAX(order_line_no) FROM ORDER WHERE order_id = ? and order_type = ?";
return jdbcTemplate.queryForObject(sql, new Object[] {orderId, orderType }, Integer.class);

}

Now in the above code, if queryForObject return null value.It can not be assigned to primitive type int value .

So null pointer exception will come.To handle this , we should change our method like:

 

public int getLineNumber(String orderId, String orderType) {
String sql = "SELECT MAX(order_line_no) FROM ORDER WHERE order_id = ? and order_type = ?";
Integer value = jdbcTemplate.queryForObject(sql, new Object[] {orderId, orderType }, Integer.class);

return ((value == null) ? 0 : value.intValue());
}

This will ensure no exception if null is returned in the query.Similar changes can be applied to get an long value.

Quartz 2 (2.2.1) Scheduler integration with Spring 4 (4.0.5.RELEASE) and Maven build tool.

Coffe_Cup     Assumption is that you are having a maven project.Let divide whole task (Quartz 2.2.1 integration with Spring 4.0.5.RELEASE) in to 4 step process 

1. Add Spring 4.0.5.RELEASE and quartz 2.2.1 dependency in pom.xml file:
2.Creating a task class(in which our method is present which we want to schedule to run periodically)
3.Creating a job class(this will initiate task class and call the method in it)
4.Configuration in spring xml file(spring-quartz.xml)


 

1. Add Spring 4.0.5.RELEASE and quartz 2.2.1 dependency in pom.xml file:
 
<properties>
<spring.version>4.0.5.RELEASE</spring.version>
</properties>

<!-- Spring dependency -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>

And quartz dependency are:

<!-- Quartz framework -->
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.1</version>
</dependency>

 
Download above dependency in your ..../username/.m2/respository/* location by running mvn eclipse:eclipse or mvn install.
2.Creating a task class(in which our method is present which we want to schedule to run periodically)mentioned below:
package com.devil.space.quartz;

import java.util.List;
import org.apache.log4j.Logger;
import com.devil.space.exception.WebServiceUploadException;
import com.devil.space.model.Transaction;
import com.devil.space.service.TransactionProcessService;
import com.devil.space.xmlservice.XMLUploadService;

/*****************************************************************************
* Class used for processing<code>Transaction</code> records for
xml uploading.
*
*
******************************************************************************
public class TransactionRecordsProcessTask {
Logger logger = Logger.getLogger(TransactionRecordsProcessTask.class);
private XMLUploadService xmlUploadService;
private TransactionProcessService transactionProcessService;

public void processRecords() {

if (logger.isInfoEnabled()) {
logger.info("In TransactionRecordsProcessTask.Transaction records processing started");
}

try {
List<Transaction> transactionRecordForXMLUpload = transactionProcessService
.processTransactionRecords();

for (Transaction transaction : transactionRecordForXMLUpload) {
xmlUploadService.uploadTransactionRecord(transaction);
}
} catch (XMLUploadException e) {
logger.warn("error processing TransactionRecordsQuartzJob", e);
} catch (Throwable t) {
logger.warn("uncatchable error:", t);
}
}

public XMLUploadService getXmlUploadService() {
return xmlUploadService;
}

public void setXmlUploadService(XMLUploadService xmlUploadService) {
this.xmlUploadService = xmlUploadService;
}

public TransactionProcessService getTransactionProcessService() {
return transactionProcessService;
}

public void setTransactionProcessService(TransactionProcessService transactionProcessService) {
this.transactionProcessService = transactionProcessService;
}

}

 

3. Creating a job class(this will initiate task class and call the method in it)

 

package com.devil.space.quartz;

import java.util.Date;

import org.apache.log4j.Logger;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

public class TransactionRecordsQuartzJob extends QuartzJobBean {
Logger logger = Logger.getLogger(TransactionRecordsQuartzJob.class);
TransactionRecordsProcessTask transactionRecordsProcessTask;

public void executeInternal(JobExecutionContext context)
throws JobExecutionException {
if (logger.isInfoEnabled()) {
logger.info("In TransactionRecordsQuartzJob - executing its JOB on "
+ new Date()
+ " by "
+ context.getTrigger().getDescription());
}

transactionRecordsProcessTask.processRecords();
}

public TransactionRecordsProcessTask getTransactionRecordsProcessTask() {
return transactionRecordsProcessTask;
}

public void setTransactionRecordsProcessTask(TransactionRecordsProcessTask transactionRecordsProcessTask) {
this.transactionRecordsProcessTask = transactionRecordsProcessTask;
}

}

 

4.Configuration in spring xml file(spring-quartz.xml) Create a xml file for spring-quartz configuration and put down the below content in it.

 

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

http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/oxm
http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"
>

<!-- Simple Trigger, run every 5 seconds -->
<bean id="simpleTrigger"
class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">

<property name="jobDetail" ref="transactionRecordJob" />
<property name="repeatInterval" value="5000" />
<property name="startDelay" value="1000" />

</bean>

<bean id="transactionRecordJob"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="transactionRecordsProcessTask" />
<property name="targetMethod" value="processRecords" />
</bean>

<bean id="transactionRecordsProcessTask" class="com.devil.space.quartz.TransactionRecordsProcessTask">
<property name="xmlUploadService" ref="xmlUploadService" />
<property name="transactionProcessService" ref="transactionProcessService" />

</bean>

<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="autoStartup" value="true" />
<property name="jobDetails">
<list>
<ref bean="transactionRecordJob" />
</list>
</property>
<property name="triggers">
<list>
<ref bean="simpleTrigger" />
</list>
</property>

</bean>

<bean id="xmlUploadService" class="com.devil.space.xmlservice.XMLUploadServiceImpl">
</bean>

<bean id="transactionProcessService" class="com.devil.space.service.TransactionProcessServiceImpl">
</bean>

</beans>

 

That is it.Now create a war file of the project by mvn install and deploy the application in a application server (Say apache-tomcat-7.0.29).Quartz will start working as expected.