java如何写一个简单的定时任务?

使用java自带类Timer

通过import java.util.Timer导入Timer类,定时任务实现通过Timer的scheduler方法,scheduler方法包括三个入参,分别是定时任务,delay(任务执行后多久执行,单位ms),和period(多久执行一次,单位ms)

定时任务可以通过新写个类继承TimerTask类,通过重写run方法实现业务逻辑,如下为对一个post接口实现定时调用的代码实现

1.定时任务类实现如下

package com.example.scheduletask;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.TimerTask;

import com.alibaba.fastjson.JSON;
//编写定时任务类继承TimerTask方法
public class SchedulerTask extends TimerTask {
//    重新run方法,写业务逻辑
    @Override
    public void run() {
        try {
//            调用机器人接口,打印请求体
            System.out.println("hello world");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
}

2.启动类实现如下

package com.example.scheduletask;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.io.IOException;
import java.util.Timer;

@SpringBootApplication
public class ScheduleTaskApplication {
    public static void main(String[] args) throws IOException {
        SchedulerTask schedulerTask=new SchedulerTask();
//使用自带Timer类,生成定时任务
        Timer timer=new Timer();
//        延迟1s执行,每隔12h执行一次
        timer.schedule(schedulerTask,1000,43200000);
        SpringApplication.run(ScheduleTaskApplication.class, args);
    }

}

这里是12h执行一次定时任务,定时任务功能为打印一次hello world到控制台。我们还可以重新run方法为自己想要的业务逻辑,如定时调用某个接口(通过httpclient方法),实现定时提醒功能等。
调用接口的例子如下所示,为了安全这里隐去接口名和请求体

public static String request() throws UnsupportedEncodingException {
        HttpClient httpClient = new HttpClient();
        PostMethod postMethod = new PostMethod("这里应该输入你要请求接口的url");
        postMethod.addRequestHeader("Content-Type", "application/json");
//        注意 要提前把字符串转换成json格式
        String requestBody = "body体,注意要提前转成json格式";
        RequestEntity entity = new StringRequestEntity(requestBody, "application/json", "UTF-8");
        postMethod.setRequestEntity(entity);
        String res = "";
        try {
            int code = httpClient.executeMethod(postMethod);
            System.out.println(code);
            if (code == 200) {
                res = postMethod.getResponseBodyAsString();
                System.out.println(res);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return res;

    }

3.打成jar包上传到虚拟机,并在虚拟机启动

程序打包

4.在虚拟机上执行

通过java -jar jar包名 启动程序
如 java -jar scheduleTask-0.0.1-SNAPSHOT-jar-with-dependencies.jar

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值