}
@Subscribe
public boolean unRegisterListener(MonitorID monitorID) throws AiravataMonitorException {
Iterator<MonitorID> iterator = finishQueue.iterator();
MonitorID next = null;
while(iterator.hasNext()){
next = iterator.next();
if(next.getJobID().endsWith(monitorID.getJobID())){
break;
}
}
if(next == null) {
logger.error("Job has removed from the queue, old obsolete message recieved");
return false;
}
String channelID = CommonUtils.getChannelID(next);
if (JobState.FAILED.equals(monitorID.getStatus()) || JobState.COMPLETE.equals(monitorID.getStatus())) {
finishQueue.remove(next);
// if this is the last job in the queue at this point with the same username and same host we
// close the channel and close the connection and remove it from availableChannels
if (CommonUtils.isTheLastJobInQueue(finishQueue, next)) {
logger.info("There are no jobs to monitor for common ChannelID:" + channelID + " , so we unsubscribe it" +
", incase new job created we do subscribe again");
Channel channel = availableChannels.get(channelID);
if (channel == null) {
logger.error("Already Unregistered the listener");
throw new AiravataMonitorException("Already Unregistered the listener");
} else {
try {
channel.queueUnbind(channel.queueDeclare().getQueue(), "glue2.computing_activity", CommonUtils.getRoutingKey(next));
channel.close();
channel.getConnection().close();
availableChannels.remove(channelID);
} catch (IOException e) {
logger.error("Error unregistering the listener");
throw new AiravataMonitorException("Error unregistering the listener");
}
}
}
}
next.setStatus(monitorID.getStatus());
publisher.publish(new JobStatusChangeRequest(next, new JobIdentity(next.getExperimentID(), next.getWorkflowNodeID(), next.getTaskID(), next.getJobID()),next.getStatus()));
return true;
}