MongoJobRepository extends AbstractMongoRepository<String, JobInfo> implements JobRepository { public List<String> findAllJobIdsDistinct() { return collection() .aggregate(Arrays.asList( new Document("$sort", new Document("started", -1)), new Document("$group", new HashMap<String, Object>() {{ put("_id", "$type"); put("latestJobId", new Document("$first", "$_id")); }}))) .maxTime(mongoProperties.getDefaultReadTimeout(), TimeUnit.MILLISECONDS) .map(doc -> doc.getString("latestJobId")) .into(new ArrayList<>()).stream() .filter(Objects::nonNull) .collect(toList()); } MongoJobRepository(final MongoDatabase mongoDatabase,
                              final String jobInfoCollectionName,
                              final MongoProperties mongoProperties); @Override JobStatus findStatus(final String jobId); @Override void removeIfStopped(final String id); @Override void appendMessage(final String jobId, final JobMessage jobMessage); @Override void setJobStatus(final String jobId, final JobStatus jobStatus); @Override void setLastUpdate(final String jobId, final OffsetDateTime lastUpdate); @Override List<JobInfo> findLatest(final int maxCount); @Override List<JobInfo> findLatestJobsDistinct(); List<String> findAllJobIdsDistinct(); @Override List<JobInfo> findLatestBy(final String type, final int maxCount); @Override List<JobInfo> findByType(final String type); @Override List<JobInfo> findRunningWithoutUpdateSince(final OffsetDateTime timeOffset); @Override List<JobInfo> findAllJobInfoWithoutMessages(); @Override void keepJobMessagesWithinMaximumSize(String jobId); static final String ID; }
@Test public void shouldFindAllJobTypes() throws Exception { final OffsetDateTime now = OffsetDateTime.now(); final JobInfo eins = someRunningJobInfo("jobEins", "someJobTypeEins", now); final JobInfo zwei = someRunningJobInfo("jobZwei", "someJobTypeZwei", now.plusSeconds(1)); final JobInfo drei = someRunningJobInfo("jobDrei", "someJobTypeDrei", now.plusSeconds(2)); final JobInfo vierWithTypeDrei = someRunningJobInfo("jobVier", "someJobTypeDrei", now.plusSeconds(3)); repo.createOrUpdate(eins); repo.createOrUpdate(zwei); repo.createOrUpdate(drei); repo.createOrUpdate(vierWithTypeDrei); final List<String> allJobIds = repo.findAllJobIdsDistinct(); assertThat(allJobIds, hasSize(3)); assertThat(allJobIds, containsInAnyOrder("jobEins", "jobZwei", "jobVier")); }