how to generate mail alerts for the following changes in jvm
1)memory 2) threads 3)db connections
by using jmx
any help would be useful
I'll answer your question re: memory. To get a notification any time a given memory pool exceeds a threshold, do something like the below. To get a list of all the memory pools, call ManagementFactory.getMemoryPoolMXBeans(). Then choose the (ones) that matter for you. One my machine, the list is:
but I think there is deliberately no standard on this and they vary between versions and implementations. So first get a pool you're interested in:
Then, check if a usage threshold is allowed. If so, set one (in bytes)
Then, request notifications when the threshhold is exceeded, by passing a NotificationListener to the addNotificationListener of a MemoryMXBean (actually, you have to cast it to NotificationEmitter first). In the example, the NotificationListener is an anonymous inner class, but it can be whatever you want, as long as it implements the javax.management.NotificationListener interface. Basically, what I've done below is print stupid messages to stdout/stderr, depending on how much memory is being used. Of course, you can hook into JavaMail or a third-party mail framework to send a mail instead.
As far as threads, the relevant bean is obviously ThreadMXBean, which you get from ManagementFactory.getThreadMXBean(). But it doesn't look like there's a built-in way to set a threshhold or add listeners, so you'll have to poll. You can get thread count from just ManagementFactory.getThreadMXBean().getAllThreadIds().length , and of course, there's more information available in the bean.
Finally, "db connections" is just vague. What database system are you using? Is the JVM in question the server or client, etc.
I recommend you ask a new question if you need more info on the latter two resources.
+1 for Matthews answer
But if any Java 7.40 + users stumble across this there is a UI oriented solution.
We could make use of the Java Mission Control(JMC) to generate these alerts.
JMC is packaged in Java distribution now, so just invoke JMC and connect to the JMX remote port (if this JVM is local, you should already be connected when you invoke JMC).
On Initial screen(MBeanServer) you should see Triggers tab at the bottom , click on it here you should be able to set up email triggers for all the 3 conditions above - in fact you could do much more than just sending an email.