Search in sources :

Example 1 with MessageReceiver

use of org.apache.oozie.jms.MessageReceiver 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 MessageReceiver

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

the class JMSAccessorService method registerForNotification.

/**
 * Register for notifications on a JMS topic.
 *
 * @param connInfo Information to connect to a JMS compliant messaging service.
 * @param topic Topic in which the JMS messages are published
 * @param msgHandler Handler which will process the messages received on the topic
 */
public void registerForNotification(JMSConnectionInfo connInfo, String topic, MessageHandler msgHandler) {
    if (!isTopicInRetryList(connInfo, topic)) {
        if (isConnectionInRetryList(connInfo)) {
            queueTopicForRetry(connInfo, topic, msgHandler);
        } else {
            Map<String, MessageReceiver> topicsMap = getReceiversTopicsMap(connInfo);
            if (!topicsMap.containsKey(topic)) {
                synchronized (topicsMap) {
                    if (!topicsMap.containsKey(topic)) {
                        ConnectionContext connCtxt = createConnectionContext(connInfo);
                        if (connCtxt == null) {
                            queueTopicForRetry(connInfo, topic, msgHandler);
                            return;
                        }
                        MessageReceiver receiver = registerForTopic(connInfo, connCtxt, topic, msgHandler);
                        if (receiver == null) {
                            queueTopicForRetry(connInfo, topic, msgHandler);
                        } else {
                            LOG.info("Registered a listener for topic {0} on {1}", topic, connInfo);
                            topicsMap.put(topic, receiver);
                        }
                    }
                }
            }
        }
    }
}
Also used : MessageReceiver(org.apache.oozie.jms.MessageReceiver) ConnectionContext(org.apache.oozie.jms.ConnectionContext) DefaultConnectionContext(org.apache.oozie.jms.DefaultConnectionContext)

Example 3 with MessageReceiver

use of org.apache.oozie.jms.MessageReceiver 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)

Example 4 with MessageReceiver

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

the class JMSAccessorService method registerForTopic.

private MessageReceiver registerForTopic(JMSConnectionInfo connInfo, ConnectionContext connCtxt, String topic, MessageHandler msgHandler) {
    try {
        Session session = connCtxt.createSession(sessionOpts);
        MessageConsumer consumer = connCtxt.createConsumer(session, topic);
        MessageReceiver receiver = new MessageReceiver(msgHandler, session, consumer);
        consumer.setMessageListener(receiver);
        return receiver;
    } catch (JMSException e) {
        LOG.warn("Error while registering to listen to topic {0} from {1}", topic, connInfo, e);
        return null;
    }
}
Also used : MessageConsumer(javax.jms.MessageConsumer) MessageReceiver(org.apache.oozie.jms.MessageReceiver) JMSException(javax.jms.JMSException) Session(javax.jms.Session)

Example 5 with MessageReceiver

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

the class TestJMSAccessorService method testUnRegisterTopic.

@Test
public void testUnRegisterTopic() {
    try {
        HCatAccessorService hcatService = services.get(HCatAccessorService.class);
        JMSAccessorService jmsService = services.get(JMSAccessorService.class);
        String server = "hcat.server.com:5080";
        String topic = "hcatalog.mydb.mytable";
        JMSConnectionInfo connInfo = hcatService.getJMSConnectionInfo(new URI("hcat://hcat.server.com:8020"));
        jmsService.registerForNotification(connInfo, topic, new HCatMessageHandler(server));
        MessageReceiver receiver1 = jmsService.getMessageReceiver(connInfo, topic);
        assertNotNull(receiver1);
        jmsService.unregisterFromNotification(connInfo, topic);
        receiver1 = jmsService.getMessageReceiver(connInfo, topic);
        assertEquals(null, receiver1);
    } catch (Exception e) {
        e.printStackTrace();
        fail("Exception encountered : " + e);
    }
}
Also used : HCatMessageHandler(org.apache.oozie.dependency.hcat.HCatMessageHandler) MessageReceiver(org.apache.oozie.jms.MessageReceiver) JMSConnectionInfo(org.apache.oozie.jms.JMSConnectionInfo) URI(java.net.URI) Test(org.junit.Test)

Aggregations

MessageReceiver (org.apache.oozie.jms.MessageReceiver)6 URI (java.net.URI)2 HCatMessageHandler (org.apache.oozie.dependency.hcat.HCatMessageHandler)2 ConnectionContext (org.apache.oozie.jms.ConnectionContext)2 DefaultConnectionContext (org.apache.oozie.jms.DefaultConnectionContext)2 JMSConnectionInfo (org.apache.oozie.jms.JMSConnectionInfo)2 MessageHandler (org.apache.oozie.jms.MessageHandler)2 Test (org.junit.Test)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ArrayList (java.util.ArrayList)1 JMSException (javax.jms.JMSException)1 MessageConsumer (javax.jms.MessageConsumer)1 Session (javax.jms.Session)1