Announcement Announcement Module
Collapse
No announcement yet.
Exception in scheduling Jasper job with Spring Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Exception in scheduling Jasper job with Spring

    hello all,
    i have a webapplication that uses spring together with Quartz for scheduling jobs.
    I have written a job that uses Jasper to print a report of some data that i have in the database.
    Code for my Job is as follows:
    <code>
    public class ReportJob extends QuartzJobBean implements StatefulJob {


    private int dayOffSet = 0;


    protected void executeInternal(JobExecutionContext context)
    throws JobExecutionException {
    System.err.println("---" + context.getJobDetail().getFullName()
    + " executing.[" + new Date() + "]");

    JobDataMap map = context.getJobDetail().getJobDataMap();
    System.err.println("*BJob.JobDataMap is:");
    try{
    java.util.Calendar cal = java.util.Calendar.getInstance();
    int offset = ((Integer)map.get("dayOffset")).intValue();
    Date endDate = cal.getTime();
    cal.add(java.util.Calendar.DATE, -offset);
    Date startDate = cal.getTime();
    Map parameters = new HashMap();
    parameters.put("dateStart", startDate);
    parameters.put("dateEnd", endDate);
    parameters.put("ReportTitle", "List of expenses");
    parameters.put("FilterClause", "%");
    parameters.put("user", "%");
    SchedulerContext schedulerContext = context.getScheduler().getContext();
    BeanFactory beanFactory = (BeanFactory)schedulerContext.get("applicationCont ext");
    ReportGenerator reportGenerator = (ReportGenerator)beanFactory.getBean("reportGenera tor");
    System.err.println("*GEnerting report........");

    reportGenerator.generate(map, Constants.FORMAT_PRINT);


    } catch(Exception e) {
    System.err.println("Exception in printing report..." + e);
    e.printStackTrace();
    }
    }

    }
    </code>
    ReportGenerator is a class that i have declared in applicationContext.xml. It will gets parameters passed at runtime (in this case, via the job) and it will print a report using a connection from a Datasource that i have defined in applicationContext.xml.
    Here is the code of my JasperReportGenerator
    <code>
    public class JasperReportGenerator implements ReportGenerator,java.io.Serializable {

    private String reportFile = null;

    private LogWrapper log = new LogWrapper(this.getClass().getName());
    private DataSource dataSource= null;

    public JasperReportGenerator() {}



    public void setReportFile(String fileName){
    reportFile = fileName;
    }

    public void setDataSource(DataSource source) {
    dataSource = source;
    }

    public Object generate(Map parameters, String type) throws ReportException {
    try {
    Iterator iterator = parameters.keySet().iterator();
    Map jasperParams = new HashMap();
    while(iterator.hasNext()) {
    String key = (String)iterator.next();
    Object value = parameters.get(key);
    jasperParams.put(key, value);
    }

    Connection conn = getConnection();

    System.setProperty("org.xml.sax.driver", "org.apache.xerces.parsers.SAXParser");
    System.setProperty("jasper.reports.compiler.class" ,
    "net.sf.jasperreports.engine.design.JRBshCompiler" );

    JasperReport report = JasperCompileManager.compileReport("c:/sw/Budget-Project/Framework/src/conf/QueryReport.xml");

    SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
    java.util.Date startDate = formatter.parse("01/01/2004");
    java.util.Date endDate = formatter.parse("01/02/2004");


    log.error("PARAMETERS IS:" + parameters);

    Map imageMap = new HashMap();
    JasperPrint prnt = JasperManager.fillReport(report,parameters, getConnection());
    if(type.equals(Constants.FORMAT_HTML)) {
    JRHtmlExporter exp = new JRHtmlExporter();
    StringBuffer sbuf = new StringBuffer();
    exp.setParameter(JRExporterParameter.JASPER_PRINT, prnt);
    exp.setParameter(JRExporterParameter.OUTPUT_STRING _BUFFER, sbuf);
    exp.setParameter(JRHtmlExporterParameter.IMAGES_MA P, imageMap);
    exp.setParameter(JRHtmlExporterParameter.IMAGES_UR I, "images/");
    exp.exportReport();
    return sbuf.toString();
    } else {
    log.debug("---- log debug before printing report...");
    log.error("printing....");
    JasperPrintManager.printReport(prnt, true);
    return "";
    }

    } catch(Exception e) {
    System.err.println("***Exception in reportGEnerator:\n " + e);
    throw new ReportException(e);
    }
    }


    private Connection getConnection() throws Exception {
    try {
    return dataSource.getConnection();
    } catch(Exception e) {
    System.err.println("Could not get connection!" + e);
    throw e;
    }
    }

    }
    </code>

    So, according to what i have written, there should be NO data persisted in the database other than dayOffset (which is a field that is being set at runtime).
    But for some reason, when printing a report, i am getting the following exception:

    12:11:36,765 ERROR [ErrorLogger] An error occured while marking executed job com
    plete. job= 'BudgetApp Group.Report Task'
    org.quartz.JobPersistenceException: Couldn't serialize job data: com.mysql.jdbc.
    Connection [See nested exception: java.io.NotSerializableException: com.mysql.jd
    bc.Connection]
    at org.quartz.impl.jdbcjobstore.JobStoreSupport.trigg eredJobComplete(Job
    StoreSupport.java:1872)
    at org.quartz.impl.jdbcjobstore.JobStoreTX.triggeredJ obComplete(JobStore
    TX.java:1285)
    at org.quartz.core.QuartzScheduler.notifyJobStoreJobC omplete(QuartzSched
    uler.java:1388)
    at org.quartz.core.JobRunShell.completeTriggerRetryLo op(JobRunShell.java
    :380)
    at org.quartz.core.JobRunShell.run(JobRunShell.java:2 64)
    at org.quartz.simpl.SimpleThreadPool$WorkerThread.run (SimpleThreadPool.j
    ava:516)
    * Nested Exception (Underlying Cause) ---------------
    java.io.NotSerializableException: com.mysql.jdbc.Connection
    at java.io.ObjectOutputStream.writeObject0(ObjectOutp utStream.java:1054)

    at java.io.ObjectOutputStream.writeObject(ObjectOutpu tStream.java:278)
    at java.util.HashMap.writeObject(HashMap.java:978)
    at sun.reflect.GeneratedMethodAccessor95.invoke(Unkno wn Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAcces
    sorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:324)
    at java.io.ObjectStreamClass.invokeWriteObject(Object StreamClass.java:80
    9)
    at java.io.ObjectOutputStream.writeSerialData(ObjectO utputStream.java:12
    96)
    at java.io.ObjectOutputStream.writeOrdinaryObject(Obj ectOutputStream.jav
    a:1247)
    at java.io.ObjectOutputStream.writeObject0(ObjectOutp utStream.java:1052)

    at java.io.ObjectOutputStream.defaultWriteFields(Obje ctOutputStream.java
    :1332)
    at java.io.ObjectOutputStream.writeSerialData(ObjectO utputStream.java:13
    04)
    at java.io.ObjectOutputStream.writeOrdinaryObject(Obj ectOutputStream.jav
    a:1247)
    at java.io.ObjectOutputStream.writeObject0(ObjectOutp utStream.java:1052)

    at java.io.ObjectOutputStream.writeObject(ObjectOutpu tStream.java:278)
    at org.quartz.impl.jdbcjobstore.StdJDBCDelegate.seria lizeObject(StdJDBCD
    elegate.java:3630)
    at org.quartz.impl.jdbcjobstore.StdJDBCDelegate.seria lizeJobData(StdJDBC
    Delegate.java:3654)
    at org.quartz.impl.jdbcjobstore.StdJDBCDelegate.updat eJobData(StdJDBCDel
    egate.java:803)
    at org.quartz.impl.jdbcjobstore.JobStoreSupport.trigg eredJobComplete(Job
    StoreSupport.java:1870)
    at org.quartz.impl.jdbcjobstore.JobStoreTX.triggeredJ obComplete(JobStore
    TX.java:1285)
    at org.quartz.core.QuartzScheduler.notifyJobStoreJobC omplete(QuartzSched
    uler.java:1388)
    at org.quartz.core.JobRunShell.completeTriggerRetryLo op(JobRunShell.java
    :380)
    at org.quartz.core.JobRunShell.run(JobRunShell.java:2 64)
    at org.quartz.simpl.SimpleThreadPool$WorkerThread.run (SimpleThreadPool.j
    ava:516)

    From my point of view, it looks like it is trying to store the JDBC connection in the database for my Job, but it cannot be since it is not part of the map... when the job gets executed, i am getting ReportGenerator from applicationContext.....

    anyone could explain me what is hte error and why is it happening?

    htanx in advance and regards
    marco
Working...
X