Search in sources :

Example 1 with Request

use of oap.zabbix.Request in project oap by oaplatform.

the class ZabbixAppender method dispatchEvents.

private void dispatchEvents() throws InterruptedException, IOException {
    ILoggingEvent event = deque.takeFirst();
    val events = new ArrayList<ILoggingEvent>();
    events.add(event);
    val list = new ArrayList<Data>();
    addEvent(event, list);
    while ((event = deque.pollFirst()) != null) {
        events.add(event);
        addEvent(event, list);
    }
    val request = new Request(list);
    try {
        if (!socketConnectionCouldBeEstablished()) {
            throw new ConnectException();
        }
        addInfo(peerId + "connection established");
        val outputStream = socket.getOutputStream();
        val inputStream = socket.getInputStream();
        addInfo("zabbixRequest = " + request);
        ZabbixRequest.writeExternal(request, outputStream);
        outputStream.flush();
        val buf = new byte[1024];
        val responseBaos = new ByteArrayOutputStream();
        while (true) {
            int read = inputStream.read(buf);
            if (read <= 0) {
                break;
            }
            responseBaos.write(buf, 0, read);
        }
        val bResponse = responseBaos.toByteArray();
        if (bResponse.length < 13) {
            addInfo("response.length < 13");
        } else {
            String jsonString = new String(bResponse, 13, bResponse.length - 13, StandardCharsets.UTF_8);
            addInfo("response = " + jsonString);
        }
    } catch (IOException e) {
        tryReAddingEventsToFrontOfQueue(events);
        throw e;
    } finally {
        CloseUtil.closeQuietly(socket);
        socket = null;
        addInfo(peerId + "connection closed");
    }
}
Also used : lombok.val(lombok.val) ArrayList(java.util.ArrayList) Request(oap.zabbix.Request) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) ConnectException(java.net.ConnectException)

Aggregations

ILoggingEvent (ch.qos.logback.classic.spi.ILoggingEvent)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 ConnectException (java.net.ConnectException)1 ArrayList (java.util.ArrayList)1 lombok.val (lombok.val)1 Request (oap.zabbix.Request)1