michaelklishin / quartz-mongodb

A MongoDB-based store for the Quartz scheduler. This fork strives to be as feature complete as possible. Originally by MuleSoft.
Other
249 stars 203 forks source link

java.lang.RuntimeException: json can't serialize type : class org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean #38

Closed taushant closed 11 years ago

taushant commented 11 years ago

I get following error when using this tool with Spring 3.2.4, Spring Data MongoDB 1.3.1, and Quartz 2.1.7

java.lang.RuntimeException: json can't serialize type : class org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean
at com.mongodb.util.ClassMapBasedObjectSerializer.serialize(ClassMapBasedObjectSerializer.java:77)
at com.mongodb.util.JSONSerializers$MapSerializer.serialize(JSONSerializers.java:307)
at com.mongodb.util.ClassMapBasedObjectSerializer.serialize(ClassMapBasedObjectSerializer.java:79)

My quartz-job.xml looks like this

<?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:p="http://www.springframework.org/schema/p" 
xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<!-- The bean that does the actual work -->
<bean id="myTask" class="com.abcd.quartz.MyTask" />

<!-- Quartz uses Trigger, Job and JobDetail objects to realize scheduling of all kinds of jobs. 
        For the basic concepts behind Quartz, have a look at http://www.opensymphony.com/quartz.
        See: http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/scheduling.html#scheduling-quartz -->
<bean id="jobAbcd" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
  <property name="targetObject" ref="myTask" />
  <property name="targetMethod" value="doAbcd" />
  <property name="concurrent" value="false" />
</bean>

<!-- startDelay: Delay 10 seconds 
        cronExpression:Repeat every 5 minutes -->       
<bean id="cronTrigger4jobAbcd" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <property name="jobDetail" ref="jobAbcd" /> 
        <property name="startDelay" value="10000" />
        <property name="cronExpression" value="0 0/5 * * * ?" />
 </bean>

<!--  SCHEDULER FOR ALL JOBS -->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="waitForJobsToCompleteOnShutdown" value="true" />
        <property name="triggers">
            <list>
                <ref bean="cronTrigger4jobAbcd" />
            </list>
        </property>
        <property name="quartzProperties"> 
            <props> 
                <prop key="org.quartz.jobStore.class">com.novemberain.quartz.mongodb.MongoDBJobStore</prop>
                <prop key="org.quartz.jobStore.mongoUri">mongodb://localhost:27017</prop> 
                <prop key="org.quartz.jobStore.dbName">mydb</prop>
                <prop key="org.quartz.jobStore.collectionPrefix">qrtz</prop>
                <prop key="org.quartz.threadPool.threadCount">4</prop><!-- thread count setting is ignored by the MongoDB store but Quartz requires it  -->
            </props>
        </property>
</bean>

MyTask.java

package com.abcd.quartz;
public class MyTask{    
    public void doAbcd() {      
            System.out  
            .println("Current Time : " + Calendar.getInstance().getTime());             
    }   
}
michaelklishin commented 11 years ago

MongoDB Java driver cannot serialize Spring beans, so job store cannot store them.

taushant commented 11 years ago

Understood but then how do I use quartz-mongodb with Spring? Is there a workaround? Or are you saying that Spring and quartz-mongo are incompatible and do not work together - period?

michaelklishin commented 11 years ago

MongoDB Java driver does not allow extending it to arbitrary types. Avoid storing Spring beans in jobDetail.

taushant commented 11 years ago

Thanks for the response. I will try doing what you suggest.