Search in sources :

Example 11 with RTCCategory

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);
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) InputStream(java.io.InputStream) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) HttpPostInfo(org.opennms.netmgt.rtc.datablock.HttpPostInfo) StringReader(java.io.StringReader) EuiLevel(org.opennms.netmgt.xml.rtc.EuiLevel) RTCCategory(org.opennms.netmgt.rtc.datablock.RTCCategory) ConnectException(java.net.ConnectException)

Aggregations

RTCCategory (org.opennms.netmgt.rtc.datablock.RTCCategory)11 EuiLevel (org.opennms.netmgt.xml.rtc.EuiLevel)6 InputStream (java.io.InputStream)3 Date (java.util.Date)3 Test (org.junit.Test)3 Category (org.opennms.netmgt.xml.rtc.Category)3 Node (org.opennms.netmgt.xml.rtc.Node)3 StringReader (java.io.StringReader)2 HashMap (java.util.HashMap)2 OnmsMonitoredService (org.opennms.netmgt.model.OnmsMonitoredService)2 OnmsOutage (org.opennms.netmgt.model.OnmsOutage)2 HttpPostInfo (org.opennms.netmgt.rtc.datablock.HttpPostInfo)2 RTCNode (org.opennms.netmgt.rtc.datablock.RTCNode)2 Preconditions (com.google.common.base.Preconditions)1 Lists (com.google.common.collect.Lists)1 Maps (com.google.common.collect.Maps)1 IOException (java.io.IOException)1 UndeclaredThrowableException (java.lang.reflect.UndeclaredThrowableException)1 ConnectException (java.net.ConnectException)1 MalformedURLException (java.net.MalformedURLException)1