Search in sources :

Example 1 with TakeoverLockException

use of com.sun.messaging.jmq.jmsserver.persist.api.TakeoverLockException in project openmq by eclipse-ee4j.

the class BrokerDAOImpl method takeover.

/**
 * Update the state and other relevant attributes of a broker to signify the store is being taken over by another
 * broker. If the operation is successful then this means that the current broker was able to acquire the lock and it is
 * now responsible for taken over the store of the target broker.
 *
 * @param conn database connection
 * @param id the current or local broker ID
 * @param targetBrokerID the broker ID of the store being taken over
 * @param lastHeartbeat broker's last heartbeat
 * @param expectedState the expected state
 * @param newHeartbeat the new timestamp
 * @param newState the new state
 * @throws TakeoverLockException if the current broker is unable to acquire the takeover lock
 * @return previous broker's info associated with the broker
 */
@Override
public HABrokerInfo takeover(Connection conn, String id, String targetBrokerID, long lastHeartbeat, BrokerState expectedState, long newHeartbeat, BrokerState newState) throws BrokerException {
    HABrokerInfo bkrInfo = null;
    PreparedStatement pstmt = null;
    Exception myex = null;
    try {
        // Save the broker's state before updating
        bkrInfo = getBrokerInfo(conn, targetBrokerID);
        if (bkrInfo == null) {
            String errorMsg = br.getKString(BrokerResources.E_BROKERINFO_NOT_FOUND_IN_STORE, targetBrokerID);
            throw new BrokerException(br.getKString(BrokerResources.E_INTERNAL_BROKER_ERROR, errorMsg));
        }
        DBManager dbMgr = DBManager.getDBManager();
        pstmt = dbMgr.createPreparedStatement(conn, takeoverSQL);
        pstmt.setString(1, id);
        pstmt.setInt(2, newState.intValue());
        pstmt.setLong(3, newHeartbeat);
        pstmt.setString(4, targetBrokerID);
        pstmt.setInt(5, expectedState.intValue());
        pstmt.setLong(6, lastHeartbeat);
        if (pstmt.executeUpdate() != 1) {
            HABrokerInfo binfo = getBrokerInfo(conn, targetBrokerID);
            String errorMsg = br.getKString(BrokerResources.E_UNABLE_TO_ACQUIRE_TAKEOVER_LOCK, targetBrokerID);
            TakeoverLockException ex = new TakeoverLockException(errorMsg);
            // Store broker info
            ex.setBrokerInfo(binfo);
            throw ex;
        }
    } catch (Exception e) {
        myex = e;
        try {
            if ((conn != null) && !conn.getAutoCommit()) {
                conn.rollback();
            }
        } catch (SQLException rbe) {
            logger.log(Logger.ERROR, BrokerResources.X_DB_ROLLBACK_FAILED, rbe);
        }
        Exception ex;
        if (e instanceof BrokerException) {
            throw (BrokerException) e;
        } else if (e instanceof SQLException) {
            ex = DBManager.wrapSQLException("[" + takeoverSQL + "]", (SQLException) e);
        } else {
            ex = e;
        }
        throw new BrokerException(br.getKString(BrokerResources.E_UNABLE_TO_TAKEOVER_BROKER, targetBrokerID), ex);
    } finally {
        Util.close(null, pstmt, null, myex);
    }
    return bkrInfo;
}
Also used : HABrokerInfo(com.sun.messaging.jmq.jmsserver.persist.api.HABrokerInfo) TakeoverLockException(com.sun.messaging.jmq.jmsserver.persist.api.TakeoverLockException) TakeoverLockException(com.sun.messaging.jmq.jmsserver.persist.api.TakeoverLockException)

Aggregations

HABrokerInfo (com.sun.messaging.jmq.jmsserver.persist.api.HABrokerInfo)1 TakeoverLockException (com.sun.messaging.jmq.jmsserver.persist.api.TakeoverLockException)1