use of org.opennms.netmgt.rtc.datablock.RTCCategory in project opennms by OpenNMS.
the class DataSender method subscribe.
/**
* Subscribe - Add the received URL and related info to the category->URLs map
* so the sendData() can send out to appropriate URLs for each category.
* Also send the latest info for the category
*
* @param url a {@link java.lang.String} object.
* @param catlabel a {@link java.lang.String} object.
* @param user a {@link java.lang.String} object.
* @param passwd a {@link java.lang.String} object.
*/
public synchronized void subscribe(final String url, final String catlabel, final String user, final String passwd) {
// send category data to the newly subscribed URL
// look up info for this category
final RTCCategory cat = m_dataMgr.getCategories().get(catlabel);
if (cat == null) {
// oops! category for which we have no info!
LOG.warn("RTC: No information available for category: {}", catlabel);
return;
}
// create new HttpPostInfo
final HttpPostInfo postInfo;
try {
postInfo = new HttpPostInfo(url, catlabel, user, passwd);
} catch (final MalformedURLException mue) {
LOG.warn("ERROR subscribing: Invalid URL '{}' - Data WILL NOT be SENT to the specified url", url);
return;
}
// Add the URL to the list for the specified category
Set<HttpPostInfo> urlList = m_catUrlMap.get(catlabel);
if (urlList == null) {
urlList = new HashSet<HttpPostInfo>();
m_catUrlMap.put(catlabel, urlList);
}
if (!urlList.add(postInfo)) {
LOG.debug("Already subscribed to URL: {}\tcatlabel: {}\tuser: {} - IGNORING LATEST subscribe event", url, catlabel, user);
} else {
LOG.debug("Subscribed to URL: {}\tcatlabel: {}\tuser:{}", url, catlabel, user);
}
try {
m_dsrPool.execute(new Runnable() {
@Override
public void run() {
// send data
InputStream inp = null;
try {
LOG.debug("DataSender: posting data to: {}", url);
final EuiLevel euidata = m_dataMgr.getEuiLevel(cat);
// Connect with a fairly long timeout to allow the web UI time to register the
// {@link RTCPostServlet}. Actually, this doesn't seem to work because the POST
// will immediately throw a {@link ConnectException} if the web UI isn't ready
// yet. Oh well.
final String marshaledUeiData = JaxbUtils.marshal(euidata);
try (final StringReader inr = new StringReader(marshaledUeiData)) {
inp = HttpUtils.post(postInfo.getURL(), inr, user, passwd, 8 * HttpUtils.DEFAULT_POST_BUFFER_SIZE, 60000);
}
final byte[] tmp = new byte[1024];
int bytesRead;
while ((bytesRead = inp.read(tmp)) != -1) {
if (LOG.isDebugEnabled()) {
if (bytesRead > 0) {
LOG.debug("DataSender: post response: {}", new String(tmp, 0, bytesRead));
}
}
}
LOG.debug("DataSender: posted data for category: {}", catlabel);
} catch (final ConnectException e) {
// These exceptions will be thrown if we try to POST RTC data before the web UI is available.
// Don't log a large stack trace for this because it will happen during startup before the
// RTCPostServlet is ready to handle requests.
LOG.warn("DataSender: Unable to send category '{}' to URL '{}': {}", catlabel, url, e.getMessage());
} catch (final Throwable t) {
LOG.warn("DataSender: Unable to send category '{}' to URL '{}'", catlabel, url, t);
} finally {
IOUtils.closeQuietly(inp);
}
}
});
} catch (RejectedExecutionException e) {
LOG.warn("Unable to queue datasender. The task was rejected by the pool. Current queue size: {}.", m_queue.size(), e);
}
}
Aggregations