我正在运行 spring-boot 应用程序,我已经实现了石英调度程序作为应用程序的一部分。
早些时候,我有一个带有端点的 rest 控制器,例如 http://localhost:8080/GoogleMail/{id},它触发如下所示的函数,并接受 HttpServletRequest/Response 作为参数以及我正在传递的 Pathvariable。
@PostMapping(value = "/GoogleMail/{id}", consumes = "application/json", produces = "application/json")
public String sendMail(HttpServletRequest request, HttpServletResponse response, @Valid @PathVariable(value = "id") String id,
@Valid @RequestBody MailMessage mailMsg) throws Exception {
if(id == null || id.isEmpty()) {
ResponseEntity.badRequest().build();
}
this.userId = id;
return GoogleMailIntegrationService.sendUserMails(request, response, id, mailMsg,
m -> !StringUtils.isBlank(mailMsg.getTo())
&& !StringUtils.isBlank(mailMsg.getSubject())
&& !StringUtils.isBlank(mailMsg.getBody()));
}
现在,我需要通过使用 Quartz 调度程序每 1 小时发布一次 JSON 正文来调用此函数,而不是进行 REST 调用。可能如下所示
if (context.getJobDetail().getKey().getName().equalsIgnoreCase(JobName.READRESPONSE.toString())) {
// emailService.readMail();
try {
sendMail(Request, Response, id);
} catch (IOException e) {
e.printStackTrace();
}
我的问题:有没有办法使用调度程序进行 REST 调用,或者是否可以通过直接传递请求/响应参数来进行 sendMail(( 调用。
我不确定如何做到这一点,在发布之前,我大部分时间都在浏览解决方案。
RestTemplate 通过以下方式对某个控制器端点执行请求调用:
if (context.getJobDetail().getKey().getName().equalsIgnoreCase(JobName.READRESPONSE.toString())) {
// emailService.readMail();
try {
RestTemplate restTemplate = new RestTemplate();
HttpEntity<MailMessage > request = new HttpEntity<>(mailMsg, new HttpHeaders());
ResponseEntity<String> responseEntityStr =
restTemplate.postForEntity(
String.format("http://localhost:7777/GoogleMail/%s", id),
request, String.class);
} catch (IOException e) {
e.printStackTrace();
}