One fine morning. While zipping coffee on the couch, I made up my design. It is going to be a service that reads configuration data saved using Active Objects. Easy peasy. So I thought! I wrote my service faster than I planned, looking at my own tutorial, compiled it and everything looked fine. And then I started testing it using atlas-run. (Suspense music ♪♫♫). Boom..Null pointer Exception where the injected ActiveObjects component is used. Surely, ActiveObjects in not injected properly in the constructor of a service. Now, it makes sense because the service is probably initialized before the Active Objects plugin is loaded. Backup plan (Plan B) - Get the ActiveObjects object suing ComponentManager. ActiveObjects ao = ComponentManager.getOSGiComponentInstanceOfType(ActiveObjects.class); In the run method, I initialized ActiveObjects for the first time as mentioned above and yes! I got a valid object. But then came across this error: atlassian.activeobjects.osgi.ActiveObjectsServiceFactory] Could not find any active objects configurations for bundle org.apache.felix.framework. Did you define an 'ao' module descriptor in your plugin? Try adding this in your atlassian-plugin.xml file: <ao key='some-key' /> And this, in spite of having a valid definition in the atlassian-plugin.xml. Surely, something is not right. Backup plan (Plan C) - Use the powers of Google, when yours is not working. Google, now effectively crawling answers.atlassian.com, brought me a similar thread (hail Google!) where Stefan Chapuis stumbled up on a similar issue for mail handlers. And Wojciech Seliga had a brilliant workaround based on the reasoning - ActiveObjects can only be injected. The workaround is pretty simple:
Hope this helps someone stumbling up on the same/similar issue. And if you know a better trick, or if it is working for you in some other way, please do comment.
20 Comments
9/28/2012 04:36:29 pm
Thanks for the useful reference.
Reply
J-Tricks
9/29/2012 01:48:39 am
Thanks Sim. That should work. If you have an example, please do share :)
Reply
7/10/2013 11:20:25 pm
Such an article really helps us to start a service that can read configuration data. It can make us write faster than we have planned. This trick can really help anyone stumbling upon the similar issue. I hope you will continue your efforts by posting more of such tricks in future. 10/28/2012 10:25:23 pm
Plan B will not work. The issue here is that there are multiple instances of Active Objects available throughout your product (be it JIRA, Confluence, etc.) instance.
Reply
J-Tricks
10/29/2012 01:48:55 am
Yup, Plan B won't work. If only it was that easy ;)
Reply
Fatma
5/3/2013 02:46:35 am
teşekkürler
Reply
arthur
2/13/2014 09:03:20 pm
Hahaha! Very creative XD Plan C is beautiful
Reply
J-Tricks
2/14/2014 01:02:32 am
Works 99% of the times ;)
Reply
J-Tricks
3/31/2014 05:38:08 am
Hi,
Reply
Tom Heideman
4/30/2015 02:35:51 pm
when you say in step 3, "Get an instance of the component in the service's run method using ComponentManager", what do you mean by "the service's run method"? Which method does this go in? is there literally a "run()" method that needs to be created in one of my classes?
J-Tricks
5/2/2015 06:05:11 am
Yes, there is a 'run' method. Check out http://www.j-tricks.com/tutorials/jira-service for more details.
Jordan
6/26/2014 10:04:33 pm
Hello i try to do this but i have an error :
Reply
J-Tricks
6/27/2014 01:03:10 am
Give the fully qualified name of the class (that implements InjectionAo) as an attribute in the component definition. Something like the following:
Reply
jordan
6/27/2014 01:37:46 am
Thanks a lot it works
Jira Plugin Developer
7/15/2014 01:50:01 am
What stands in the interface and where do you get it?
Reply
J-Tricks
7/15/2014 02:25:49 am
Just the empty method:
Reply
Pavel
2/1/2016 05:57:10 am
What am I doing wrong? I still giving NPE.
Reply
J-Tricks
2/15/2016 09:52:42 pm
If you are using Atlassian Spring Scanner, that could be the issue. See http://www.j-tricks.com/tutorials/atlassian-spring-scanner-and-nosuchbeandefinitionexception for more details.
Reply
Nitin Prashar
9/13/2016 09:29:54 am
Hi tried your solution and want JtrickPluginManager dependency in Servlet.
Reply
Your comment will be posted after it is approved.
Leave a Reply. |
AuthorJobin Kuruvilla - Works in Adaptavist as Head of DevOps Professional Services. Categories
All
Archives
October 2016
|