Event Listeners being called twice.

870 views
Skip to first unread message

Trevor Summerfield

unread,
Jul 31, 2014, 2:48:53 PM7/31/14
to motec...@googlegroups.com
I've built and deployed a module based upon the HelloWorld example on: https://code.google.com/p/motech/wiki/MotechHelloWorld

I'm having an issue where my eventListeners are all called twice for one give event emission - I believe the listeners are registered correctly.

Any ideas? Thanks.

Paweł Gesek

unread,
Jul 31, 2014, 5:39:14 PM7/31/14
to motec...@googlegroups.com
Remove
<Context-File>META-INF/spring/blueprint.xml</Context-File>

from your pom. Files from META-INF/spring are picked are automatically, specifying it in the pom leads to two contexts being created and the listener is being registered twice.

Regards,
Paweł


W dniu 2014-07-31 20:48, Trevor Summerfield pisze:
--
You received this message because you are subscribed to the Google Groups "MOTECH Developer" group.
To unsubscribe from this group and stop receiving emails from it, send an email to motech-dev+...@googlegroups.com.
To post to this group, send email to motec...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/motech-dev/d2cf47db-a53d-404c-b57f-216691b2778c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Koshal Thirumalai

unread,
Dec 5, 2014, 6:12:50 AM12/5/14
to motec...@googlegroups.com
I'm running into the same issue now and don't have the blueprint.xml file reference in my pom either. My handler class is decorated as @Component and a component scan specified in the applicationContext.xml.

The component seems to be getting initialized twice (when the bundle is installed), as seen in the log line from the constructor - 

2014-12-05 02:09:44,274 INFO  [org.motechproject.nms.events.Handler] Initialized handler
[INFO] Initializing Spring FrameworkServlet 'servlet_33'
2014-12-05 02:09:44,373 INFO  [org.motechproject.nms.events.Handler] Initialized handler
[INFO] Initialized servlet for org.motechproject.nms [149]

This causes 2 listeners to be registered on the same event and 2 actions performed for every event - 

2014-12-05 02:11:49,951 INFO  [org.motechproject.nms.web.MAController] Received call to status API @ 2014-12-05T10:11:49.951Z
2014-12-05 02:11:52,810 INFO  [org.motechproject.nms.web.MAController] Called save bookmark
2014-12-05 02:11:52,812 INFO  [org.motechproject.nms.service.impl.MAServiceImpl] Got bookmark event, sending it
2014-12-05 02:11:52,860 INFO  [org.motechproject.nms.events.Handler] handled event for bookmark save with name: 2014-12-05T02:11:52.811-08:00
2014-12-05 02:11:52,864 INFO  [org.motechproject.nms.events.Handler] handled event for bookmark save with name: 2014-12-05T02:11:52.811-08:00

Here's what I've tried so far -
> removing <context:annotation-config /> from applicationContext.xml, since context:component-scan is supposed to be a superset of annotation (source)
> removing context-file reference to applicationContext.xml from pom.xml service mapping fails
> Google & stack overflow

The closest answer that I've arrived at is here - http://stackoverflow.com/a/18274300 - where they talk about multiple contexts existing in Spring for root and servlet. This seems to be close since my event handler seems to be getting called before the servlet initialization and again, inside of it. I can't seem to figure out how to only load the component (handler) once in the servlet context?

Source - https://github.com/koshalt/nms (simple modified hello world module with eventing)

Thanks,
Koshal


Koshal Thirumalai

unread,
Dec 5, 2014, 6:58:36 AM12/5/14
to motec...@googlegroups.com
To add to the wall of text from earlier, the issue is resolved by deleting the applicationContext.xml and the removing the reference from the pom.xml. Then we would just need to add the component-scan, annotation and mvc tags in our blueprint and everything work as expected with one handler being registered. (see attached patch)

I'm trying to understand if we should stop generating the applicationContext.xml from the archetypes.

Thanks,
Koshal
duplicate-fix.patch

Sebastian Brudziński

unread,
Dec 5, 2014, 7:16:08 AM12/5/14
to motec...@googlegroups.com
I've just been testing it, and simply removing the <Context-File>META-INF/motech/applicationContext.xml</Context-File> resolved the problem for me. There should be no need to remove the context file completely.

Concerning the archetypes, I'd also just remove the <Context-File> entry from the pom, rather than the file itself.

Best regards,
Sebastian.

Koshal Thirumalai

unread,
Dec 5, 2014, 7:19:09 AM12/5/14
to motec...@googlegroups.com
Ok. I tried that before and saw servlet exceptions from bootstrap and a FlashMap type mismatch during runtime. I'll try it again on a fresh deployment.

Thanks Sebastian.

Reply all
Reply to author
Forward
0 new messages