Search in sources :

Example 1 with MessageHandler

use of org.apache.oozie.jms.MessageHandler in project oozie by apache.

the class JMSAccessorService method retryConnection.

@VisibleForTesting
boolean retryConnection(JMSConnectionInfo connInfo) {
    ConnectionRetryInfo connRetryInfo = retryConnectionsMap.get(connInfo);
    if (connRetryInfo.getNumAttempt() >= retryMaxAttempts) {
        LOG.info("Not attempting connection [{0}] again. Reached max attempts [{1}]", connInfo, retryMaxAttempts);
        return false;
    }
    LOG.info("Attempting retry of connection [{0}]", connInfo);
    connRetryInfo.setNumAttempt(connRetryInfo.getNumAttempt() + 1);
    connRetryInfo.setNextDelay(connRetryInfo.getNextDelay() * retryMultiplier);
    ConnectionContext connCtxt = createConnectionContext(connInfo);
    boolean shouldRetry = false;
    if (connCtxt == null) {
        shouldRetry = true;
    } else {
        Map<String, MessageHandler> retryTopicsMap = connRetryInfo.getTopicsToRetry();
        Map<String, MessageReceiver> listeningTopicsMap = getReceiversTopicsMap(connInfo);
        List<String> topicsToRemoveList = new ArrayList<String>();
        // For each topic in the retry list, try to register the MessageHandler for that topic
        for (Entry<String, MessageHandler> topicEntry : retryTopicsMap.entrySet()) {
            String topic = topicEntry.getKey();
            if (listeningTopicsMap.containsKey(topic)) {
                continue;
            }
            synchronized (listeningTopicsMap) {
                if (!listeningTopicsMap.containsKey(topic)) {
                    MessageReceiver receiver = registerForTopic(connInfo, connCtxt, topic, topicEntry.getValue());
                    if (receiver == null) {
                        LOG.warn("Failed to register a listener for topic {0} on {1}", topic, connInfo);
                    } else {
                        listeningTopicsMap.put(topic, receiver);
                        topicsToRemoveList.add(topic);
                        LOG.info("Registered a listener for topic {0} on {1}", topic, connInfo);
                    }
                }
            }
        }
        for (String topic : topicsToRemoveList) {
            retryTopicsMap.remove(topic);
        }
        if (retryTopicsMap.isEmpty()) {
            shouldRetry = false;
        }
    }
    if (shouldRetry) {
        scheduleRetry(connInfo, connRetryInfo.getNextDelay());
    } else {
        retryConnectionsMap.remove(connInfo);
    }
    return true;
}
Also used : MessageHandler(org.apache.oozie.jms.MessageHandler) MessageReceiver(org.apache.oozie.jms.MessageReceiver) ArrayList(java.util.ArrayList) ConnectionContext(org.apache.oozie.jms.ConnectionContext) DefaultConnectionContext(org.apache.oozie.jms.DefaultConnectionContext) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 2 with MessageHandler

use of org.apache.oozie.jms.MessageHandler in project oozie by apache.

the class JMSAccessorService method reestablishConnection.

/**
 * Reestablish connection for the given JMS connect information
 * @param connInfo JMS connection info
 */
public void reestablishConnection(JMSConnectionInfo connInfo) {
    // Queue the connection and topics for retry
    connectionMap.remove(connInfo);
    ConnectionRetryInfo connRetryInfo = queueConnectionForRetry(connInfo);
    Map<String, MessageReceiver> listeningTopicsMap = receiversMap.remove(connInfo);
    if (listeningTopicsMap != null) {
        Map<String, MessageHandler> retryTopicsMap = connRetryInfo.getTopicsToRetry();
        for (Entry<String, MessageReceiver> topicEntry : listeningTopicsMap.entrySet()) {
            MessageReceiver receiver = topicEntry.getValue();
            retryTopicsMap.put(topicEntry.getKey(), receiver.getMessageHandler());
        }
    }
}
Also used : MessageHandler(org.apache.oozie.jms.MessageHandler) MessageReceiver(org.apache.oozie.jms.MessageReceiver)

Aggregations

MessageHandler (org.apache.oozie.jms.MessageHandler)2 MessageReceiver (org.apache.oozie.jms.MessageReceiver)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ArrayList (java.util.ArrayList)1 ConnectionContext (org.apache.oozie.jms.ConnectionContext)1 DefaultConnectionContext (org.apache.oozie.jms.DefaultConnectionContext)1