Search in sources :

Example 1 with Event

use of com.twitter.hbc.core.event.Event in project nifi by apache.

the class GetTwitter method onTrigger.

@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {
    final Event event = eventQueue.poll();
    if (event != null) {
        switch(event.getEventType()) {
            case STOPPED_BY_ERROR:
                getLogger().error("Received error {}: {} due to {}. Will not attempt to reconnect", new Object[] { event.getEventType(), event.getMessage(), event.getUnderlyingException() });
                break;
            case CONNECTION_ERROR:
            case HTTP_ERROR:
                getLogger().error("Received error {}: {}. Will attempt to reconnect", new Object[] { event.getEventType(), event.getMessage() });
                client.reconnect();
                break;
            default:
                break;
        }
    }
    final String tweet = messageQueue.poll();
    if (tweet == null) {
        context.yield();
        return;
    }
    FlowFile flowFile = session.create();
    flowFile = session.write(flowFile, new OutputStreamCallback() {

        @Override
        public void process(final OutputStream out) throws IOException {
            out.write(tweet.getBytes(StandardCharsets.UTF_8));
        }
    });
    final Map<String, String> attributes = new HashMap<>();
    attributes.put(CoreAttributes.MIME_TYPE.key(), "application/json");
    attributes.put(CoreAttributes.FILENAME.key(), flowFile.getAttribute(CoreAttributes.FILENAME.key()) + ".json");
    flowFile = session.putAllAttributes(flowFile, attributes);
    session.transfer(flowFile, REL_SUCCESS);
    session.getProvenanceReporter().receive(flowFile, Constants.STREAM_HOST + client.getEndpoint().getURI().toString());
}
Also used : FlowFile(org.apache.nifi.flowfile.FlowFile) HashMap(java.util.HashMap) OutputStream(java.io.OutputStream) Event(com.twitter.hbc.core.event.Event) OutputStreamCallback(org.apache.nifi.processor.io.OutputStreamCallback)

Aggregations

Event (com.twitter.hbc.core.event.Event)1 OutputStream (java.io.OutputStream)1 HashMap (java.util.HashMap)1 FlowFile (org.apache.nifi.flowfile.FlowFile)1 OutputStreamCallback (org.apache.nifi.processor.io.OutputStreamCallback)1