Programming and So

Tips and tricks in Java

Posts Tagged ‘spring

Oracle JDBC Driver and Spring 2.0.X Timestamp issue

without comments

Oracle JDBC Driver (ojdbc14.jar)

Spring Framework 2.0

import org.springframework.jdbc.support.rowset.SqlRowSet;

public class QueryDAO extends JdbcDaoSupport {

    public SqlRowSet loadSqlRowSet(String sSQL) {
        return getJdbcTemplate().queryForRowSet(sSQL);
    }

}

Using Spring JDBC in such way Timestamp fields are recovered without time information (hours, minutes and seconds set to 0). It seems some issue relative to Sun or Oracle, but there is a workaround in order to achieve desired result.

SqlRowSet rset =
    jdbcTemplate.queryForRowSet("select cast(sysdate as timestamp) from dual");
while (rset.next()) {
    System.out.println("-> " +
        ((oracle.sql.TIMESTAMP)rset.getObject(1)).timestampValue());
}

Our batch process failed last night because of this…

Written by angelborroy

April 30, 2009 at 2:33 pm

Posted in java

Tagged with , ,

Complementary triggers using Quartz and Spring

without comments

Following example configures one quartz job to run on normal days and another quartz job to run on holidays dates. That is, one quartz trigger run when the other not. I imagine some easier solution exists, but it works.

applicationContext.xml

<!-- Properties file -->
<bean id="properties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="location" value="file:/foo/dates.prop"/>
</bean>

<!-- QUARTZ Configuration -->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
	<list>
            <ref bean="normalTrigger" />
            <ref bean="holidayTrigger" />
        </list>
    </property>
<property name="calendars">
        <map>
            <entry key="calendarNormal"><ref bean="normalCalendar" /></entry>
            <entry key="calendarHoliday"><ref bean="holidayCalendar" /></entry>
        </map>
    </property>
</bean>

<!-- Everyday but holidays -->
<bean id="normalCalendar" class="MyNormalCalendar">
    <constructor -arg><ref bean="properties" /></constructor>
</bean>

<!-- Every holiday -->
<bean id="holidayCalendar" class="MyHolidayCalendar">
    <constructor -arg><ref bean="properties" /></constructor>
</bean>

<!-- Normal Job -->
<bean id="normalTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="someJobDetail" />
<property name="calendarName" value="calendarNormal"/>
<property name="cronExpression">
        <!-- Every five minutes from 8h to 14h -->
        <value>0 0/5 8-14 * * ?</value>
    </property>
</bean>

<!-- Holiday Job -->
<bean id="holidayTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="anotherJobDetail" />
<property name="calendarName" value="calendarHoliday"/>
<property name="cronExpression">
        <!-- Every five minutes from 8h to 14h -->
        <value>0 0/5 8-14 * * ?</value>
    </property>
</bean>

dates.prop
workingDays.legalHolidays=2008-01-01,2008-03-21,2008-03-24,2008-05-01,2008-05-08,2008-05-12,2008-07-14,2008-08-15,2008-11-11,2008-12-25

MyHolidayCalendar.java

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Properties;

import org.quartz.impl.calendar.HolidayCalendar;

/**
 *
 * Activate trigger on holidays.
 *
 * @author gz451w
 *
 */
@SuppressWarnings("serial")
public class MyHolidayCalendar extends HolidayCalendar {

  private static final String WORKING_DAYS_LEGAL_HOLIDAYS = "workingDays.legalHolidays";
  private Properties properties;

  /**
   * Build activate days list
   * @param properties
   */
  public MyHolidayCalendar(Properties properties) {

    this.properties = properties;

    String stringDates = getProperties().getProperty(WORKING_DAYS_LEGAL_HOLIDAYS);

    if (stringDates != null) {
      Calendar[] dates = getDatesInverse(stringDates);
      for (Calendar date : dates) {
        addExcludedDate(date.getTime());
      }
    }
  }

  /**
   * @return the properties
   */
  public Properties getProperties() {
    return properties;
  }

  /**
   * @param properties the properties to set
   */
  public void setProperties(Properties properties) {
    this.properties = properties;
  }

  /**
   * Inverse list from a date list
   * @param stringDates
   * @return
   */
  private Calendar[] getDatesInverse(String stringDates) {

    String[] stringDatesArray = stringDates.split(",");

    // Holiday list
    ArrayList<calendar> dates = new ArrayList</calendar><calendar>();
    for (String stringDate : stringDatesArray) {
      String[] splitDate = stringDate.split("-");
      Calendar date =
        new GregorianCalendar(
            Integer.parseInt(splitDate[0]),
            Integer.parseInt(splitDate[1]) - 1,  // Month field starts with 0!
            Integer.parseInt(splitDate[2]));
      dates.add(date);
    }

    // Exclude non-holiday dates
    ArrayList</calendar><calendar> returnDates = new ArrayList</calendar><calendar>();
    try {

      Calendar c = Calendar.getInstance();
        c.setTime(new SimpleDateFormat("dd/MM/yyyy").parse("01/01/" + dates.get(0).get(Calendar.YEAR)));

      while(c.get(Calendar.YEAR) == dates.get(0).get(Calendar.YEAR)){
        if (!dates.contains(c)) {
          returnDates.add((Calendar)c.clone());
        }
          c.add(Calendar.DAY_OF_YEAR, 1);
        }

    } catch (Exception e) {
      e.printStackTrace();
    }

    return returnDates.toArray(new Calendar[]{});

  }

}

MyNormalCalendar.java

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Properties;

import org.quartz.impl.calendar.HolidayCalendar;

/**
 *
 * Activate trigger on non-holiday dates.
 *
 * @author gz451w
 *
 */
@SuppressWarnings("serial")
public class MyNormalCalendar extends HolidayCalendar {

  private static final String WORKING_DAYS_LEGAL_HOLIDAYS = "workingDays.legalHolidays";

  private Properties properties;

  /**
   * Exclude holidays
   * @param properties
   */
  public MyNormalCalendar(Properties properties) {

    this.properties = properties;

    String stringDates = getProperties().getProperty(WORKING_DAYS_LEGAL_HOLIDAYS);
    if (stringDates != null) {
      Calendar[] dates = getDates(stringDates);
      for (Calendar date : dates) {
          addExcludedDate(date.getTime());
      }
    }
  }

  /**
   * @return the properties
   */
  public Properties getProperties() {
    return properties;
  }

  /**
   * @param properties the properties to set
   */
  public void setProperties(Properties properties) {
    this.properties = properties;
  }

  /**
   * Get holidays
   * @param stringDates
   * @return
   */
  private Calendar[] getDates(String stringDates) {

    String[] stringDatesArray = stringDates.split(",");

    Calendar[] dates = new Calendar[stringDatesArray.length];
    int i = 0;
    for (String stringDate : stringDatesArray) {
      String[] splitDate = stringDate.split("-");
      Calendar date =
        new GregorianCalendar(
            Integer.parseInt(splitDate[0]),
            Integer.parseInt(splitDate[1]) - 1,  // Month field starts with 0!
            Integer.parseInt(splitDate[2]));
      dates[i++] = date;
    }
    return dates;

  }

}

Written by angelborroy

December 11, 2008 at 2:56 pm

Posted in java

Tagged with ,

Using JPA with Spring 2.0 or the Java Agent issue

without comments

Maybe this article is the main reference about JPA in Spring 2.0. It’s a great resource to start playing with JPA technology but it does not include any comment relative to a key issue: load time weaving. This technique is responsible of any class transformation from generic database objects to customized domain beans.

Spring’s 2.0 reference include several options to perform this work hooking application’s container resources and describe standalone support via java agent (VM option -javaagent:/path/to/spring-agent.jar). In Spring 2.5 hooking options have been increased (p.e. Weblogic only is available since this version).

I was evaluating a migration from Hibernate to JPA but this issue make me doubt. It seems as if they were using a bazooka to kill a fly!

Written by angelborroy

October 31, 2008 at 10:48 am

Posted in java

Tagged with ,