`
wlh269
  • 浏览: 453261 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Spring定时任务

阅读更多
1.配置文件:ScheduledTimerTask是spring内置的一个类,
    在它的timerTask标签中引入我们需要定时执行的业务代码,这里例如,targetSMSQueryTaskBiz;在period标签中加入执行频率,单位为毫秒;


 <bean id="targetSMSQueryTaskBiz"  class="com.hollyinfo.invest.biz.message.SMSQueryBizImpl">
	      <property name="fileClient">
	       <ref bean="fileClient"/>
	      </property>
	      <property name="messageDao">
				<ref bean="messageDao" />
		  </property>
	  </bean>
	 <bean id="scheduledSMSQureyTask" class="org.springframework.scheduling.timer.ScheduledTimerTask">
    	<property name="timerTask">
      		<ref bean="targetSMSQueryTaskBiz"/>
    	</property>
   		<property name="period">
      	  <value>60000</value>
    	</property>
     </bean>
	<bean class="org.springframework.scheduling.timer.TimerFactoryBean">
		<property name="scheduledTimerTasks">
     	 <list>
        	<ref bean="scheduledSMSQureyTask"/>
     	 </list>
    	</property>
   </bean>
   	<bean id="SMSQueryTaskBiz" parent="jdbcTransactionProxy">
		<property name="target" ref="targetSMSQueryTaskBiz" />
	</bean>



2.业务类代码,这里为SMSQueryBizImpl,该类继承TimerTask,而TimerTask实现了Runnable接口,所以我们的业务类要实现run()方法;
public class SMSQueryBizImpl extends TimerTask implements SMSQueryBiz  {
	/** 日志初始化 */
	private static HollyinfoLog LOG = HollyinfoLogger.getLog(MessageBizImpl.class);
	private SMSFileTransClient fileClient;
	public void setFileClient(SMSFileTransClient fileClient) {
		this.fileClient = fileClient;
	}
	/** 消息数据通道接口 */
	private MessageDao messageDao;
	
	public void setMessageDao(MessageDao messageDao) {
		this.messageDao = messageDao;
	}
	public SMSQueryBizImpl() {
		
	}
	 public void run() {
		 try{
			//查询待接收结果文件
	    	List fileNames=fileClient.queryFileList();
	    	for (Iterator iterator = fileNames.iterator(); iterator.hasNext();) {
				String fileName = (String) iterator.next();
				int idx=fileName.indexOf(".");
				String preName=fileName.substring(0, idx);
				//下载结果文件
				boolean flag=fileClient.download(preName, "txt");
				if(flag){
					LOG.info("下载结果文件"+fileName+","+flag);
					Thread.sleep(10000);
					 boolean delflag=false;
					 if(readLastLine(new File(SystemParameter.smsResultDir+File.separator+fileName),"utf-8").indexOf("EOF")!=-1){
						 //删除服务端已下载文件
						 delflag=fileClient.deleteFile(preName, "txt");
						 //更新发送结果到核心系统
						 updateSendResults(fileName);
					  }
					 //删除客户端已下载文件
					 delFile(new File(SystemParameter.smsResultDir+File.separator+fileName));
					  
			    	LOG.info("删除中间服务结果文件"+fileName+","+delflag);	 
			    	 
			    }else{
					LOG.info("下载结果文件"+fileName+","+flag);
				}
			}
	    	
	    	//======检查是否还有没上传成功的文件,做重新上传操作=====//
	    	 String [] filenames=new File(SystemParameter.smsSendDir).list();
	    	 for (int i = 0; i < filenames.length; i++) {
             	 File tmpFile=new File(SystemParameter.smsSendDir+File.separator+filenames[i]);
             	 Pattern p = Pattern.compile("sms_send_list.+?txt");
             	 Matcher fMatcher = p.matcher(tmpFile.getName());
             	 //如果后缀为txt开头为sms_send_list,且内容不为空的文件
             	 if (fMatcher.matches()&& NotEmptyFile(tmpFile)&&readLastLine(tmpFile,"utf-8").indexOf("EOF")!=-1) {
             		boolean hasUpload=fileClient.upload(filenames[i], tmpFile, "txt");
             		if(hasUpload){
            			LOG.info("重复上传待发催报短信息文件:"+filenames[i]+"成功!");
            			try {
    						Thread.sleep(10000);
    					} catch (InterruptedException e) {
    						e.printStackTrace();
    					}
    					//删除已经上传的信息文件
            		   boolean flag =delFile(tmpFile);
            		   if(flag){
            			   LOG.info("删除待发催报短信息文件:"+filenames[i]+"成功!");   
            		   }
            		   
            		}else{
            			LOG.info("重复上传待发催报短信息文件:"+filenames[i]+"失败!");
            		}
             	 }
	    	 } 
	    	
		 }catch (InterruptedException e) {
				e.printStackTrace();
		 }
	   }
}

分享到:
评论

相关推荐

    spring定时任务关键jar包(齐全)

    本文将详细探讨Spring定时任务的关键知识点,并与提供的jar包列表关联。 首先,Spring定时任务主要依赖于`spring-context-support`模块,这个模块包含了处理定时任务所需的类和接口。在压缩包`lib`中,应该包含了这...

    Spring定时任务管理

    Spring定时任务的几种实现,欢迎交流!

    spring定时任务依赖的jar包

    2. **依赖的jar包**:实现Spring定时任务,通常需要以下10个关键的jar包: - `spring-context`: 包含了Spring的核心功能,如依赖注入(DI),AOP,事件处理等,是实现定时任务的基础。 - `spring-context-support`: ...

    spring定时任务执行两次的异常排查处理

    一个tomcat下部署了两个应用,一个是普通web应用syncc,另一个应用syncc_wx属于微信公众号后台程序涉及消息定时推送,tomcat未分离...”spring定时任务执行两次的异常排查处理.docx"针对上述描述问题进行分析和解决。

    spring定时任务

    spring定时任务 xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation=...

    Spring 定时任务

    ### Spring 定时任务 在Spring框架中,定时任务是一个非常重要的特性,它允许开发者以简单的方式实现周期性任务的调度。Spring通过`@Scheduled`注解提供了对定时任务的支持,该注解可以轻松地应用于任何Java方法上...

    spring定时任务配置

    spring定时任务SimpleTrigger 和CronTrigger 配置

    Spring定时任务实现(非Web项目)

    在非Web项目中实现Spring定时任务,主要步骤如下: 1. **配置Spring Task**:在Spring的配置文件(如`applicationContext.xml`或使用Java配置类)中,我们需要启用任务调度功能并配置相应的执行器或调度器。例如,...

    spring定时任务实现

    Spring框架提供了多种方式来实现定时任务,这使得开发者可以在不同场景下选择最适合的方案。本文主要探讨了Spring中实现定时任务的三种主要方法:Java的`java.util.Timer`、Quartz库以及Spring自身的Task调度器。 ...

    Spring定时任务(Web项目)

    一、Spring定时任务简介 Spring框架的定时任务功能主要依赖于`Spring Task`模块,也称为Spring的后台任务处理。它提供了基于`@Scheduled`注解和`TaskScheduler`接口的两种定时任务实现方式。`@Scheduled`适用于简单...

    Spring 定时任务源码(spring 三种定时任务的实现方式)

    在Spring框架中,定时任务是实现系统自动化运行关键任务的重要工具。Spring提供了多种方式来创建和管理定时任务,...在chapter13目录下的文件可能包含了这些源码示例,你可以逐一研究,加深对Spring定时任务的理解。

    spring定时任务所需jar

    下面我们将深入探讨Spring定时任务所需的相关jar包以及它们的功能。 首先,Spring框架的核心jar包`spring-context.jar`是必不可少的。这个jar包包含了Spring的核心功能,如依赖注入(Dependency Injection,DI)、...

    spring定时任务demo包含jar包

    Spring定时任务是Spring框架中的一个强大特性,它允许开发者在应用程序中设置定时任务,以便在特定的时间点或按照预设的周期执行特定的业务逻辑。这个"spring定时任务demo包含jar包"提供了一个完整的示例,帮助我们...

    Spring定时任务的简单例子

    Spring定时任务支持更多的功能,比如任务执行的并发控制、任务执行的监听器、以及使用Quartz等第三方调度库进行更复杂的任务调度。 总结,Spring定时任务为开发者提供了方便的API和注解,使我们可以轻松地在Java...

    Spring定时任务@Scheduled例子

    在Spring框架中,定时任务是实现自动化...以上就是关于Spring定时任务`@Scheduled`的例子,包括其工作原理、配置以及在实际项目中的应用。理解并熟练运用这些知识,能够帮助我们构建更加高效、自动化的Spring应用程序。

    spring2.0学习笔记+spring定时任务

    标题 "spring2.0学习笔记+spring定时任务" 暗示了我们即将探讨的是关于Spring框架2.0版本的学习心得以及如何在Spring中配置和使用定时任务。在这个主题下,我们将深入理解Spring的核心概念,特别是它在企业级Java...

    spring定时任务实例

    在Spring框架中,定时任务是一项重要的功能,它允许开发者在特定的时间间隔内执行特定的任务,无需手动触发。这个实例是关于如何在Spring中配置和使用定时任务,同时结合MyBatis来向数据库插入数据。接下来,我们将...

Global site tag (gtag.js) - Google Analytics