[id='creating-continuous-queries_{context}']
= Creating continuous queries
You can create continuous queries for remote and embedded caches.

.Procedure

. Create a `Query` object.
. Obtain the `ContinuousQuery` object of your cache by calling
the appropriate method:
+
* Remote caches: `org.infinispan.client.hotrod.Search.getContinuousQuery(RemoteCache<K, V> cache)`
* Embedded caches: `org.infinispan.query.Search.getContinuousQuery(Cache<K, V> cache)`
+
. Register the query and a `ContinuousQueryListener` object as follows:
+
[source,java]
----
continuousQuery.addContinuousQueryListener(query, listener);
----
+
. When you no longer need the continuous query, remove the listener as follows:
+
[source,java]
----
continuousQuery.removeContinuousQueryListener(listener);
----

[discrete]
== Continuous query example

The following code example demonstrates a simple continuous query with an embedded cache.

In this example, the listener receives notifications when any `Person` instances under the age of 21 are added to the cache.
Those `Person` instances are also added to the "matches" map.
When the entries are removed from the cache or their age becomes greater than or equal to 21, they are removed from "matches" map.
⁠
[source,java,title="Registering a Continuous Query"]
----
include::code_examples/RegisteringContinuousQuery.java[]
----
