首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2EE开发 >

困扰好久了,spring quartz一个有关问题,job无法得到spring注入的属性bean,请CSDN年XDJM看一上,帮忙看一上,给分不是有关问题,万分

2012-09-28 
困扰好久了,spring quartz一个问题,job无法得到spring注入的属性bean,请CSDN年XDJM看一下,帮忙看一下,给分

困扰好久了,spring quartz一个问题,job无法得到spring注入的属性bean,请CSDN年XDJM看一下,帮忙看一下,给分不是问题,万分感谢
事情是这样的,动态启动多个任务去发送消息,这个任务我是从数据库里获取,各个任务的启动是同一个JOB,只是每个任务JOB传入的参数不同,但这个JOB,需要一个spring 的dao,得不到spring注入的dao属性,不知道怎么回事

1.spring配置dao

--application*.xml--
<bean id="sendMsg" class="com.test.SendMsg">
  <property name="msgDao"><ref bean="msgDao"/></property>
</bean>

....msgDao已经配好数据源 spring
2.启任务
1)public calss TestInit(){
  //get tasklist
  public void initJobTrigger() throws Exception
  {
  List list=getTasklist();
  if(null!=list&&list.size()>0){
  Iterator ite=list.iterator();//这个可以得到dao 
  while(ite.hasNext()){
  Tmsg msg=(Tmsg)ite.next();
  JobDetail jobdetail=new JobDetail("msgJob_"+msg.getId(),Scheduler.DEFAULT_GROUP,SendMsg.class);
  SimpleTrigger sm=new SimpleTrigger("trigger_"+msg.getId(),Scheduler.DEFAULT_GROUP,new Date(),null,SimpleTrigger.REPEAT_INDEFINITELY,60L*10000L);
  try{
  scheduler.schedulerJob(jobDetail,sm);
  }catch(Exception ex){

  }
  scheduler.unscheduleJob("InitTrigger",Scheduler.DEFAULT_GROUP);//初始化任务只执行一行,执行一次之后移除初始化触发器
  scheduler.start();
  }
  }
}

public class SendMsg implements Job
{
  private MsgDao msgDao;
  //msgDao get,set方法,略
  ...
  public void execute(JobExecutionContext je) throws JobExecutionException{

  ......略
  List list=msgDao.getNeedMsg() ;//问题出在这里没法获得 msgDao

  .....do sth.  
  }
}

现在主要出现两个问题:

1.实现的job没法得到spring已经注入的dao,出现空指针,先前第一次初始化任务可以得到dao,但是动态生成任务,实现job,无法得到spring注入的属性dao,这是什么原因,请各位XDJM看有什么办法,困扰好久了...万分感谢...
2..多任务创建存在job和group id已经存在的错误,因为这些id是从数据库获取

[解决办法]
你读取spring配置文件啦吗?ApplicationContext……
[解决办法]
<property name="schedulerContextAsMap">
<map>
<!-- spring 管理的service需要放到这里,才能够注入成功 -->
<description>schedulerContextAsMap</description>
<entry key="xxService或xxRepo" value-ref="xxService或xxRepo" />
</map>
</property>
[解决办法]

XML code
 <!--  定时执行任务的类,要继承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 > 


[解决办法]

XML code
<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>
[解决办法]
楼主所指的动态生成任务没有得到msgDao,但这个动态生成的任务是如何创建SendMsg 实例的呢?反射吗?反射的话就得不到msgDao了。只有取spring容器中的SendMsg 实例才能取到msgDao,通过反射创建的SendMsg实例,spring并没有将msgDao注入其中。

第2个问题没看懂,不知道是不是由于第1个问题引起的。
[解决办法]
探讨
第二个问题,错误提示如下:org.quartz.ObjectAlreadyExistsException:Unable to store Job withe name:'smsJob_222' and group:'DEFAULT',because one already exists with this identification

热点排行