Skip to content

JSON Event Processing (JEP)

xmljim edited this page Feb 6, 2018 · 1 revision

The JEP processor consists of four key parts:

  • JSONEventParser - This is the main orchestration between the constituent components. Like the JOM JSONParser, the methods are designed for parsing. The main difference is that the components are designed to fire and consume parsing events

  • JSONEventProcessor - Implementations of this interface read the underlying stream of data and initiate the events. There is no requirement for the processor to fire events to the EventHandler, but rather to collect the data that will be needed for the event and pass them to a JSONEventProvider

  • JSONEventProvider This interface is responsible for taking data provided by the JSONEventProcessor and creating events that can be consumed by the JSONEventHandler. With a provider, it is possible to create scenarios such as BlockingQueues and other mechanisms for managing how manages will be consumed by Event Handlers.

  • JSONEventHandler- Defines the events that can be consumed from the [JSONEventProvider]. This is the interface that must be implemented to process the JSON data. You choose to implement this interface directly, or you can take advantage of abstract implementations like EventHandler, or BaseEventHandler that can be extended while taking advantage of helper methods.

Tying all of these interfaces together is the ParserSettings class. This class is responsible for managing the configuration settings necessary for processors, providers, and event handlers. It's also use to instantiate all three components. There are number of static helper methods on ParserSettings that make it easier to build the full settings instance, including the ParserConfiguration which is used to by the Parser for orchestration, and to notify each of the components of each other.

Usage

There are default implementations of the JSONEventProcessor and JSONEventProvider interfaces that make the work of implementing a JEP process easier. The only thing you need to implement is a JSONEventHandler class, or extend the EventHandler or BaseEventHandler abstract classes with the event methods.

There's a working example of a concrete implementation of an EventHandler class called ConsoleEventHandler. This class emits event message information to stdout.

Once you implement a JSONEventHandler, you can run the process with code similar to the following:

//Instantiate your instance of the JSONEventHandler
MyEventHandler handler = new MyEventHandler();
// Create a new ParserSettings class this will create a configuration of your handler, along with
// the default JSONEventProcessor and default JSONEventProvider
ParserSettings settings = ParserSettings.newConfiguration(handler);

//Create a new EventParser
JSONEventParser parser = EventParser.newEventParser();
//Start the process using an inputstream, file, and the ParserSettings instance
parser.parse(theInputStream, settings);

When you invoke the parse method, this will start the JSONEventProcessor using the data source provided. The processor reads the data detecting specific tokens that will be used to fire events. These events are passed to the JSONEventProvider who may provide further processing (or not) and pass the event to JSONEventHandler implementation.

Clone this wiki locally