////
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.
////

GORM Data Services also support returning RxJava 1.x https://reactivex.io/RxJava/1.x/javadoc/rx/Observable.html[rx.Observable] or https://reactivex.io/RxJava/1.x/javadoc/rx/Single.html[rx.Single] types.

NOTE: RxJava 2.x support is planned for a future release

To use the RxJava support you need to ensure that the `grails-datastore-gorm-rx` dependencies is on the classpath by adding the following to `build.gradle`:

[source,groovy,subs="attributes"]
.build.gradle
----
compile "org.grails:grails-datastore-gorm-rx:{version}"
----

For example:

[source,groovy]
----
import rx.*

@Service(Book)
interface BookService {
   Single<Book> findOne(String title)
}
----

When a `rx.Single` is used then a single result is returned. To query multiple results use an `rx.Observable` instead:

[source,groovy]
----
import rx.*

@Service(Book)
interface BookService {
   Observable<Book> findBooks(String title)
}
----

For regular GORM entities, GORM will by default execute the persistence operation using RxJava's https://reactivex.io/RxJava/1.x/javadoc/rx/schedulers/Schedulers.html#io()[IO Scheduler].

NOTE: For RxGORM entities where the underlying database supports non-blocking access the database driver will schedule the operation accordingly.

You can run the operation on a different scheduler using the `RxSchedule` annotation:

[source,groovy]
----
import rx.*
import grails.gorm.rx.services.RxSchedule
import grails.gorm.services.Service
import rx.schedulers.Schedulers

@Service(Book)
interface BookService {

   @RxSchedule(scheduler = { Schedulers.newThread() })
   Observable<Book> findBooks(String title)
}
----