http://stackoverflow.com/questions/37799265/event-received-only-by-one-bean-despite-having-several-ones
UPDATE1:
---
[Staphanee Nicoll][4] has described in [spring blog post][5] as below:
> The actual processing of the event will respect the semantics you set
> on your bean. So if you set prototype we are going to create a new
> instance of the bean for you prior to call the method on it. And we're
> going to do that for every event. If you expect all your *existing*
> prototype instances to be called for a particular event, that's not
> going to happen (we never did multicast an event and that
> infrastructure is not meant to do that).
---
You have missed to add `cvp.publish()` for calling every custom event handler.
Another issue `static` section is already described by [costi-ciudatu][1]
Hope it will work.
public static void main(String[] args) {
ConfigurableApplicationContext context =
new ClassPathXmlApplicationContext("Beans.xml");
CustomEventPublisher cvp =
(CustomEventPublisher) context.getBean("customEventPublisher");
// Scope of these beans is prototype
// CustomEventHandler ce = (CustomEventHandler) context.getBean("customEventHandler");
// CustomEventHandler ce1 = (CustomEventHandler) context.getBean("customEventHandler");
// CustomEventHandler ce2 = (CustomEventHandler) context.getBean("customEventHandler");
for(int i=0; i<3; i++) {
CustomEventHandler ce = (CustomEventHandler) context.getBean("customEventHandler");
cvp.publish();
}
}
**OR,** You can use this code also.
public static void main(String[] args) {
ConfigurableApplicationContext context =
new ClassPathXmlApplicationContext("Beans.xml");
CustomEventPublisher cvp =
(CustomEventPublisher) context.getBean("customEventPublisher");
// Scope of these beans is prototype
CustomEventHandler ce = (CustomEventHandler) context.getBean("customEventHandler");
cvp.publish();
CustomEventHandler ce1 = (CustomEventHandler) context.getBean("customEventHandler");
cvp.publish();
CustomEventHandler ce2 = (CustomEventHandler) context.getBean("customEventHandler");
cvp.publish();
}
For learning more, you can go through this tutorial: [Custom events in spring][2]
Github Resource Link:
---
[Spring app event demo][3]
[1]: http://stackoverflow.com/users/262683/costi-ciudatu
[2]: http://www.tutorialspoint.com/spring/custom_events_in_spring.htm
[3]: https://github.com/omanand/spring-app-event-demo
[4]: https://disqus.com/by/snicoll/
[5]: https://spring.io/blog/2015/02/11/better-application-events-in-spring-framework-4-2
UPDATE1:
---
[Staphanee Nicoll][4] has described in [spring blog post][5] as below:
> The actual processing of the event will respect the semantics you set
> on your bean. So if you set prototype we are going to create a new
> instance of the bean for you prior to call the method on it. And we're
> going to do that for every event. If you expect all your *existing*
> prototype instances to be called for a particular event, that's not
> going to happen (we never did multicast an event and that
> infrastructure is not meant to do that).
---
You have missed to add `cvp.publish()` for calling every custom event handler.
Another issue `static` section is already described by [costi-ciudatu][1]
Hope it will work.
public static void main(String[] args) {
ConfigurableApplicationContext context =
new ClassPathXmlApplicationContext("Beans.xml");
CustomEventPublisher cvp =
(CustomEventPublisher) context.getBean("customEventPublisher");
// Scope of these beans is prototype
// CustomEventHandler ce = (CustomEventHandler) context.getBean("customEventHandler");
// CustomEventHandler ce1 = (CustomEventHandler) context.getBean("customEventHandler");
// CustomEventHandler ce2 = (CustomEventHandler) context.getBean("customEventHandler");
for(int i=0; i<3; i++) {
CustomEventHandler ce = (CustomEventHandler) context.getBean("customEventHandler");
cvp.publish();
}
}
**OR,** You can use this code also.
public static void main(String[] args) {
ConfigurableApplicationContext context =
new ClassPathXmlApplicationContext("Beans.xml");
CustomEventPublisher cvp =
(CustomEventPublisher) context.getBean("customEventPublisher");
// Scope of these beans is prototype
CustomEventHandler ce = (CustomEventHandler) context.getBean("customEventHandler");
cvp.publish();
CustomEventHandler ce1 = (CustomEventHandler) context.getBean("customEventHandler");
cvp.publish();
CustomEventHandler ce2 = (CustomEventHandler) context.getBean("customEventHandler");
cvp.publish();
}
For learning more, you can go through this tutorial: [Custom events in spring][2]
Github Resource Link:
---
[Spring app event demo][3]
[1]: http://stackoverflow.com/users/262683/costi-ciudatu
[2]: http://www.tutorialspoint.com/spring/custom_events_in_spring.htm
[3]: https://github.com/omanand/spring-app-event-demo
[4]: https://disqus.com/by/snicoll/
[5]: https://spring.io/blog/2015/02/11/better-application-events-in-spring-framework-4-2
No comments:
Post a Comment