In this tutorial, I'll talk about a different concept called JMX Embedded. So by this time in this developer series, I have already explained most frequently used back-end concepts whether it's a sling model, scheduler, servlets, OSGI configurations or OSGI services. So I'll talk about today Embedded, JMX Embedded which is bit different and not as much frequently used as other explained concepts. but this gmx m beans used a lot for the aem internal implementation whole health check framework whatever the framework you see we have a specific dashboard in aem for health checks that whole framework is written using m beans and there are other things as well written using m beans i'll explain that so we have a specific console in aem for gmx m beans so first of all you must understand mBeans or JMX mBeans are not specific to AEM.
These are the open source framework provided by Java that Java management extensions JMX framework and that framework was introduced used since Java 1.5. So in generic way how you write JMX mBean same way you can write in AEM but obviously because we use OSGI framework as well so we'll writing that using those annotations as we wrote other backend modules right there is a specific console in am specifically for the m beans you can see your m beans there and you can execute code so as i said the standard way osgi way you have to use annotations like you use others along with some specific annotations we are only and only used for the jmx m beans so how it is different from other backend modules so hey using this m means you can execute your code at runtime you can provide some configuration at the runtime means you do not need means as a being a admin or the if you have access to that console you can execute your functions or methods straight away without without any other help okay we will see that so for this tutorial i'll take a different approach first i will explain how to write that m bean and we'll have a demo post demo after demo i'll explain the other concept and we'll compare how it is different and how it is useful for sometimes it can be really useful for a client projects so for ambience also we'll use a standard way how to write that we will have a interface and one implementation class so let's say i create an interface called geeks mbin okay geeks.mbin i'm not writing any method as of now we'll see how to write that further there is the implementation class of that interface so you have to write a an at the rate component annotation which is mandatory for any at the back end module other than the slang model as this is the same way old story immediate true obviously service is equal to dynamic mbin so this is this is needed you define service as you define for the servlet you define a service and this way you define this service okay this is provided by your management means jmx itself provided by java all right important thing is this property you define what is your object name right jmx dot object name same thing you define even if you write it this ambient in a plain java so here where you define a different way because we are following the OSGI kind of annotation so we define in this way property jmx object name your package where you have this then colon type type is equal to the name this name this name would be available in your console jmx console when you start searching it then you extend this class annotation standard bean because this class provides some methods to you so that way you have to use it And this class implement this interface as well. Implementing your own interface.
Now, obviously this line, line number 21 is just to add logger, simple way, nothing specific. Here, you write a constructor for implementation class. This is mandatory.
This way, using this constructor, you can call your super method, super constructors, okay? So inside this, you call super by passing your interface, means your mbin, right? throw not compile this mbin exception when this exception come when if your mbin does not follow the pattern design pattern provided by gmx okay means the standard pattern if you don't follow then you can have this exceptions that's why i'm just throwing it i'm not writing this in you know try catch so i'm just throwing it obviously after this code your mbin is ready when you deploy you will be see a mbin but right now i did not define anything custom what I want from this MB.
Now you have to define methods. So what methods you want to execute. So let me define a method here.
First you define the method. This is a standard approach. You define a method here. in this interface and then implementation class so here i wrote a simple method intention purpose of this tutorial is to explain how to write mb not what to write inside mb so we have a very simple method called get author name so what we'll do we'll provide a name to the author and that will be you know return back it's a string type so basically intention here is how you see you can execute a method by console inside this implementation class i just pass a method and return the same name okay pass a name and return a same name along with i have a log statement so that you can see this has been executed all right that's the simple one here you can have any number of methods you can perform any number of tasks as per your need but here we'll see just a simple use case now let me deploy this as soon as you deploy in your console you will see a mbin with name geeks mbin i'll show you let it let this deployment finish so if you see once deployment is done there is a console available system system console slash gmx if you hit that you will see the console which contain all the ambience you have in your aem so you will see the latest one which we defined called type is equal to geeks ambience and then the same thing we define inside if you hear the type okay this type you Alright, so now if you open this, just click on this and you will see the methods which you define.
Obviously, this should be the method which has accessible to it. It should not be private. Now, you will see here. Now, if you have access to this console, you can run these methods. Click on this.
Whatever the input of your method is, it will give you a box or text box to add input. Let me add something. AEM and Geeks.
all right before I execute using this invoke if this method will invoke and because it returned the same thing you will see the response here along with a statement inside a log so let me open that log first before execute this alright now let me invoke as soon as I invoke you see the response because it's a string type so it returned me the string if I change this let's say this one so it again so now in logs I should see two statements because this is executed twice so if I open this log obviously you will see this it's called and again it's called so that means the method executed and it returns something now the question is when it should be used as I said whole health check framework is written using this go to the dashboard health check dashboard and you will see lot of lot of the functionality which is written in M beans even if you go back and if you see you will see this health check things lot of health check uh hc health check you will see start from here health check lot of things are written using health check and other as well i'll show you one more example if you have any time upgraded your am instance from older versions to 6.5 so there is a mb in which actually check your instance is ready to upgrade or not there's some specific task you perform let me find it it's there you see this pre-upgrade task so when you execute these methods so it will check whether you made ready instance to upgrade or not so this kind of stuff functionality is as executed there yeah you know written using these okay so there are others as well now how it is different from others so first of all you are executing your code at runtime or you are providing some information at runtime not only executing code you can provide some configuration you can provide some other things to am at the runtime on your own choice you do not depend on other let's say you do not depending on sublet call or page page load or something it's up to you when you want to execute that code second thing you must have a question how it is different from OSGI configuration because OSGI configuration also you provide the inputs but that is different you just provide input but you don't have control when to execute that code and the basic fundamental difference whenever you update something in your OSGI configuration your configuration always re-registered but when you call mbean nothing re-registered okay whenever you hit save button in OSGI configuration you will see in logs your service or your configuration registered again unregistered and registered but that's not the case with the mbean one more thing you can call this mbeans in your other modules as well I'll explain sometime later that, but you can definitely call this. You can call the mBeans in some third party. JConsole is the most famous or most commonly used application, third party application where you can call mBeans.
So that way you can even the information, you can share the information using mBeans as well. How you share using servlets. So sometimes consider that as well whenever you design some solution. So I hope you understood. If you have any questions.
please add your comment thank you