Now Event Handling in Grails is very Intersting.
It follows neither any Callack nor Listener approach. But it follows a Reactive Approach(Rx), Basically the Observable-Subscriber Pattern.
Event Handling in Grails is being done using the following annotations(in Grails Components i.e Controller, Services):
1) @Selector.
2) @Consumer. etc.
Let's walk through an example.
The Code that we have within a service method, which acts as a Subsriber to an event named myEvent.select is:
We can also send data(Objects) while publishing the Events, which can be handled in the Subscriber to that particular Event passed as a parameter to the method. Here we can see that the Object parameter to eventSelector() method is the data passed from the Event Published.
Now, the next Question that arises, that how we can publish Events along with Data so that the Subsribers could be in Action.
It can be done with the help of notify OR sendAndReceive methods.
The first parameter to notify() method is the Event name and the next is the Object Passed with the event i.e the Data.
For sendAndReceive the first and second parameters are same as notify, with this we can also pass a closure that gets executed after the value is being returned from the corresponding subsriber Subscriber and we can access the value as returned from the Subscriber in the closure.
By Default all the Services and Controllers in Grails implements Events trait, so with the help of @Consumer & @Selector annotation we can make a Sunscriber Component.
We can also make a POJO to subscribe to various events.
We need to declare the POJO to implement the Events trait and register it as normal bean in the Application Context, and then with the help of lifecycle phases of a bean(appropriate Lifecycle methods) we can make the POJO to subscribe to various events using the on() method.
Here is the example:
An Event can get fired or Published by any component at any point of time and if there are any Subscribers to that Event then, those are executed. Grails uses REACTOR API to achieve this, which is one of the Most innovative and updated topic in Present time i.e. The Reactive approach.
It follows neither any Callack nor Listener approach. But it follows a Reactive Approach(Rx), Basically the Observable-Subscriber Pattern.
Event Handling in Grails is being done using the following annotations(in Grails Components i.e Controller, Services):
1) @Selector.
2) @Consumer. etc.
Let's walk through an example.
The Code that we have within a service method, which acts as a Subsriber to an event named myEvent.select is:
@Consumer
@Transactional
class AuthorService {
@Selector('myEvent.select')
def eventSelector(Object object)
{
println " In AuthorService The Object is $object"
}
}
The Consumer annotation manifests this service to be a consumer of events, published from various sources. With the help of Selector annotation, we annotatate methods within Grails Components to deal with various events published from various sources.We can also send data(Objects) while publishing the Events, which can be handled in the Subscriber to that particular Event passed as a parameter to the method. Here we can see that the Object parameter to eventSelector() method is the data passed from the Event Published.
Now, the next Question that arises, that how we can publish Events along with Data so that the Subsribers could be in Action.
Subscribers Responsive to Stimulus!!!!
It can be done with the help of notify OR sendAndReceive methods.
The first parameter to notify() method is the Event name and the next is the Object Passed with the event i.e the Data.
For sendAndReceive the first and second parameters are same as notify, with this we can also pass a closure that gets executed after the value is being returned from the corresponding subsriber Subscriber and we can access the value as returned from the Subscriber in the closure.
By Default all the Services and Controllers in Grails implements Events trait, so with the help of @Consumer & @Selector annotation we can make a Sunscriber Component.
We can also make a POJO to subscribe to various events.
We need to declare the POJO to implement the Events trait and register it as normal bean in the Application Context, and then with the help of lifecycle phases of a bean(appropriate Lifecycle methods) we can make the POJO to subscribe to various events using the on() method.
Here is the example:
class MyEventListener implements Events {
EventData eventData
@PostConstruct
void init() {
on("myEvent.select") {
println "Event fired! for $it"
}
}
}
and the declaration in resources.groovy is:
/**
* Registering Event Listener
*/
eventData(EventData){
data = 'All is Good'
}
myEventListener(MyEventListener){
eventData = ref('eventData')
}
So when the bean named myEventListener gets registered in the Application Context, then during post initiaization of the bean, the init() gets executed which registers the subscriber for the Event Named myEvent.select with the help of on() method and the closure gets executed when this Subscriber receives the Event.An Event can get fired or Published by any component at any point of time and if there are any Subscribers to that Event then, those are executed. Grails uses REACTOR API to achieve this, which is one of the Most innovative and updated topic in Present time i.e. The Reactive approach.



No comments:
Post a Comment