使用特定条件和时间运行线程

1tuwyuhd  于 2021-08-20  发布在  Java
关注(0)|答案(1)|浏览(262)

我想创建一个条件,当用户下订单时,有一个线程运行20秒,以检查数据库的付款状态是完成还是挂起,如果完成,则线程将停止,如果仍然挂起,则线程将继续运行20秒,并更新要取消的数据库中的状态,我正在使用mybatis和xmlMap器
这是我的密码

public class PaymentCheck implements Runnable{
   private String username;
    public PaymentCheck(String username) {
        this.username = username;
    }

    @Override
    public void run() {
        SqlSession session = MyBatisUtil.getSqlSessionFactory().openSession();
        Booking bookStatus =  session.selectOne("User.check",username);

        String status = bookStatus.getPayment_status();
        while (status.equalsIgnoreCase("pending")){
            final Timer timer = new Timer();
            timer.scheduleAtFixedRate(new TimerTask() {
                int i = 6; // Time in seconds

                public void run() {
                    System.out.println(i--);
                    if (i < 0) {
                        timer.cancel();

                    }
                }
            }, 0, 1000);
        }
    }
}

这是我在控制器中的代码

@RequestMapping(value = "/Booking", method = RequestMethod.POST,consumes = "application/json", produces = "application/json")
public ResponseEntity<?> Booking(@RequestBody JSONObject jobj, @RequestHeader(HEADER)  String header) throws RestClientException, JsonProcessingException {

    String username = userRepo.claimToken(SECRET,PREFIX,header);
    jobj.put("username",username);
    HttpHeaders headers = new HttpHeaders();
    MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
    headers.setContentType(type);
    System.out.println(jobj.toString());
    HttpEntity<JSONObject> formEntity = new HttpEntity<JSONObject>(jobj, headers);
    JSONObject json = restTemplate.postForEntity("http://localhost:8080/hotel/Booking", formEntity, JSONObject.class).getBody();
    String message = String.valueOf(json.get("Booking Status"));

    if (!message.equalsIgnoreCase("Failed")) {
        System.out.println("json:" + json.toString());
        System.out.println(username);
        PaymentCheck tq = new PaymentCheck(username);
        tq.run();
        return new ResponseEntity<>(json,HttpStatus.CREATED);
    }else{

        return new ResponseEntity<>(json,HttpStatus.CREATED);
    }

}

这是数据库

它不会显示任何错误,但也不会更改任何内容。。。

q1qsirdb

q1qsirdb1#

你似乎把这件事复杂化了。
问题1:线程每秒轮询一次,但控制器响应不会等到更新发生后才响应客户。所以投票没有意义。
问题2:你的线程实际上没有更新数据库。
解决:
在主线程中执行检查,轮询然后响应。
为将来20秒的一次执行设置计时器任务以执行更新。
使用通用jdbc的解决方案2的示例代码。

private Timer timer = new Timer();

@RequestMapping(value = "/Booking", method = RequestMethod.POST,consumes = "application/json", produces = "application/json")
public ResponseEntity<?> Booking(@RequestBody JSONObject jobj, @RequestHeader(HEADER)  String header) throws RestClientException, JsonProcessingException {

    String username = userRepo.claimToken(SECRET,PREFIX,header);
    jobj.put("username",username);
    HttpHeaders headers = new HttpHeaders();
    MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
    headers.setContentType(type);
    System.out.println(jobj.toString());
    HttpEntity<JSONObject> formEntity = new HttpEntity<JSONObject>(jobj, headers);
    JSONObject json = restTemplate.postForEntity("http://localhost:8080/hotel/Booking", formEntity, JSONObject.class).getBody();
    String message = String.valueOf(json.get("Booking Status"));

    if (!message.equalsIgnoreCase("Failed")) {
        System.out.println("json:" + json.toString());
        System.out.println(username);
        TimerTask tt = new PaymentCheck(username);
        timer.schedule(tt, 20000);
        return new ResponseEntity<>(json,HttpStatus.CREATED);
    }else{
        return new ResponseEntity<>(json,HttpStatus.CREATED);
    }
}

public class PaymentCheck extends TimerTask {

   private static final MYQUERY = "update User.check set payment_status = 'cancelled' where payment_status = 'pending' and user = ?";

   private String username;

   public PaymentCheck(String username) {
       this.username = username;
   }

    @Override
    public void run() {
        try (Connection cn = ...) {
            try (Statement stmt = new PreparedStatement(MYQUERY))
            {
                stmt.setString(1, username);
                stmt.execute();
                cn.commit();
            }
        }
    }
}

相关问题