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;
}
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);
}
}
}
}
}
}
}
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());
}
}
}
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;
}
}
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);
}
}
Aggregations