Here are the examples of the java api class org.apache.activemq.artemis.core.persistence.impl.journal.OperationContextImpl taken from open source projects.
1. OperationContextUnitTest#testCaptureExceptionOnFailure()
Project: activemq-artemis
File: OperationContextUnitTest.java
File: OperationContextUnitTest.java
@Test public void testCaptureExceptionOnFailure() throws Exception { ExecutorService executor = Executors.newSingleThreadExecutor(ActiveMQThreadFactory.defaultThreadFactory()); final CountDownLatch latch = new CountDownLatch(1); final OperationContextImpl context = new OperationContextImpl(executor) { @Override public void complete() { super.complete(); latch.countDown(); } }; context.storeLineUp(); final AtomicInteger failures = new AtomicInteger(0); Thread t = new Thread() { @Override public void run() { try { context.waitCompletion(5000); } catch (Throwable e) { e.printStackTrace(); failures.incrementAndGet(); } } }; t.start(); // Need to wait complete to be called first or the test would be invalid. // We use a latch instead of forcing a sleep here Assert.assertTrue(latch.await(5, TimeUnit.SECONDS)); context.onError(ActiveMQExceptionType.UNSUPPORTED_PACKET.getCode(), "Poop happens!"); t.join(); Assert.assertEquals(1, failures.get()); failures.set(0); final AtomicInteger operations = new AtomicInteger(0); // We should be up to date with lineUps and executions. this should now just finish processing context.executeOnCompletion(new IOCallback() { @Override public void done() { operations.incrementAndGet(); } @Override public void onError(final int errorCode, final String errorMessage) { failures.incrementAndGet(); } }); Assert.assertEquals(1, failures.get()); Assert.assertEquals(0, operations.get()); }
2. OperationContextUnitTest#testCaptureExceptionOnExecutor()
Project: activemq-artemis
File: OperationContextUnitTest.java
File: OperationContextUnitTest.java
@Test public void testCaptureExceptionOnExecutor() throws Exception { ExecutorService executor = Executors.newSingleThreadExecutor(ActiveMQThreadFactory.defaultThreadFactory()); executor.shutdown(); final CountDownLatch latch = new CountDownLatch(1); final OperationContextImpl impl = new OperationContextImpl(executor) { @Override public void complete() { super.complete(); latch.countDown(); } }; impl.storeLineUp(); final AtomicInteger numberOfFailures = new AtomicInteger(0); Thread t = new Thread() { @Override public void run() { try { impl.waitCompletion(5000); } catch (Throwable e) { e.printStackTrace(); numberOfFailures.incrementAndGet(); } } }; t.start(); // Need to wait complete to be called first or the test would be invalid. // We use a latch instead of forcing a sleep here Assert.assertTrue(latch.await(5, TimeUnit.SECONDS)); impl.done(); t.join(); Assert.assertEquals(1, numberOfFailures.get()); }