如何编写每6个月更新一次的Quartz计时器服务?

时间:2016-04-23 12:07:40

标签: mysql spring quartz-scheduler spring-data-jpa

我想为下面的更新代码提供Quartz计时器服务。每六个月,所有用户&#39;密钥应根据其//dataTypes are date only List<User> allDbUsers=userRepository.findAll(); Date currDate=new Date(); final long DAY_IN_MILLIS = 1000 * 60 * 60 * 24; for(User user:allDbUsers) { Date creationDate=user.getCreationDate(); String firstKey=user.getFirstKey(); Long diffInDays=(currDate.getTime()-creationDate.getTime())/DAY_IN_MILLIS; if(diffInDays==180) { user.setSecondKey(firstKey); user.setFirstKey("aH&#KK"); userRepository.saveAndFlush(user); } }

进行更新
while True:  #first while loop - repeats entire program unless broken 
while True: #second while loop - repeats input section of code unless broken
    currentcurrency = input ("Select a starting currency: ") #allows user to input their starting currency, saved as a string in variable 'currentcurrency'
    print ('You selected %s' % currentcurrency) #prints inputted string

    value = float(input ("Input your current value: ")) #allows user to input their value of money, saved as a float in variable 'value'

    newcurrency =  input ("Select a new currency: ")  #allows the user to input their new currency, saved as a string in variable 'newcurrency' 
    if currentcurrency == 'Pound' and newcurrency == 'Pound': #this part of the code determines the convertor value based upon the two inputted currencies
        convertor = 1
    if currentcurrency == 'Pound' and newcurrency == 'Euro':
        convertor = 1.34
    if currentcurrency == 'Pound' and newcurrency == 'Dollar':
        convertor = 1.46
    if currentcurrency == 'Pound' and newcurrency == 'Yen':

        convertor = 171.61
    if currentcurrency == 'Euro' and newcurrency == 'Pound':
        convertor = 0.75

    if currentcurrency == 'Euro' and newcurrency == 'Euro':
        convertor = 1
    if currentcurrency == 'Euro' and newcurrency == 'Dollar':
        convertor = 1.09
    if currentcurrency == 'Euro' and newcurrency == 'Yen':
        convertor = 127.47
    if currentcurrency == 'Dollar' and newcurrency == 'Pound':
        convertor = 0.69
    if currentcurrency == 'Dollar' and newcurrency == 'Euro':
        convertor = 0.88
    if currentcurrency == 'Dollar' and newcurrency == 'Dollar':
        convertor = 1
    if currentcurrency == 'Dollar' and newcurrency == 'Yen':
        convertor = 121.12
    if currentcurrency == 'Yen' and newcurrency == 'Pound':
        convertor = 0.0062
    if currentcurrency == 'Yen' and newcurrency == 'Euro':
        convertor = 0.0076
    if currentcurrency == 'Yen' and newcurrency == 'Dollar':
        convertor = 0.0083
    if currentcurrency == 'Yen' and newcurrency == 'Yen':
        convertor = 1

    print ("Do you want to convert", (value), (currentcurrency), "to", (newcurrency), "?")
    answer = input ("Yes/No: ")  #allows the user to input a response, saved in variable 'answer'
    if answer == 'Yes' or 'y' or 'yes': #if the user inputted string is 'Yes' or equivelant,
        break                           #the while loop will break, and the program will continue

result = (convertor) * (value)          #calculates the result by multiplying the user inputted value and the converter
result *= 100                           #this part of the code calculates the result to two decimal places
result += 0.5                           #it multiplies the result by 100 and adds 0.5
result = int(result)                    #then changes the result to an integer
result /= float(100)                    #divides the result by a 100 and changes it to a float
print (result, newcurrency)             #prints the result, followed by the new currency

answer2 = input("Do you want to convert again? (Yes/No): ") #allows the user to input an answer
if answer2 == 'No' or 'n' or 'no': #if the inputted answer is 'No' or equivalent,
    break                          #the while loop breaks and the code continues - otherwise, it repeasts the program
exit()                                 #ends the program 

1 个答案:

答案 0 :(得分:0)

您可以将Calendar用于日期之间的视图差异,例如:

    Calendar currentDate = Calendar.getInstance();
    Calendar sixMonthAfterCreation;

    for (User user : allDbUsers) {
        sixMonthAfterCreation = Calendar.getInstance()
        sixMonthAfterCreation.setTime(user.getCreationDate());
        sixMonthAfterCreation.add(Calendar.MONTH, 6);

        String firstKey = user.getFirstKey();

        if (currentDate.after(sixMonthAfterCreation)) {
            user.setSecondKey(firstKey);
            user.setFirstKey("aH&#KK");
            userRepository.saveAndFlush(user);
        }
    }

或者如果您使用java 8,您可以将创建日期转换为LocalDate并查看差异。

如果我理解你,你需要使用Quartz Scheduler。我没有使用它,但我认为您需要实现org.quartz.Job,覆盖execute方法并编写代码来更新其中的用户。

有关Quartz Scheduler的更多信息,请查看this question

一个澄清此代码不适用于运行调度程序,它是方法job.excecute的代码(必须完成的工作)。调度程序应该每隔午夜运行一次(例如),然后每个午夜都会调用execute方法查找所有用户进行更新并执行此操作。