////
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.
////

The simplest way to trigger event notification is via the link:{api}grails/events/annotation/Publisher.html[Publisher] annotation.

For example:

[source,groovy]
.SumService.groovy
----
import grails.events.annotation.*
...
include::{sourcedir}/grails-events/transforms/src/test/groovy/grails/events/annotation/PubSubSpec.groovy[tags=publisher]
----

What the above does is take the return value of the method and publish an event using an event `id` that is the same as the method name.

If you wish to change the event `id` you can provide a value to `@Publisher`:

[source,groovy]
----
@Publisher('myEvent')
----

If you want more flexiblity then you could simulate the behaviour of annotation by writing the following code:

[source,groovy]
.SumService.groovy
----
import grails.events.*
...
include::{sourcedir}/grails-events/transforms/src/test/groovy/grails/events/ManualPubSubSpec.groovy[tags=publisher]
----

Notice in the above example, the link:{api}grails/events/EventPublisher.html[EventPublisher] trait is explicitly implemented.

Although generally the annotation approach is recommended because it is simpler and more concise, the `EventPublisher` trait does provide more flexibility in terms of being able to publish multiple events in a single method and so on.