[[geode-continuous-query]]
== Continuous Query
:geode-name: {apache-geode-name}


Arguably, the most invaluable of applications are those that can process a stream of events as they happen,
and intelligently react in near real-time to the countless changes in the data over time.  The most useful
of frameworks are those that can make processing a stream of events as they happen, as easy as possible.

Spring Boot for {geode-name} does just that, without users having to perform any complex setup or configure any
necessary infrastructure components to enable such functionality.  Developers can simply define the criteria for the
data they are interested in and implement a handler to process the stream of events as they occur.

{geode-name} make defining criteria for data of interests easy when using
{apache-geode-docs}/developing/continuous_querying/chapter_overview.html[Continuous Query (CQ)]. With CQ, you can
express the criteria matching the data of interests using a query predicate.  {geode-name} implements
the {apache-geode-docs}/developing/querying_basics/query_basics.html[Object Query Language (OQL)] for defining
and executing queries.  OQL is not unlike SQL, and supports projections, query predicates, ordering and aggregates.
And, when used in CQs, they execute continuously, firing events when the data changes in such ways as to match
the criteria expressed in the query predicate.

Spring Boot for {geode-name} combines the ease of expressing interests in data using an OQL query statement with
implementing the listener handler callback, in 1 easy step.

For example, suppose we want to perform some follow up action anytime a customer's financial loan application
is either approved or denied.

First, the application model for our `EligibilityDecision` class might look something like:

.EligibilityDecision class
[source,java]
----
@Region("EligibilityDecisions")
class EligibilityDecision {

    private final Person person;

    private Status status = Status.UNDETERMINED;

    private final Timespan timespan;

    enum Status {

        APPROVED,
        DENIED,
        UNDETERMINED,

    }
}
----

Then, we can implement and declare our CQ event handler methods to be notified when a decision
is either APPROVED or DENIED:

[source,java]
----
@Component
class EligibilityDecisionPostProcessor {


    @ContinuousQuery(name = "ApprovedDecisionsHandler",
        query = "SELECT decisions.*
                 FROM /EligibilityDecisions decisions
                 WHERE decisions.getStatus().name().equalsIgnoreCase('APPROVED')")
    public void processApprovedDecisions(CqEvent event) {
        // ...
    }

    @ContinuousQuery(name = "DeniedDecisionsHandler",
        query = "SELECT decisions.*
                 FROM /EligibilityDecisions decisions
                 WHERE decisions.getStatus().name().equalsIgnoreCase('DENIED')")
    public void processDeniedDecisions(CqEvent event) {
        // ...
    }
}
----

Thus, anytime eligibility is processed and a decision as been made, either approved or denied, our application
will get notified, and as an application developer, you are free to code your handler and respond to the event
anyway you like.  And, because our Continuous Query handler class is a component, or bean in the Spring
`ApplicationContext`, you can auto-wire any other beans necessary to carry out the application's intended function.

This is not unlike Spring's {spring-framework-docs}/integration.html#jms-annotated[Annotation-driven listener endpoints]
used in (JMS) message listeners/handlers, except in Spring Boot for {geode-name}, you do not need to do anything special
to enable this functionality.  Just declare the `@ContinuousQuery` annotation on any POJO method and off you go.
