定时任务(一种在xml配置,另一种在类加入注解)
来源:互联网 发布:python 读写文本文件 编辑:程序博客网 时间:2024/06/03 20:46
//第一种public class TsetS extends QuartzJobBean{ private static final Logger logger = Logger.getLogger(TsetS .class); /** * 比如测试发送短信 * TODO * @Title: testsend */ public void testsend() { try { String mobile="手机号码"; if (这里判断手机格式是否正确(自己写方法)) { String codes = Tools.random(6);// 6位验证码 1//调用你自己短信接口 2.//例如 方法返回值 String smsresult = Tool.send(手机号,codes); 3.判断 } else { logger.info("手机号码格式不正确" +DateTime.getCurrentDate_YYYYMMDDHHMMSS()); } /*}*/ } catch (Exception e) { logger.error(e, e); } }}
//下面是xml配置<!--定时任务启动配置 --> <bean id="" class="com.test.TsetS"></bean> <!-- 测试短信发送 --> <bean id="TestjobBean" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject"> <ref bean="TsetS"/> </property> <property name="targetMethod"> <!-- 对应方法名称--> <value>testsend</value> </property> </bean> <!-- 触发器 --> <bean id="tsetTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail"> <ref bean="TestjobBean" /> </property> <property name="cronExpression"> <!--每天下午4点57执行一次 --> <value>0 57 16 * * ?</value> </property> </bean> <!--Spring触发工厂 这里开启才能执行 --> <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <!-调用的是触发器的id --> <ref bean="tsetTrigger"/> </list> </property> </bean> -->
第二种@Componentpublic class Test2{private static final Logger logger = Logger.getLogger(Test2.class); //要加入Scheduled然后后面配置时间 @Scheduled(cron="0 03 20 * * ?") public void smsuser(){ try { //这里也是一样的 1.//调用你自己短信接口 2.//例如 方法返回值 3.//生成随机验证码 4.//判断 if(如果返回值不相等) { System.out.println("用户短信发送失败!"); }else{ System.out.println("用户长短信发送成功"); } } catch (Exception e) { logger.error(e,e); System.out.println("..."); } } //两个时间的差值 public static int getMonthNum(Date date1,Date date2) { Calendar cal1=Calendar.getInstance(); cal1.setTime(date1); Calendar cal2=Calendar.getInstance(); cal2.setTime(date2); return (cal2.get(1)-cal1.get(1))*12+(cal2.get(2)-cal1.get(2)); }
xml 配置里面和上面一样 只不过是最后加入 <!-- 定时器开关 开始--><task:annotation-driven/>应该就行了第一次写。如果写的不好。请各位多指教谢谢。
0 0
- 定时任务(一种在xml配置,另一种在类加入注解)
- 如何在Spring中配置定时任务
- 在SSM框架中配置定时任务
- 定时任务-在spring中配置quartz
- 在SSM框架中配置定时任务
- 定时任务-在spring中配置quartz
- 定时任务--在Spring 中配置Quartz
- 定时任务-在spring中配置quartz
- 如何在spring中配置定时任务
- Spring3注解配置定时任务
- xml中配置定时任务
- 定时任务xml文件配置
- spring定时任务-基于注解和xml
- 在Listener(监听器)定时启动的TimerTask(定时任务)中使用Spring@Service注解的bean
- Spring3.X 配置(注解)定时任务
- spring 注解方式配置定时任务
- java spring注解方式配置定时任务
- spring 注解方式配置定时任务
- SpringMVC整合JPA小结
- Hadoop—Hadoop1.X和2.X安装练习
- jQuery中的.bind()、.live()和.delegate()之间区别分析
- 关于配置局域网SVN
- LD_PRELOAD环境变量
- 定时任务(一种在xml配置,另一种在类加入注解)
- Jackson学习对象与JSON互相转化
- static trap
- jQuery实现鼠标滑过延时显示的效果
- xcode 7 beta 启动崩溃解决办法
- interactive_timeout与wait_timeout
- Linux 删除用户出现提示userdel: user ** is currently logged in
- Redis学习手册(服务器管理)
- HDU 1513------Palindrome