Search in sources :

Example 16 with TimeoutGuard

use of nl.nn.adapterframework.task.TimeoutGuard in project iaf by ibissource.

the class Receiver method closeAllResources.

/**
 * Should only close resources when in state stopping (or error)! this should be the only trigger to change the state to stopped
 * On exit resources must be 'closed' so the receiver RunState can be set to 'STOPPED'
 */
protected void closeAllResources() {
    TimeoutGuard timeoutGuard = new TimeoutGuard(getStopTimeout(), "stopping receiver [" + getName() + "]");
    log.debug(getLogPrefix() + "closing");
    try {
        try {
            getListener().close();
        } catch (Exception e) {
            error("error closing listener", e);
        }
        if (getSender() != null) {
            try {
                getSender().close();
            } catch (Exception e) {
                error("error closing sender", e);
            }
        }
        if (getErrorSender() != null) {
            try {
                getErrorSender().close();
            } catch (Exception e) {
                error("error closing error sender", e);
            }
        }
        if (getErrorStorage() != null) {
            try {
                getErrorStorage().close();
            } catch (Exception e) {
                error("error closing error storage", e);
            }
        }
        if (getMessageLog() != null) {
            try {
                getMessageLog().close();
            } catch (Exception e) {
                error("error closing message log", e);
            }
        }
    } finally {
        if (timeoutGuard.cancel()) {
            if (!isInRunState(RunState.EXCEPTION_STARTING)) {
                // Don't change the RunState when failed to start
                runState.setRunState(RunState.EXCEPTION_STOPPING);
            }
            log.warn(getLogPrefix() + "timeout stopping");
        } else {
            log.debug(getLogPrefix() + "closed");
            if (isInRunState(RunState.STOPPING) || isInRunState(RunState.EXCEPTION_STOPPING)) {
                runState.setRunState(RunState.STOPPED);
            }
            if (!isInRunState(RunState.EXCEPTION_STARTING)) {
                // Don't change the RunState when failed to start
                throwEvent(RCV_SHUTDOWN_MONITOR_EVENT);
                resetRetryInterval();
                info("stopped");
            }
        }
    }
}
Also used : TimeoutGuard(nl.nn.adapterframework.task.TimeoutGuard) TimeoutException(nl.nn.adapterframework.core.TimeoutException) SenderException(nl.nn.adapterframework.core.SenderException) ListenerException(nl.nn.adapterframework.core.ListenerException) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException)

Example 17 with TimeoutGuard

use of nl.nn.adapterframework.task.TimeoutGuard in project iaf by ibissource.

the class LockerTest method testNoInsertAfterInsert.

/*
	 * Test the mechanism of the locker.
	 */
@Test
public void testNoInsertAfterInsert() throws Exception {
    cleanupLocks();
    locker.setTxManager(txManager);
    locker.setObjectId("myLocker");
    locker.configure();
    TimeoutGuard testTimeout = new TimeoutGuard(10, "Testtimeout");
    try {
        Semaphore waitBeforeInsert = new Semaphore();
        Semaphore insertDone = new Semaphore();
        Semaphore waitBeforeCommit = new Semaphore();
        LockerTester other = new LockerTester(txManager);
        other.setWaitBeforeAction(waitBeforeInsert);
        other.setActionDone(insertDone);
        other.setWaitAfterAction(waitBeforeCommit);
        other.start();
        IbisTransaction mainItx = null;
        if (txManager != null) {
            TransactionDefinition txdef = SpringTxManagerProxy.getTransactionDefinition(TransactionDefinition.PROPAGATION_REQUIRES_NEW, 20);
            mainItx = new IbisTransaction(txManager, txdef, "locker ");
        }
        try (Connection conn = getConnection()) {
            // now this thread has started its transaction, let the other thread do its insert
            waitBeforeInsert.release();
            // and wait that to be finished
            insertDone.acquire();
            try (PreparedStatement stmt = conn.prepareStatement("INSERT INTO IBISLOCK (OBJECTID) VALUES('myLocker')")) {
                try {
                    Timer timer = new Timer("let other thread commit after one second");
                    timer.schedule(new TimerTask() {

                        @Override
                        public void run() {
                            waitBeforeCommit.release();
                        }
                    }, 1000L);
                    stmt.executeUpdate();
                    log.debug("lock inserted");
                    fail("should not be possible to do a second insert");
                } catch (SQLException e) {
                    if (locker.getDbmsSupport().isConstraintViolation(e) || e.getMessage().toLowerCase().contains("timeout")) {
                        log.debug("Caught expected UniqueConstraintViolation or Timeout (" + e.getClass().getName() + "): " + e.getMessage());
                    } else {
                        fail("Expected UniqueConstraintViolation, but was: (" + e.getClass().getName() + "): " + e.getMessage());
                    }
                }
            }
            waitBeforeCommit.release();
        } catch (Exception e) {
            log.warn("exception for second insert: " + e.getMessage(), e);
        } finally {
            if (mainItx != null) {
                mainItx.commit();
            }
        }
    } finally {
        if (testTimeout.cancel()) {
            fail("test timed out");
        }
    }
}
Also used : TransactionDefinition(org.springframework.transaction.TransactionDefinition) Timer(java.util.Timer) TimerTask(java.util.TimerTask) SQLException(java.sql.SQLException) IbisTransaction(nl.nn.adapterframework.core.IbisTransaction) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) TimeoutGuard(nl.nn.adapterframework.task.TimeoutGuard) SQLException(java.sql.SQLException) Test(org.junit.Test)

Example 18 with TimeoutGuard

use of nl.nn.adapterframework.task.TimeoutGuard in project iaf by ibissource.

the class LockerTest method testLockWaitTimeout.

@Test
public void testLockWaitTimeout() throws Exception {
    cleanupLocks();
    locker.setTxManager(txManager);
    locker.setObjectId("myLocker");
    locker.setLockWaitTimeout(1);
    locker.configure();
    boolean lockerUnderTestReturned = false;
    log.debug("Creating Timeout Guard");
    TimeoutGuard testTimeout = new TimeoutGuard(20, "Testtimeout");
    try {
        Semaphore otherInsertReady = new Semaphore();
        Semaphore otherContinue = new Semaphore();
        Semaphore otherFinished = new Semaphore();
        log.debug("Preparing LockerTester");
        LockerTester lockerTester = new LockerTester(txManager);
        lockerTester.setActionDone(otherInsertReady);
        lockerTester.setWaitAfterAction(otherContinue);
        lockerTester.setFinalizeActionDone(otherFinished);
        log.debug("Inserting lock into table in other thread");
        lockerTester.start();
        log.debug("Waiting for other thread to return from insert");
        otherInsertReady.acquire();
        log.debug("other thread returned from insert, acquiring lock");
        String objectId = locker.acquire();
        assertNull(objectId);
        log.debug("Locker returned, releasing process in other thread to finish");
        lockerUnderTestReturned = true;
        otherContinue.release();
        log.debug("Other threads process released, waiting to finish");
        try {
            otherFinished.acquire();
        } catch (Throwable t) {
            // we do not consider this a failure condition:
            // This test is not about the other thread to complete without problems,
            // only about this thread to wait at most <timeout> seconds for the lock.
            boolean interrupted = Thread.interrupted();
            log.warn("Ignoring exception waiting for other thread to complete, interrupted [" + interrupted + "]", t);
        }
        // N.B. commented out test for other thread:
        // This test is not about the other thread to complete without problems,
        // only about this thread to wait at most <timeout> seconds for the lock.
        // log.debug("Other threads process finished, testing conditions");
        // assertNull(lockerTester.getCaught());
        log.debug("testLockWaitTimeout() passed");
    } catch (Throwable t) {
        log.error("testLockWaitTimeout() threw exception", t);
        throw t;
    } finally {
        log.debug("cancel timeout guard");
        if (testTimeout.cancel()) {
            log.debug("test timed out");
            if (!lockerUnderTestReturned) {
                fail("test timed out");
            }
        }
        log.debug("test did not time out");
    }
}
Also used : TimeoutGuard(nl.nn.adapterframework.task.TimeoutGuard) Test(org.junit.Test)

Aggregations

TimeoutGuard (nl.nn.adapterframework.task.TimeoutGuard)18 IbisTransaction (nl.nn.adapterframework.core.IbisTransaction)8 ConfigurationException (nl.nn.adapterframework.configuration.ConfigurationException)6 PipeRunException (nl.nn.adapterframework.core.PipeRunException)5 SenderException (nl.nn.adapterframework.core.SenderException)5 TimeoutException (nl.nn.adapterframework.core.TimeoutException)5 PipeLineResult (nl.nn.adapterframework.core.PipeLineResult)4 Test (org.junit.Test)4 IOException (java.io.IOException)3 Connection (java.sql.Connection)3 PreparedStatement (java.sql.PreparedStatement)3 SQLException (java.sql.SQLException)3 Date (java.util.Date)3 ListenerException (nl.nn.adapterframework.core.ListenerException)3 PipeRunResult (nl.nn.adapterframework.core.PipeRunResult)3 Message (nl.nn.adapterframework.stream.Message)3 TransactionStatus (org.springframework.transaction.TransactionStatus)3 HashMap (java.util.HashMap)2 Timer (java.util.Timer)2 TimerTask (java.util.TimerTask)2