1.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task.xsd">
<!-- 配置job -->
<bean id="job1" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 这个就是具体实现类,如果是注解,则必须为component指定value -->
<property name="targetObject">
<bean class="com.neusoft.si.service.task.MessageTaskService"/>
</property>
<!-- targetMethod是Spring定时器的特殊属性【定时任务方法名】 -->
<property name="targetMethod" value="sendMessage" />
<!-- false表示job不会并发执行,默认为true -->
<property name="concurrent" value="false" />
</bean>
<bean id="job2" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 这个就是具体实现类,如果是注解,则必须为component指定value -->
<property name="targetObject">
<bean class="com.neusoft.si.service.task.MessageTaskService"/>
</property>
<!-- targetMethod是Spring定时器的特殊属性【定时任务方法名】 -->
<property name="targetMethod" value="test" />
<!-- false表示job不会并发执行,默认为true -->
<property name="concurrent" value="false" />
</bean>
<!-- 配置调度任务执行的触发的时间 -->
<bean id="cronTrigger1" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="job1" />
<!-- 每天早上六点执行一次
<property name="cronExpression" value="0 0 6 * * ?" /> -->
<!--每分执行一次
<property name="cronExpression" value="0 0/1 * * * ?" /> -->
<!--每1秒执行一次
<property name="cronExpression" value="0/1 * * * * ?" /> -->
<!--每两个小时执行一次 -->
<property name="cronExpression" value="0 0 0/2 * * ?" />
</bean>
<bean id="cronTrigger2" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="job2" />
<!-- 每天早上六点执行一次
<property name="cronExpression" value="0 0 6 * * ?" /> -->
<!--每分执行一次
<property name="cronExpression" value="0 0/1 * * * ?" /> -->
<!--每1秒执行一次 -->
<property name="cronExpression" value="0/1 * * * * ?" />
</bean>
<!-- 配置Quartz的调度工厂 -->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="cronTrigger1" />
<ref bean="cronTrigger2" />
<!-- <ref bean="simpleTrigger"/> -->
</list>
</property>
</bean>
<!-- task任务扫描注解 -->
<task:annotation-driven/>
</beans>
2.Java文件
public class MessageTaskService {
protected Logger jobLogger = LoggerFactory.getLogger(getClass());
public static final String SENDMESSAGE1 = "messageRegister";
public static final String SENDMESSAGE2 = "messageNotification";
public static final String SENDMESSAGE3 = "messageAccount";
// 该定时任务是否启用
@Value("#{PROPERTIES_TASK['taskEnable']}")
private boolean taskEnable;
@Autowired
private MessageCenterService messageCenterService;
/**
* 消息推送【账户动态】
* @param categoryId
*/
public void sendMessage() {
try {
while (true) {
//分页查询【帐户动态】未推送消息
PaginationEntity<MessageUnSendEntity> unSendList = messageCenterService.getUnReadAccountMsgList(1);
if (unSendList.getRecordCount() == 0) {
break;
}
for (MessageUnSendEntity messageUnSendEntity : unSendList.getList()) {
PushResult result = messageCenterService.unSendMsgToSendedMsg(messageUnSendEntity);
if (result != null) {
// 服务器端内部逻辑错误(jpush可能达到每分钟600次上限),请稍后重试。
if (1000 == result.getResponseCode() || 1030 == result.getResponseCode()) {
Thread.currentThread().sleep(1000 * 60);
}
}
}
}
} catch (InterruptedException e) {
throw new BusinessException("账户动态推送消息异常中断!");
}
}
public void test() {
System.out.println("测试定时任务");
}
}