Search in sources :

Example 1 with MCRProcessableCollection

use of org.mycore.common.processing.MCRProcessableCollection in project mycore by MyCoRe-Org.

the class MCRProcessableWebsocketSenderImpl method remove.

public synchronized Integer remove(Object object) {
    Integer id = ID_MAP.get(object);
    if (id == null) {
        return null;
    }
    ID_MAP.remove(id);
    if (object instanceof MCRProcessable) {
        PROCESSABLE_COLLECTION_MAP.remove(id);
    } else if (object instanceof MCRProcessableCollection) {
        PROCESSABLE_COLLECTION_MAP.values().removeIf(id::equals);
    }
    return id;
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MCRProcessable(org.mycore.common.processing.MCRProcessable) MCRProcessableCollection(org.mycore.common.processing.MCRProcessableCollection)

Example 2 with MCRProcessableCollection

use of org.mycore.common.processing.MCRProcessableCollection in project mycore by MyCoRe-Org.

the class MCRProcessableFactoryTest method newPool.

@Test
public void newPool() throws Exception {
    MCRProcessableRegistry registry = new MCRCentralProcessableRegistry();
    MCRProcessableCollection collection = new MCRProcessableDefaultCollection("test");
    registry.register(collection);
    int nThreads = 3;
    ExecutorService es = Executors.newFixedThreadPool(nThreads);
    MCRProcessableExecutor pes = MCRProcessableFactory.newPool(es, collection);
    assertEquals("No runnables should be queued right now.", 0, collection.stream().count());
    assertEquals("Only the 'test' collection should be registered.", 1, registry.stream().count());
    Semaphore semaphore = new Semaphore(nThreads);
    // lock threads until ready
    semaphore.acquire(nThreads);
    MCRProcessableSupplier<?> sup1 = pes.submit(sleepyThread(semaphore));
    MCRProcessableSupplier<?> sup2 = pes.submit(sleepyThread(semaphore));
    MCRProcessableSupplier<?> sup3 = pes.submit(sleepyThread(semaphore));
    MCRProcessableStatus s1 = sup1.getStatus();
    MCRProcessableStatus s2 = sup2.getStatus();
    MCRProcessableStatus s3 = sup3.getStatus();
    String msgPrefix = "Job should be created or in processing: ";
    assertTrue(msgPrefix + s1, MCRProcessableStatus.processing == s1 || MCRProcessableStatus.created == s1);
    assertTrue(msgPrefix + s2, MCRProcessableStatus.processing == s2 || MCRProcessableStatus.created == s2);
    assertTrue(msgPrefix + s3, MCRProcessableStatus.processing == s3 || MCRProcessableStatus.created == s3);
    assertEquals(3, collection.stream().count());
    // go
    semaphore.release(nThreads);
    CompletableFuture.allOf(sup1.getFuture(), sup2.getFuture(), sup3.getFuture()).get();
    assertEquals(MCRProcessableStatus.successful, sup1.getStatus());
    assertEquals(MCRProcessableStatus.successful, sup2.getStatus());
    assertEquals(MCRProcessableStatus.successful, sup3.getStatus());
    es.shutdown();
    es.awaitTermination(10, TimeUnit.SECONDS);
}
Also used : MCRProcessableRegistry(org.mycore.common.processing.MCRProcessableRegistry) MCRProcessableStatus(org.mycore.common.processing.MCRProcessableStatus) MCRProcessableCollection(org.mycore.common.processing.MCRProcessableCollection) ExecutorService(java.util.concurrent.ExecutorService) Semaphore(java.util.concurrent.Semaphore) MCRProcessableDefaultCollection(org.mycore.common.processing.MCRProcessableDefaultCollection) MCRCentralProcessableRegistry(org.mycore.common.processing.impl.MCRCentralProcessableRegistry) Test(org.junit.Test)

Aggregations

MCRProcessableCollection (org.mycore.common.processing.MCRProcessableCollection)2 ExecutorService (java.util.concurrent.ExecutorService)1 Semaphore (java.util.concurrent.Semaphore)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Test (org.junit.Test)1 MCRProcessable (org.mycore.common.processing.MCRProcessable)1 MCRProcessableDefaultCollection (org.mycore.common.processing.MCRProcessableDefaultCollection)1 MCRProcessableRegistry (org.mycore.common.processing.MCRProcessableRegistry)1 MCRProcessableStatus (org.mycore.common.processing.MCRProcessableStatus)1 MCRCentralProcessableRegistry (org.mycore.common.processing.impl.MCRCentralProcessableRegistry)1