@Timeout public void timeout(Timer timer) { final Cache<String, ExecutionControl> cache = GlobalCache.get().getExecutionControls(); String info = (String) timer.getInfo(); try { int n = info.indexOf(' '); String execCtrlName = info.substring(0, n); int cycleNum = Integer.parseInt(info.substring(n + 1)); ExecutionControl execCtrl = cache.get(execCtrlName); if (execCtrl != null) execCtrl.onTimeout(cycleNum); else logger.log(Level.WARNING, "ExecutionControl " + execCtrlName + " not found."); } catch (ConcurrentAccessTimeoutException ex) { logger.warning(ex.getMessage()); } }
public void testConcurrentMethodCall() throws Exception { MyLocalBeanImpl.semaphore = new Semaphore(0); final InitialContext ctx = new InitialContext(); final MyLocalBean bean = (MyLocalBean) ctx.lookup("MyLocalBeanImplLocal"); final MyLocalBean bean2 = (MyLocalBean) ctx.lookup("MyLocalBeanImplLocal"); final CallRentrantThread call = new CallRentrantThread(bean, 3000); (new Thread(call)).start(); // ensure the call on thread came in assertTrue(MyLocalBeanImpl.semaphore.tryAcquire(1, 30, TimeUnit.SECONDS)); try { java.util.logging.Logger.getLogger(this.getClass().getName()).info("Expecting a SEVERE javax.ejb.ConcurrentAccessTimeoutException"); bean2.callRentrant(bean, 0); fail("Expected exception"); } catch (final Exception e) { if (e.getCause() instanceof ConcurrentAccessTimeoutException) { // that's what we want } else { throw e; } } }
private Lock aquireLock(final boolean read, final Duration accessTimeout, final Instance instance, final Method runMethod) { final Lock lock; if (read) { lock = instance.lock.readLock(); } else { lock = instance.lock.writeLock(); } final boolean lockAcquired; if (accessTimeout == null || accessTimeout.getTime() < 0) { // wait indefinitely for a lock //noinspection LockAcquiredButNotSafelyReleased lock.lock(); lockAcquired = true; } else if (accessTimeout.getTime() == 0) { // concurrent calls are not allowed, lock only once lockAcquired = lock.tryLock(); } else { // try to get a lock within the specified period. try { lockAcquired = lock.tryLock(accessTimeout.getTime(), accessTimeout.getUnit()); } catch (final InterruptedException e) { throw (ConcurrentAccessTimeoutException) new ConcurrentAccessTimeoutException("Unable to get " + (read ? "read" : "write") + " lock within specified time on '" + runMethod.getName() + "' method for: " + instance.bean.getClass().getName()).initCause(e); } } // Did we acquire the lock to the current execution? if (!lockAcquired) { throw new ConcurrentAccessTimeoutException("Unable to get " + (read ? "read" : "write") + " lock on '" + runMethod.getName() + "' method for: " + instance.bean.getClass().getName()); } return lock; }
public void testStatelessBeanTimeout() throws Exception { final InitialContext ctx = new InitialContext(); final Object object = ctx.lookup("CounterBeanLocal"); assertTrue("instanceof counter", object instanceof Counter); final CountDownLatch timeouts = new CountDownLatch(10); final CountDownLatch startPistol = new CountDownLatch(1); final CountDownLatch startingLine = new CountDownLatch(10); final Counter counter = (Counter) object; // Do a business method... final Runnable r = new Runnable() { public void run() { try { counter.race(startingLine, startPistol); } catch (final ConcurrentAccessTimeoutException ex) { comment("Leap Start"); timeouts.countDown(); } } }; comment("On your mark!"); for (int i = 0; i < 20; i++) { final Thread t = new Thread(r); t.start(); } // Wait for the beans to reach the start line assertTrue("expected 10 invocations", startingLine.await(3000, TimeUnit.MILLISECONDS)); comment("Get Set!"); // Wait for the other beans timeout assertTrue("expected 10 timeouts", timeouts.await(3000, TimeUnit.MILLISECONDS)); assertEquals(10, instances.get(), 1.1); comment("Go!"); startPistol.countDown(); // go }