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


Some applications must process a stream of events as they happen and intelligently react in (near) real-time to
the countless changes in the data over time. Those applications need frameworks 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 define the criteria for the
data of interest and implement a handler (listener) to process the stream of events as they occur.

{apache-geode-docs}/developing/continuous_querying/chapter_overview.html[Continuous Query (CQ)] lets you
easily define your criteria for the data you need. With CQ, you can express the criteria that match the data you need
by specifying 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 resembles SQL and supports projections, query predicates, ordering,
and aggregates. Also, 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 identifying the data you need by using an OQL query statement with
implementing the listener callback (handler) in one easy step.

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

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

.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 an eligibility 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, when eligibility is processed and a decision has been made, either approved or denied, our application gets
notified, and as an application developer, you are free to code your handler and respond to the event any way you like.
Also, because our Continuous Query (CQ) handler class is a component (or a 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],
which are used in (JMS) message listeners and handlers, except in Spring Boot for {geode-name}, you need not do anything
special to enable this functionality. You can declare the `@ContinuousQuery` annotation on any POJO method and go to
work on other things.
