Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在春季为ScheduledTimerTask设置服务器时间?_Java_Spring_Timer_Job Scheduling - Fatal编程技术网

Java 如何在春季为ScheduledTimerTask设置服务器时间?

Java 如何在春季为ScheduledTimerTask设置服务器时间?,java,spring,timer,job-scheduling,Java,Spring,Timer,Job Scheduling,我们正在使用ScheduledTimerTask管理应用程序中的作业(自动)。我们正在使用以下代码: <bean id="SampleTask" class="org.springframework.scheduling.timer.MethodInvokingTimerTaskFactoryBean"> <property name="targetObject" ref="sampleScheduler" /> <property name=

我们正在使用ScheduledTimerTask管理应用程序中的作业(自动)。我们正在使用以下代码:

<bean id="SampleTask" 
  class="org.springframework.scheduling.timer.MethodInvokingTimerTaskFactoryBean">
    <property name="targetObject" ref="sampleScheduler" />
    <property name="targetMethod" value="runMethod" />
</bean>

<bean id="sampleScheduler" class="com.sample.SampleScheduler" />

<bean id="timerTask"
    class="org.springframework.scheduling.timer.ScheduledTimerTask">
    <property name="timerTask" ref="schedulerTask" />
    <property name="delay" value="1000" />
    <property name="period" value="60000" />
</bean>

<bean class="org.springframework.scheduling.timer.TimerFactoryBean">
    <property name="scheduledTimerTasks">
        <list>
            <ref local="timerTask" />
        </list>
    </property>
</bean>

通过使用上面的代码
runMethod
每一分钟都可以正常工作。但是我们希望根据服务器时间更改执行。例如,如果服务器中的时间是下午6点,那么我们需要调用该函数。如何实现它


非常感谢您的帮助。

您可以使用Spring的任务触发器在特定时间段调用,您可以使用
CronTrigger


详细的文档是

如果您必须只使用ScheduledTimerTask,则每分钟调用一次计时器任务。 现在,在计时器任务中,检查时间是否为早上6点,并运行必要的功能。 否则就回来

反对: 这项不可操作的任务将每分钟执行一次(除非有一次它必须进行一些处理)

从spring只调用TimeTask一次。 当任务执行时,您可以获得当前时间,并可以计算当前时间与执行实际任务的时间之间的差异(将此差异称为延迟)

现在,从该计时器任务中的开始,启动一个新的执行器Executors.newScheduledThreadPool(1) 和scheduleAtFixedRate,初始延迟=延迟
并将周期设置为1天

Spring Quartz SchedulerFactoryBean

applicationcontext.xml


要按照xml中的配置执行的特定类的方法

公共类MyTask{
私营企业;
私人电子邮件;
公共静态int size=10;
公共空间{
System.out.println(“你好!!!”;
int currSize=0;
如果((currSize=edao.emp_count())!=size){
大小=当前大小;
System.out.println(“+大小”中的记录以DB为单位更改);
email.sendMail();
}否则{
System.out.println(“未添加/删除的记录”+当前大小);
}
(或基于日期)
DateFormat DateFormat=新的简化格式(“dd/MM/yyyy”);
String today=dateFormat.format(new Date());
System.out.println(“当日:+今日”);
Calendar cal=Calendar.getInstance();
cal.setTime(新日期());//当前日期
int numOfDays=cal.get(日历年的第几天);
System.out.println(“一年中的某一天:+numOfDays”);
Date edate=dateFormat.parse(“2015年10月27日”);
系统输出打印项次(“结束日期:+edate”);
cal.setTime(edate);//未来日期
int numOfpdays=cal.get(一年中的日历日);
final int fireTrigger=numOfDays-numOfDays;
System.out.println(“Shedlue:+fireTrigger”);
//如果(fireTrigger>0&&fireTrigger<10){//发送邮件..}
//...
}
  • 若触发器时间很短,它可能不会完全执行方法,因为在得到响应之前,下一个触发器可能会触发

  • 请求传递到DBS,但不返回并执行if{}代码。任何人都可以解决这个问题


使用。

感谢您的快速回复。但是我们可以为ScheduledTimerTask执行sam操作吗?因为如果我们使用cronTrigger,我们需要更改代码。您可以通过删除延迟并提供24小时的时间段,每天触发一次,从那时起它将每天调用一次,但我认为TimerTask不可能精确计时。还要注意SpringTimerTask将使用java.util.TimerTask,我们对此调用时间没有任何发言权。因此,我担心我们也无法在spring timer task中指定确切的时间。ok.Tahnks很多,但我们需要使用哪一个来触发,而不是延迟和周期。使用CronTrigger它将准确地给出您想要的。一些示例:1.2。
<!-- quartz -->

<bean id="emial" class="quartzcore.QuartzEmail"/>

    <bean id="myTask" class="quartzcore.MyTask" >
        <property name="edao" ref="empdao"/>
        <property name="email" ref="emial"/>        
    </bean>
<!--
    <bean id="schedulerTask" class="org.springframework.scheduling.timer.MethodInvokingTimerTaskFactoryBean">
        <property name="targetObject" ref="myTask" />
        <property name="targetMethod" value="sayHello" />
    </bean>

<bean id="timerTask" class="org.springframework.scheduling.timer.ScheduledTimerTask">
    <property name="timerTask" ref="schedulerTask" />
    <property name="delay" value="5000" />
    <property name="period" value="5000" />
</bean>

<bean class="org.springframework.scheduling.timer.TimerFactoryBean">
    <property name="scheduledTimerTasks">
        <list>
            <ref local="timerTask" />
        </list>
    </property>
</bean>
 -->

    <bean name="quartzJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject" ref="myTask" />
        <property name="targetMethod" value="sayHello" />
    </bean>

    <bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean"> 
        <property name="jobDetail" ref="quartzJob" />
        <property name="repeatInterval" value="1000" />
        <property name="startDelay" value="1000" />
    </bean>

    <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <property name="jobDetail" ref="quartzJob" />
        <property name="cronExpression" value="0/15 * * * * ?" /> 
    </bean>

    <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="jobDetails">
            <list>
                <ref bean="quartzJob" />
            </list>
        </property> 
        <property name="triggers">
            <list>
                <ref bean="cronTrigger" />
            </list>
        </property>
    </bean>

    </beans>
public class MyTask {

    private EmpDao edao;        
    private QuartzEmail email;    
    public static int size = 10;

    public void sayHello() {
        System.out.println("Hello !!! ");

        int currSize = 0;
        if ((currSize = edao.emp_count()) != size) {
            size = currSize;
            System.out.println("####### Records Changed in DB : "+size);
            email.sendMail();               
        }else {
            System.out.println("Records not added/removed. "+currSize);
        }
         (or base on dates)
        DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
        String today = dateFormat.format(new Date());
        System.out.println("Current Day : "+today);

        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date()); // Current date
        int numOfDays = cal.get(Calendar.DAY_OF_YEAR);
        System.out.println("Day of Year : "+numOfDays);

        Date edate = dateFormat.parse("27/10/2015");
        System.out.println("End Date : "+edate);
        cal.setTime(edate);  // Future Date
        int numOfpdays = cal.get(Calendar.DAY_OF_YEAR);
        final int fireTrigger = numOfpdays - numOfDays;
        System.out.println("Shedlue : "+fireTrigger);
        //if(fireTrigger > 0 && fireTrigger < 10 ){ //send a mail.. }   
    //...
}