use of org.apereo.portal.concurrency.CallableWithoutResult in project uPortal by Jasig.
the class JdbcAuthDaoTest method testConcurrentCreate.
@Test
public void testConcurrentCreate() throws UnknownHostException {
final String service = "foo";
// Create & Return
final String token = this.execute(new Callable<String>() {
@Override
public String call() throws Exception {
final String t = authDao.getAuthToken(service);
assertEquals(1000, t.length());
return t;
}
});
this.execute(new CallableWithoutResult() {
@Override
protected void callWithoutResult() {
authDao.createToken(service);
}
});
this.execute(new CallableWithoutResult() {
@Override
protected void callWithoutResult() {
final String t = authDao.getAuthToken(service);
assertEquals(token, t);
}
});
}
use of org.apereo.portal.concurrency.CallableWithoutResult in project uPortal by Jasig.
the class JaxbPortalDataHandlerService method exportAllDataOfType.
@Override
public void exportAllDataOfType(Set<String> typeIds, File directory, BatchExportOptions options) {
final Queue<ExportFuture<?>> exportFutures = new ConcurrentLinkedQueue<>();
final boolean failOnError = options != null ? options.isFailOnError() : true;
// Determine the parent directory to log to
final File logDirectory = determineLogDirectory(options, "export");
// Setup reporting file
final File exportReport = new File(logDirectory, "data-export.txt");
final PrintWriter reportWriter;
try {
reportWriter = new PrintWriter(new BufferedWriter(new FileWriter(exportReport)));
} catch (IOException e) {
throw new RuntimeException("Failed to create FileWriter for: " + exportReport, e);
}
try {
for (final String typeId : typeIds) {
final List<FutureHolder<?>> failedFutures = new LinkedList<>();
final File typeDir = new File(directory, typeId);
logger.info("Adding all data of type {} to export queue: {}", typeId, typeDir);
reportWriter.println(typeId + "," + typeDir);
final Iterable<? extends IPortalData> dataForType = this.getPortalData(typeId);
for (final IPortalData data : dataForType) {
final String dataId = data.getDataId();
// Check for completed futures on every iteration, needed to fail as fast as
// possible on an import exception
final List<FutureHolder<?>> newFailed = waitForFutures(exportFutures, reportWriter, logDirectory, false);
failedFutures.addAll(newFailed);
final AtomicLong exportTime = new AtomicLong(-1);
// Create export task
Callable<Object> task = new CallableWithoutResult() {
@Override
protected void callWithoutResult() {
exportTime.set(System.nanoTime());
try {
exportData(typeId, dataId, typeDir);
} finally {
exportTime.set(System.nanoTime() - exportTime.get());
}
}
};
// Submit the export task
final Future<?> exportFuture = this.importExportThreadPool.submit(task);
// Add the future for tracking
final ExportFuture futureHolder = new ExportFuture(exportFuture, typeId, dataId, exportTime);
exportFutures.offer(futureHolder);
}
final List<FutureHolder<?>> newFailed = waitForFutures(exportFutures, reportWriter, logDirectory, true);
failedFutures.addAll(newFailed);
reportWriter.flush();
if (failOnError && !failedFutures.isEmpty()) {
throw new RuntimeException(failedFutures.size() + " " + typeId + " entities failed to export.\n" + "\tPer-entity exception logs and a full report can be found in " + // location clickable in some shells
logDirectory.toURI());
}
}
} catch (InterruptedException e) {
throw new RuntimeException("Interrupted while waiting for entities to export", e);
} finally {
IOUtils.closeQuietly(reportWriter);
}
}
use of org.apereo.portal.concurrency.CallableWithoutResult in project uPortal by Jasig.
the class JpaPortalEventStoreTest method testStoreSingleEvents.
// Breaks on move to Gradlde
@Ignore
@Test
public void testStoreSingleEvents() throws Exception {
final DateTime startDate = DateTime.now().minusDays(1);
final DateTime endDate = DateTime.now().plusDays(1);
final List<PortalEvent> originalEvents = generateEvents();
execute(new CallableWithoutResult() {
@Override
protected void callWithoutResult() {
for (final PortalEvent event : originalEvents) {
portalEventDao.storePortalEvent(event);
}
}
});
verifyGetEvents(originalEvents, startDate, endDate);
verifyAggregateEvents(originalEvents, startDate, endDate);
verifyAggregateEvents(Collections.<PortalEvent>emptyList(), startDate, endDate);
deleteEvents(originalEvents, startDate, endDate);
verifyGetEvents(Collections.<PortalEvent>emptyList(), startDate, endDate);
verifyAggregateEvents(Collections.<PortalEvent>emptyList(), startDate, endDate);
}
use of org.apereo.portal.concurrency.CallableWithoutResult in project uPortal by Jasig.
the class JpaPortalEventStoreTest method verifyAggregateEvents.
protected void verifyAggregateEvents(final List<PortalEvent> originalEvents, final DateTime startDate, final DateTime endDate) {
execute(new CallableWithoutResult() {
@Override
protected void callWithoutResult() {
// Get all events
final List<PortalEvent> portalEvents = new LinkedList<PortalEvent>();
final AtomicReference<DateTime> nextStart = new AtomicReference<DateTime>(startDate);
// aggregate all events, 5 at a time.
final int loadSize = 10;
int startSize;
do {
startSize = portalEvents.size();
portalEventDao.aggregatePortalEvents(nextStart.get(), endDate, loadSize, new Function<PortalEvent, Boolean>() {
@Override
public Boolean apply(PortalEvent input) {
portalEvents.add(input);
nextStart.set(input.getTimestampAsDate());
return Boolean.TRUE;
}
});
} while (loadSize + startSize == portalEvents.size());
assertEquals(originalEvents.size(), portalEvents.size());
final Iterator<PortalEvent> originalEventItr = originalEvents.iterator();
final Iterator<PortalEvent> eventItr = portalEvents.iterator();
while (originalEventItr.hasNext()) {
assertEquals(originalEventItr.next().getClass(), eventItr.next().getClass());
}
}
});
}
use of org.apereo.portal.concurrency.CallableWithoutResult in project uPortal by Jasig.
the class JpaPortalEventStoreTest method testStoreBatchEvents.
// Breaks on move to Gradlde
@Ignore
@Test
public void testStoreBatchEvents() throws Exception {
final DateTime startDate = DateTime.now().minusDays(1);
final DateTime endDate = DateTime.now().plusDays(1);
final List<PortalEvent> originalEvents = generateEvents();
execute(new CallableWithoutResult() {
@Override
protected void callWithoutResult() {
final DateTime oldestPortalEventTimestamp = portalEventDao.getOldestPortalEventTimestamp();
assertNull(oldestPortalEventTimestamp);
final DateTime newestPortalEventTimestamp = portalEventDao.getNewestPortalEventTimestamp();
assertNull(newestPortalEventTimestamp);
}
});
execute(new CallableWithoutResult() {
@Override
protected void callWithoutResult() {
portalEventDao.storePortalEvents(originalEvents);
}
});
Collections.sort(originalEvents, new Comparator<PortalEvent>() {
@Override
public int compare(PortalEvent o1, PortalEvent o2) {
return o1.getTimestampAsDate().compareTo(o2.getTimestampAsDate());
}
});
execute(new CallableWithoutResult() {
@Override
protected void callWithoutResult() {
final DateTime oldestPortalEventTimestamp = portalEventDao.getOldestPortalEventTimestamp();
final DateTime newestPortalEventTimestamp = portalEventDao.getNewestPortalEventTimestamp();
assertNotNull(oldestPortalEventTimestamp);
assertNotNull(newestPortalEventTimestamp);
assertEquals(originalEvents.get(0).getTimestampAsDate().getMillis(), oldestPortalEventTimestamp.getMillis());
assertEquals(originalEvents.get(originalEvents.size() - 1).getTimestampAsDate().getMillis(), newestPortalEventTimestamp.getMillis());
}
});
verifyGetEvents(originalEvents, startDate, endDate);
verifyAggregateEvents(originalEvents, startDate, endDate);
verifyAggregateEvents(Collections.<PortalEvent>emptyList(), startDate, endDate);
deleteEvents(originalEvents, startDate, endDate);
verifyGetEvents(Collections.<PortalEvent>emptyList(), startDate, endDate);
verifyAggregateEvents(Collections.<PortalEvent>emptyList(), startDate, endDate);
}
Aggregations