解决方案 »
- 关于extjs 布局
- 关于url地址编码过的参数jsp页面显示问题
- 明天回家-----散分开始(二)
- hibernate session 使用filter关闭 导致页面空白
- 求一段javascript代码
- 请问各位大虾:我是一名初学者,要学习EJB,WEB SERVICE,JMS等,看什么书最好?
- 请问J2EE的应用服务器是不是就是指的web server?和传统的web server有什么区别呢
- 如何用java读写wav文件阿,请高手赐教,急急急!!
- 如何将自动递增的键作为实体bean的主键
- 高手找错误!!! ??? 多谢!!!
- spring aop管理事务的一个疑问
- jsp中,td单元格如何斜着一分为二?
<map>
<!-- spring 管理的service需要放到这里,才能够注入成功 -->
<description>schedulerContextAsMap</description>
<entry key="xxService或xxRepo" value-ref="xxService或xxRepo" />
</map>
</property>
<!-- 定时执行任务的类,要继承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 >
参考下
<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;
JobDetail jobDetail=new JobDetail("msgJob_"+msgGetId(),Scheduler.DEFAULT_GROUP,SendMsgJob.class);//可以看这是一个CLASS类,SendMsgJob.class
JobDetail jobdetail=new JobDetail("msgJob_"+msg.getId(),Scheduler.DEFAULT_GROUP,SendMsg.class);循环的时候,msg.getId有重复的吧?
问题已解决。
首先:
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 ...即可