本帖最后由 comeonchinagoogle 于 2012-08-10 10:53:31 编辑

解决方案 »

  1.   

    没人回答,XDJM们,用的技术就是spring2.5.5+quartz-1.6.5
      

  2.   

    你读取spring配置文件啦吗?ApplicationContext……
      

  3.   

    <property name="schedulerContextAsMap">
    <map>
    <!-- spring 管理的service需要放到这里,才能够注入成功 -->
    <description>schedulerContextAsMap</description>
    <entry key="xxService或xxRepo" value-ref="xxService或xxRepo" />
    </map>
    </property>
      

  4.   


     <!--  定时执行任务的类,要继承TimerTask  --> 
       
      <!--  <bean  id ="SmsSendTask"  class ="com.aisino.platform.sms.core.SmsSendTask" /> -->  
         <!--  用Spring管理这个TimerTask,设定触发间隔  --> 
          <!-- 
          <bean  id ="ScheduledUserTimerTask"  class ="org.springframework.scheduling.timer.ScheduledTimerTask" > 
            <property  name ="delay" > 
              <value > 10000 </value > 
            </property > 
            <property  name ="period" > 
              <value > 60000 </value > 
            </property > 
            <property  name ="timerTask" > 
              <ref  local ="SmsSendTask" /> 
            </property > 
          </bean > 
      -->
        
        
        <!-- 
         <bean  id ="SmsQuickQueueSendTask"  class ="com.aisino.platform.sms.queue.SmsQuickQueueSendTask" />  
           <bean  id ="ScheduledQuickTimerTask"  class ="org.springframework.scheduling.timer.ScheduledTimerTask" > 
            <property  name ="delay" > 
              <value >30000</value> 
            </property > 
           
            <property  name ="period" > 
              <value >60000</value > 
            </property > 
            <property  name ="timerTask" > 
              <ref  local ="SmsQuickQueueSendTask" /> 
            </property > 
          </bean > 
        
          <bean  id ="timerFactory"  class ="org.springframework.scheduling.timer.TimerFactoryBean" > 
           <property  name ="scheduledTimerTasks" > 
            <list > 
         
              <ref  local ="ScheduledQuickTimerTask" /> 
            </list > 
           </property > 
         </bean > 
    参考下
      

  5.   


    <bean id="sendMsgJobDetail" class="org.springframework.scheduling.quartz.JobDetailBean">
            <property name="jobClass">
                <value>com.youcom.job.SendMsg</value>
            </property>
            <property name="jobDataAsMap">
                <map>
                    <entry key="msgDao"><ref bean="msgDao"/></entry>
                </map>
            </property>
        </bean>
    将SendMsg改为继承SPIRNG的QuartzJobBean在TestInit中注入上面定义的JobDetail,加入如下内容:
    @Resource
    private JobDetail sendMsgJobDetail;
      

  6.   

    楼主所指的动态生成任务没有得到msgDao,但这个动态生成的任务是如何创建SendMsg 实例的呢?反射吗?反射的话就得不到msgDao了。只有取spring容器中的SendMsg 实例才能取到msgDao,通过反射创建的SendMsg实例,spring并没有将msgDao注入其中。第2个问题没看懂,不知道是不是由于第1个问题引起的。
      

  7.   

    第二个问题,错误提示如下:org.quartz.ObjectAlreadyExistsException:Unable to store Job withe name:'smsJob_222' and group:'DEFAULT',because one already exists with this identification
      

  8.   

    是通过反射创建的:
      JobDetail jobDetail=new JobDetail("msgJob_"+msgGetId(),Scheduler.DEFAULT_GROUP,SendMsgJob.class);//可以看这是一个CLASS类,SendMsgJob.class
      
      

  9.   


    JobDetail jobdetail=new JobDetail("msgJob_"+msg.getId(),Scheduler.DEFAULT_GROUP,SendMsg.class);循环的时候,msg.getId有重复的吧?
      

  10.   

    通过反射创建的SendMsg实例,spring并没有将msgDao注入其中。如果必须要用的dao的话,还是通过ApplicationContext获取吧。
      

  11.   


    问题已解决。
    首先:
          job的实现中无法实现dao或者service的spring初始化注入;
    其次:
          job的实现每次都会创建一个任务实例,如果在job中去做new ClassPathXmlApplicationContext("classpath*:xxx");的话可以解决上面的情况,但是job每次都会创建一个实例,这样在高并发或者长时间使用下,很容易就会内存溢出;我的处理办法是:
          在spring配置文件中加入ApplicationContextAware 的实现类SpringContextUtil ,让spring在初始化的时候做一次需要使用的service或dao的bean初始化,这样在job中就可以通过这个util去拿到已经初始化的实例,而不用每次都创建一次context。
    util的实现只需要加入:
    @Override
           @SuppressWarnings("static-access" )
           public void setApplicationContext(ApplicationContext contex)
                       throws BeansException {
                 this.context = contex;
          }       public static Object getBean(String beanName){
            return context .getBean(beanName);
        }
          
           public static String getMessage(String key){
            return context .getMessage(key, null, Locale. getDefault());
        }
    然后在spring配置文件中加入这个util的<bean class ...即可