欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

quartz定时任务,Cron表达式前端验证以及展示最近几次执行时间。

程序员文章站 2022-05-24 21:02:47
...
  1. 后台代码编写
    引入quartz的包
    <dependency>
  		<groupId>org.quartz-scheduler</groupId>
  		<artifactId>quartz</artifactId>
  		<version>${quartz.version}</version>
  	</dependency>
  	
   代码编写
      public  List<String> getRecentTriggerTime(String cron) {
  		List<String> list = new ArrayList<String>();
  		try {
  			CronTriggerImpl cronTriggerImpl = new CronTriggerImpl();
  			cronTriggerImpl.setCronExpression(cron);
  			// 6 代表获取6次
  			List<Date> dates = TriggerUtils.computeFireTimes(cronTriggerImpl, null, 6);
  			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  			for (Date date : dates) {
  				list.add(dateFormat.format(date));
  			}
  		} catch (Exception e) {
  			logger.error("cron表达式输入有误.......................");
  			list.add("cron表达式输入有误");
  		}
  		return list;
  	}

  1. 前端代码编写
   <div id="dialogRule" style="display: none">
				<div class="row1" id="timeForm">
					<label>表达式字段</label>
					<div class="col-input">
						<p>秒</p>
						<input type="text" id="second">
					</div>
					<div class="col-input">
						<p>分钟</p>
						<input type="text" id="minute">
					</div>
					<div class="col-input">
						<p>小时</p>
						<input type="text" id="hour">
					</div>
					<div class="col-input">
						<p>日</p>
						<input type="text" id="day">
					</div>
					<div class="col-input">
						<p>月</p>
						<input type="text" id="month">
					</div>
					<div class="col-input">
						<p>星期</p>
						<input type="text" id="week">
					</div>
					<div class="col-input">
						<p>年</p>
						<input type="text" id="year">
					</div>
				</div>
				<div class="row1">
					<label>Cron表达式</label>
					<div class="col-input-7">
						<input type="text" id="express" >
					</div>
					<button onclick="CronExecute()">执行</button>
				</div>
				<div>
					<label>执行结果</label>
					<div id="result" style="padding-left: 10px;"></div>
				</div>
			</div>

            <script>
	
	          $('#timeForm input').bind('input propertychange', function(){
		       var expressVal = $("#second").val()+" "+$("#minute").val()+" "+$("#hour").val()+" "+$("#day").val()+" "+$("#month").val()+" "+$("#week").val()+" "+$("#year").val();
		       $("#express").val(expressVal)	
			   })  
			   
			   $('.col-input-7 input').bind('input propertychange', function(){
						var text= $("#express").val();
						text=text.replace(/[\s ]+/g, "-+")
						var arry=[7]
						arry= text.split("-+");	
						$("#second").val(arry[0]);
						$("#minute").val(arry[1]);
						$("#hour").val(arry[2]);
						$("#day").val(arry[3]);
						$("#month").val(arry[4]);
						$("#week").val(arry[5]);
						$("#year").val(arry[6]);
		      
			   })  

			 function  CronExecute(){
				 if($("#express").val()==""||$("#express").val()==null){
					 layer.msg("表达式字段不能为空")
					 return;
				 }
				$.ajax({
                        url : '后端代码对应的url',
                        dataType : 'json',
                        type : 'POST',
                        data : {cron: $("#express").val()},
                        success : function(res) {
							var rest="最近6次执行时间:<br>";
							if(res.length>0){
								for(var i=0;i<res.length;i++){
									if(res[i]=="cron表达式输入有误"){
										$("#result").html("<span style='color:red;'>cron表达式输入有误</span>")
										return
									}
									rest+=res[i]+"<br>"
								}	
							}else{
								rest="<sapn style='color:red;'>当前时间已超过执行时间</sapn>"
							}
						    $("#result").html(rest)
                        },
                        error: function(){
                           $("#result").html("<span style='color:red;'>服务器内部错误</span>")
                        }
                    });
			   }
			   
    	</script>	

     <style>
		#dialogRule{
			width: 740px;
			font-size: 12px;

		}
		#dialogRule .row1{
			height: 45px;
		}
		#dialogRule label{
			margin-top: 30px;
			padding:10px;
			width: 90px;
		}
		.col-input{
			display: inline-block;
			text-align: center;
			width: 80px;
			margin-left: 5px;
		}
		.col-input input{
			width: 80px;
			height: 20px;
			line-height: 20px;

		}
		.col-input-7{
			display: inline-block;
			text-align: center;
			width: 520px;
			margin-left: 5px;
			margin-right: 5px;
		}
		.col-input-7 input{
			width: 100%;
			height: 20px;
			line-height: 20px;
		}
		</style>
  1. 说明
本文用的是bootstrap样式,需要引用jquery.js,其他自定义样式需要自己去调试
相关标签: cron