Being spent some part of my past life developing dashboard tools for monitoring the applications in a large enterprise, I am always interested in exposing system health and metrics to external tools for monitoring. So, naturally when Java 1.5 exposed JMX beans for JVM, I was very excited. However, in my current work, we never exposed health of the system through JMX, we had our custom ways to do things and custom monitors for it.
With the latest release coming up, now that we are finally using 1.6 jre, I enabled the in-built support for JMX using these instructions. I did not think much about how JMX client is going to connect to the VM, until today my fellow developer asked, "Ramesh what is the URL for the JMX port on the VM?"
Well, I did not knew the answer, I always used either JConsole or VisualVM to connect and did not pay attention. Looking through the instructions, I see that one could expose the JMX port, so that you can connect remotely from another machine or local machine, however I did not find any default URL I could find to connect to locally to a JVM's JMX port. I did not wanted open a specific port, because once I do that, I would need to provide security mechanism tie it down, more over the agent that is polling for the stats is supposed to live on same box.
To handle the security, I could expose the port on the VM and setup a private SSL certificate between local agent and Server, but I knew some how JConsole connects to the local processes, so started investigate how it does it, then I came across "Attach API"
Attch API, is used by debuggers and profilers, to connect to a VM and inject agents into the VM and expose inner workings of the VM. Here, I just needed to find the URL for local JMX connection, then I came across this blog, which showed how to use the Attcah API to find the JMX URL, now we can dynamically resolve the JMX URL to the local VM. Problem Solved!
Showing posts with label java. Show all posts
Showing posts with label java. Show all posts
Thursday, March 5, 2009
Monday, November 17, 2008
Which Class Loader Should I use?
Every Java developer knows thing or two about class loaders in the Java. I am sure there are plenty of articles out there to tell you more about "What class loaders are?" and their significance in the Java language. In this article I would like to go over various different ways you can get hands on a class loader, and which method I use and when and why?
There are two elements you need to know before you can load a class or resource, in a Java application
1) Name of the resource.
2) Location of the resource.
Depending upon the application, developer may or may not know above settings at design time of an application. Below we will discuss, which technique to use depending upon the situation
For example purpose let's assume that there is interface "Foo" and it has corresponding implementation class "FooImpl"
If you know the name and location, then it is pretty simple.
If you do not know the name, however if you *know* that resource will be reachable from the same class loader as the executing class, then you can code as below
or
If you do not know both the name and location of resource, then it becomes little complicated. Usually this occurs when you are using some framework code, and writing some extensions and you do not have control over how your resources are being loaded (for example in application server) then the above techniques work some times, but below should be safer option
When the executing thread gets created it will be assigned class loader of the executing class, however user could be set to any other class loader at any point depending upon needs giving the most flexibility.
There are two elements you need to know before you can load a class or resource, in a Java application
1) Name of the resource.
2) Location of the resource.
Depending upon the application, developer may or may not know above settings at design time of an application. Below we will discuss, which technique to use depending upon the situation
For example purpose let's assume that there is interface "Foo" and it has corresponding implementation class "FooImpl"
If you know the name and location, then it is pretty simple.
Foo foo = new FooImpl();
If you do not know the name, however if you *know* that resource will be reachable from the same class loader as the executing class, then you can code as below
Class fooClazz = this.getClass().getClassLoader().loadClass("FooImpl");
Foo foo = fooClazz.newInstance(); // assuming there is default constructor
or
Class fooClazz = Class.forName("FooImpl");
Foo foo = fooClazz.newInstance();
If you do not know both the name and location of resource, then it becomes little complicated. Usually this occurs when you are using some framework code, and writing some extensions and you do not have control over how your resources are being loaded (for example in application server) then the above techniques work some times, but below should be safer option
Class fooClazz = Thread.currentThread().getContextClassLoader().loadclass("FooImpl");
Foo foo = fooClazz.newInstance();
When the executing thread gets created it will be assigned class loader of the executing class, however user could be set to any other class loader at any point depending upon needs giving the most flexibility.
Wednesday, October 8, 2008
Where it all started!
I have been wanting to enter blogosphere for little while now to keep in touch with my friends and colleagues but never took the initiative until now.
It all started when I was watching the 2008 vice-presidential debate on CNN last week, and I heard Alaska Gov. Sarah Palin calling all "Joe Six Packs" and hockey moms in the nation to come together in saying "never again" to those predatory lenders who is responsible for mortgage meltdown.
That got me thinking as to who is this Joe Six Pack? Am I a Joe Six Pack? Then after couple days light came on in my head, Yes of course I am Joey, but actually then I may be a "Java Six Pack" based on my career so far. Then I started listing what makes my six pack?
there I came about name "Java Six Pack". I thought, hey I may not be intelligent about any of these technologies but now and then I do come across some questions, interesting uses and use cases about all the above time to time, that thought inspired me to start this blog!
So, what is your Six Pack? Please share.
It all started when I was watching the 2008 vice-presidential debate on CNN last week, and I heard Alaska Gov. Sarah Palin calling all "Joe Six Packs" and hockey moms in the nation to come together in saying "never again" to those predatory lenders who is responsible for mortgage meltdown.
That got me thinking as to who is this Joe Six Pack? Am I a Joe Six Pack? Then after couple days light came on in my head, Yes of course I am Joey, but actually then I may be a "Java Six Pack" based on my career so far. Then I started listing what makes my six pack?
there I came about name "Java Six Pack". I thought, hey I may not be intelligent about any of these technologies but now and then I do come across some questions, interesting uses and use cases about all the above time to time, that thought inspired me to start this blog!
So, what is your Six Pack? Please share.
Subscribe to:
Posts (Atom)