计划任务中发生意外错误

时间:2017-03-28 15:58:36

标签: spring-boot scheduler

我尝试使用spring-boot在应用程序中使用applicationConfig运行计划任务,默认情况下只添加注释@EnableScheduling并使用下一个代码中的scheluded方法配置类

@Component 
public class Task { 

 @Scheduled(cron ="*/10 * * * * *")
  public void init() {
    System.out.println("QuartzConfig initialized.");
    System.out.println(callService());
  }

  private String callService() {
    String urlService = "https://www.google.com";
    GetMethod method = new GetMethod(urlService );
    String response = "";
    try {
        client.executeMethod(method);
        response = method.getResponseBodyAsString();
    } catch (IOException e) {
       e.printStackTrace();
    } finally {
        method.releaseConnection();
    }
    return response;
  }
}

但是当任务调用方法时,callService的回答是

java.lang.NullPointerException: null

1 个答案:

答案 0 :(得分:0)

感谢我解决了这个注释,将下一个代码添加到init方法

 public Task(){     
          client = new HttpClient();
          client.getParams().setParameter("http.useragent", "Bacon/1.0");
    }

感谢@edgarNgwenya