taskscheduler和spring监听器

pbossiut  于 2021-07-05  发布在  Java
关注(0)|答案(1)|浏览(316)

我尝试使用springtaskscheduler更改实体,但由于springauditor找不到currentauditor,所以出现了rollbackexception。我甚至不知道我在哪里能读到如何解决我的问题。如果有人能帮我解答这个问题,我将非常感激?
原因:com.geleigeit.linenandflowers.config.auditor.auditorawareimpl.getcurrentauditor(auditorawareimpl)上的java.lang.nullpointerexception。java:12)~[classes/:?]位于org.springframework.data.auditing.auditinghandler.lambda$touchauditor$6(auditinghandler。java:193)~[spring-data-commons-2.3.1.发布。jar:2.3.1.release]在java.util.optional.map(可选。java:258) ~[?:?]

@Override
@Transactional
@Scheduled(fixedDelay=10000)
public void toInProgress() {
    String currentStatus = "QUEUE";
    String statusToSet = "IN_PROGRESS";
    OrderStatus currentOrderStatus = orderStatusRepository.findByStatus(currentStatus);
    OrderStatus orderStatusToSet = orderStatusRepository.findByStatus(statusToSet);
    try {
        List<Order> orders = repository.findAllByOrderStatus(currentOrderStatus);
        for(Order order : orders) {
            int diff = (int) ((new Date().getTime() - order.getCreatedDate().getTime()) / (24 * 60 * 60 * 100));
            if(diff >= 1) {
                order.setOrderStatus(orderStatusToSet);
                repository.save(order);
                logger.info("order status QUEUE was changed to IN_PROGRESS by order.{}", order.hashCode());
            }
        }
    } catch (NullPointerException e) {
        logger.info("no orders with status QUEUE");
    }
}
wfveoks0

wfveoks01#

我解决了我的问题。我只是在这个方法中验证一个用户(调度器)。

Authentication auth = new UsernamePasswordAuthenticationToken(SecurityUser.fromUser(
            userService.getUser(scheduleId)), Role.ADMIN);
    SecurityContextHolder.getContext().setAuthentication(auth);

相关问题