获取java.lang.IllegalStateException:未找到线程绑定请求

问题描述 投票:0回答:1

当我尝试使用@Async注释时,我收到以下错误。

java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.

码:

@Async
@Override
@Transactional
public void triggerEmailCommunication(List<String> eventCode, Integer authorizationId, Integer claimId,
        boolean callMethod) {
    Map<String, Object> emailBody = new HashMap<>();
    try {

        if (callMethod) {
            LOGGER.info("Communication async flow triggerEmailCommunication method starts.");
            emailBody = emailBodyObject(authorizationId, claimId, eventCode);
  }


private Map<String, Object> emailBodyObject(Integer authorizationId, Integer claimId, List<String> eventCode)
        throws CareBusinessServiceException {
    LOGGER.info("EmailBodyObject method starts.");
    Map<String, Object> emailBody = new HashMap<String, Object>();
    EmailClaimDetailsVO emailClaimDetails = new EmailClaimDetailsVO();
    ClaimAuthorizationVO claimAuthVO = new ClaimAuthorizationVO();
    Claim claim = new Claim();
    Authorization authorization = new Authorization();
    List<String> rejectReasonList = new ArrayList<>();
    Provider provider = new Provider();
    String providerName = null;
    String claimIntimationNbr = null;
    String authorizationNbr = null;
    SimpleDateFormat formatter = new SimpleDateFormat(AmhiConstants.DATE_FORMAT_DD_MM_YYYY);
    try {
        Integer claimIntimationId = null;
        if (null != claimId) {
            claim = enableBusinessService.retrieveClaimDetails(claimId);
     } catch(Exception e) {
     }
}

DAO layer

@Override
public Claim retrieveClaimIdRecord(Integer claimId) {
    CriteriaBuilder builder = manager.getCriteriaBuilder();
    CriteriaQuery<Claim> criteriaQuery = builder.createQuery(Claim.class);
    Root<Claim> root = criteriaQuery.from(Claim.class);
    ArrayList<Predicate> conditions = new ArrayList<>();
    conditions.add(builder.equal(root.get(Claim_.claimId), claimId));
    criteriaQuery.select(root).where(conditions.toArray(new Predicate[] {}));
    javax.persistence.Query query = manager.createQuery(criteriaQuery);
    List<Claim> claims = query.getResultList();
    if(CollectionUtils.isNotEmpty(claims)){
        return claims.get(0);
    }
    return new Claim();
}

从DB检索该值。但是如上所述,我正在超越异常。

java spring-mvc
1个回答
0
投票

在我的情况下,它发生在我在请求级别创建一个bean并尝试从使用@Async批注创建的另一个线程访问它时。 bean信息仅存储在原始线程的本地线程中。

在@Async通知之后,您是否在函数或后续函数中使用创建并存储在基本线程本地线程中的内容?

@Async创建另一个线程,但不复制导致问题的原始线程的本地线程。

看到这个 - https://jira.spring.io/browse/SPR-6873

© www.soinside.com 2019 - 2024. All rights reserved.