预定的JPA查询

时间:2017-11-17 14:08:25

标签: spring asynchronous spring-boot scheduled-tasks

我是Spring-boot上的新手,我需要一个问题的帮助...

我尝试做一个简单的站点地图,生成包含JPA查询到存储库的结果的网址。

这是代码:

预定的服务是:

@Configuration
@EnableScheduling
@EnableAsync
public class SitemapScheduleConfig {
  private final Logger log = LoggerFactory.getLogger(this.getClass());

  @Autowired
  private SitemapService sitemapService;

  @Scheduled(cron = "${sitemap.cron}")
  public void executeSitemap() throws MalformedURLException, ExecutionException {
    if (log.isDebugEnabled()) {
      log.debug("starting execute scheduled sitemap at {}",
          LocalDateTime.now().toString(ISODateTimeFormat.dateHourMinuteSecondMillis()));
    }

    sitemapService.createSitemap();

    if (log.isDebugEnabled()) {
      log.debug("ending scheduled sitemap at {}",
          LocalDateTime.now().toString(ISODateTimeFormat.dateHourMinuteSecondMillis()));
    }
  }
}

此服务调用另一项名为SitemapService的服务:

@Service
public final class SitemapService {

  private final SitemapProvider sitemapProvider;
  private final Logger log = LoggerFactory.getLogger(this.getClass());

  private List<PressNotes> pressList = Collections.emptyList();


  public SitemapService(final SitemapProvider sitemapProvider){
    this.sitemapProvider = sitemapProvider;
  }

  public void createSitemap() throws MalformedURLException {log.debug("Starting Sitemap at {}",
      LocalDateTime.now().toString(ISODateTimeFormat.dateHourMinuteSecondMillis()));
    try
    {
        Future<List<PressNotes>> pressNotes = sitemap.fillPressNotes();
        try {
          pressList = pressNotes.get();
        } catch (InterruptedException e) {
          e.printStackTrace();
        } catch (ExecutionException e) {
          e.printStackTrace();
        }

        System.out.println(acordsList.size());
    }
    catch(Exception e)
    {
      log.debug("Error en la generació del sitemap: " +e.getMessage());
    }
    log.debug("Finish Sitemap at {}",
        LocalDateTime.now().toString(ISODateTimeFormat.dateHourMinuteSecondMillis()));
  }
}

执行Sitemap功能的SitemapProvider:

@Service
public class SitemapProvider {

  public static final String POSICIO = "posicio";
  private final PressNotesService pressNotesService;
  private final UrlConfigurationProperties urls;
  private final IdiomaService idiomaService;
  private Slugify slugify;
  final PageRequest pageableSize3OrderPosicioAsc = new PageRequest(0, Integer.MAX_VALUE, Direction.ASC, POSICIO);
  private final Logger log = LoggerFactory.getLogger(this.getClass());
  private static final int CHUNK_SIZE = 100;

  private List<PressNotes> pressList = Collections.emptyList();


  public SitemapProvider(final IdiomaService idiomaService, final PressNotesService pressNotesService, final UrlConfigurationProperties urlConfigurationProperties)
  {
    this.idiomaService = idiomaService;
    this.urls = urlConfigurationProperties;
    this.pressNotesService = pressNotesService;
    slugify = new Slugify();
  }



  @Async
  public Future<List<PressNotes>> fillAcordsGovern() throws MalformedURLException {

    Pageable pageRequest = new PageRequest(0, CHUNK_SIZE, Direction.DESC, Constants.DATA_PUBLICACIO_PORTAL);
    //sitemap.addUrl(getUrl(Constants.PATH_SEGMENT_ACORD_GOVERN));
    log.debug("getting AcordsGovern");
    List<PressNotes> pressNotes = new ArrayList<>();
    boolean isLast = false;
    while (!isLast) {
      Page<PressNotes> page = pressNotesRepository.findAll(
          Specifications.where(PublicationsSpecifications.<PressNotes>isPublicated()).and(
              PublicationsSpecifications.<PressNotes>byLanguage(
                  idiomaService.getIdiomaFromLocale())), pageRequest);
      for (PressNotes pressNote : page) {
        pressNotes.add(pressNote);
      }
      isLast = page.isLast();
      if (page.hasNext()) {
        pageRequest = page.nextPageable();
      }
    }
    return new AsyncResult<>(acordsGovern);
  }

问题是,当我使用Web控制器调用此方法没有问题时,存储库会给我值。

但是当它被安排时它不会返回数据。

我做错了什么?我要另外注释?

如何将存储库配置为与预定方式异步工作?

由于

0 个答案:

没有答案