Search in sources :

Example 1 with ConnectionContext

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

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

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

the class JMSAccessorService method destroy.

@Override
public void destroy() {
    LOG.info("Destroying JMSAccessor service ");
    receiversMap.clear();
    LOG.info("Closing JMS connections");
    for (ConnectionContext conn : connectionMap.values()) {
        conn.close();
    }
    if (jmsProducerConnContext != null) {
        jmsProducerConnContext.close();
    }
    connectionMap.clear();
}
Also used : ConnectionContext(org.apache.oozie.jms.ConnectionContext) DefaultConnectionContext(org.apache.oozie.jms.DefaultConnectionContext)

Example 4 with ConnectionContext

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

the class TestJMSJobEventListener method testCoordinatorActionSelectors.

@Test
public void testCoordinatorActionSelectors() throws ParseException {
    JMSJobEventListener coordEventListener = new JMSJobEventListener();
    coordEventListener.init(conf);
    Date startDate = DateUtils.parseDateUTC("2012-07-22T00:00Z");
    Date nominalTime = DateUtils.parseDateUTC("2011-07-11T00:00Z");
    CoordinatorActionEvent cae = new CoordinatorActionEvent("caId1", "caJobId1", CoordinatorAction.Status.FAILED, "user1", "wf-app-name1", nominalTime, startDate, null);
    ConnectionContext jmsContext = getConnectionContext();
    try {
        Session session = jmsContext.createSession(Session.AUTO_ACKNOWLEDGE);
        String selector = JMSHeaderConstants.USER + "='user1'";
        MessageConsumer consumer = jmsContext.createConsumer(session, coordEventListener.getTopic(cae), selector);
        coordEventListener.onCoordinatorActionEvent(cae);
        TextMessage message = (TextMessage) consumer.receive(5000);
        CoordinatorActionMessage coordActionFailMessage = JMSMessagingUtils.getEventMessage(message);
        Assert.assertEquals(CoordinatorAction.Status.FAILED, coordActionFailMessage.getStatus());
        assertEquals("user1", coordActionFailMessage.getUser());
        assertEquals(MessageType.JOB, coordActionFailMessage.getMessageType());
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : CoordinatorActionMessage(org.apache.oozie.client.event.message.CoordinatorActionMessage) MessageConsumer(javax.jms.MessageConsumer) ConnectionContext(org.apache.oozie.jms.ConnectionContext) Date(java.util.Date) TextMessage(javax.jms.TextMessage) ParseException(java.text.ParseException) JMSJobEventListener(org.apache.oozie.jms.JMSJobEventListener) Session(javax.jms.Session) Test(org.junit.Test)

Example 5 with ConnectionContext

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

the class TestJMSJobEventListener method testOnWorkflowJobSuccessEvent.

@Test
public void testOnWorkflowJobSuccessEvent() throws ParseException {
    JMSJobEventListener wfEventListener = new JMSJobEventListener();
    wfEventListener.init(conf);
    Date startDate = DateUtils.parseDateUTC("2012-07-22T00:00Z");
    Date endDate = new Date();
    WorkflowJobEvent wfe = new WorkflowJobEvent("wfId1", "caId1", WorkflowJob.Status.SUCCEEDED, "user1", "wf-app-name1", startDate, endDate);
    ConnectionContext jmsContext = getConnectionContext();
    try {
        Session session = jmsContext.createSession(Session.AUTO_ACKNOWLEDGE);
        MessageConsumer consumer = jmsContext.createConsumer(session, wfEventListener.getTopic(wfe));
        wfEventListener.onWorkflowJobEvent(wfe);
        TextMessage message = (TextMessage) consumer.receive(5000);
        WorkflowJobMessage wfSuccMessage = JMSMessagingUtils.getEventMessage(message);
        assertEquals(WorkflowJob.Status.SUCCEEDED, wfSuccMessage.getStatus());
        assertEquals(startDate, wfSuccMessage.getStartTime());
        assertEquals(endDate, wfSuccMessage.getEndTime());
        assertEquals("wfId1", wfSuccMessage.getId());
        assertEquals("caId1", wfSuccMessage.getParentId());
        assertEquals(MessageType.JOB, wfSuccMessage.getMessageType());
        assertEquals(AppType.WORKFLOW_JOB, wfSuccMessage.getAppType());
        assertEquals(EventStatus.SUCCESS, wfSuccMessage.getEventStatus());
        assertEquals("user1", wfSuccMessage.getUser());
        assertEquals("wf-app-name1", wfSuccMessage.getAppName());
        wfEventListener.destroy();
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : MessageConsumer(javax.jms.MessageConsumer) WorkflowJobMessage(org.apache.oozie.client.event.message.WorkflowJobMessage) ConnectionContext(org.apache.oozie.jms.ConnectionContext) Date(java.util.Date) TextMessage(javax.jms.TextMessage) ParseException(java.text.ParseException) JMSJobEventListener(org.apache.oozie.jms.JMSJobEventListener) Session(javax.jms.Session) Test(org.junit.Test)

Aggregations

ConnectionContext (org.apache.oozie.jms.ConnectionContext)24 Test (org.junit.Test)18 ParseException (java.text.ParseException)15 Date (java.util.Date)15 MessageConsumer (javax.jms.MessageConsumer)15 Session (javax.jms.Session)15 TextMessage (javax.jms.TextMessage)15 JMSJobEventListener (org.apache.oozie.jms.JMSJobEventListener)15 DefaultConnectionContext (org.apache.oozie.jms.DefaultConnectionContext)8 WorkflowJobMessage (org.apache.oozie.client.event.message.WorkflowJobMessage)7 CoordinatorActionMessage (org.apache.oozie.client.event.message.CoordinatorActionMessage)5 Configuration (org.apache.hadoop.conf.Configuration)4 JMSConnectionInfo (org.apache.oozie.jms.JMSConnectionInfo)4 URI (java.net.URI)3 BrokerService (org.apache.activemq.broker.BrokerService)3 MessageReceiver (org.apache.oozie.jms.MessageReceiver)2 JMSAccessorService (org.apache.oozie.service.JMSAccessorService)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ArrayList (java.util.ArrayList)1 Random (java.util.Random)1