Search in sources :

Example 1 with Event

use of com.aphyr.riemann.Proto.Event in project nifi by apache.

the class PutRiemann method onTrigger.

@Override
public void onTrigger(ProcessContext context, ProcessSession session) throws ProcessException {
    // in the connection.
    if (riemannClient == null || !riemannClient.isConnected()) {
        // clean up the client and attempt to re-initialize the processor
        cleanUpClient();
        onScheduled(context);
    }
    List<FlowFile> incomingFlowFiles = session.get(batchSize);
    List<FlowFile> successfulFlowFiles = new ArrayList<>(incomingFlowFiles.size());
    List<Event> eventsQueue = new ArrayList<>(incomingFlowFiles.size());
    for (FlowFile flowFile : incomingFlowFiles) {
        try {
            eventsQueue.add(FlowFileToEvent.fromAttributes(context, customAttributes, flowFile));
            successfulFlowFiles.add(flowFile);
        } catch (NumberFormatException e) {
            getLogger().warn("Unable to create Riemann event.", e);
            session.transfer(flowFile, REL_FAILURE);
        }
    }
    try {
        if (transport == Transport.TCP) {
            Proto.Msg returnMessage = riemannClient.sendEvents(eventsQueue).deref(writeTimeout, TimeUnit.MILLISECONDS);
            if (returnMessage == null) {
                context.yield();
                throw new ProcessException("Timed out writing to Riemann!");
            }
        } else {
            riemannClient.sendEvents(eventsQueue);
        }
        riemannClient.flush();
        session.transfer(successfulFlowFiles, REL_SUCCESS);
        session.commit();
    } catch (Exception e) {
        context.yield();
        session.transfer(incomingFlowFiles);
        session.commit();
        throw new ProcessException("Failed writing to Riemann\n" + e.getMessage());
    }
}
Also used : FlowFile(org.apache.nifi.flowfile.FlowFile) ProcessException(org.apache.nifi.processor.exception.ProcessException) Proto(com.aphyr.riemann.Proto) ArrayList(java.util.ArrayList) Event(com.aphyr.riemann.Proto.Event) ProcessException(org.apache.nifi.processor.exception.ProcessException) IOException(java.io.IOException)

Aggregations

Proto (com.aphyr.riemann.Proto)1 Event (com.aphyr.riemann.Proto.Event)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 FlowFile (org.apache.nifi.flowfile.FlowFile)1 ProcessException (org.apache.nifi.processor.exception.ProcessException)1