如果使用jmx,如果jvm中的内存消耗超过某个阈值,我如何生成邮件警报/通知 [英] how can i generate mail alert/notification if memory consumption in jvm exceeds some thrsold, by using jmx

查看:146
本文介绍了如果使用jmx,如果jvm中的内存消耗超过某个阈值,我如何生成邮件警报/通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何针对jvm中的以下更改生成邮件警报

how to generate mail alerts for the following changes in jvm

1)内存 2)线程 3)db连接

1)memory 2) threads 3)db connections

使用jmx

任何帮助都是有用的

谢谢

推荐答案

我将回答您的问题:内存.要在给定的内存池超过阈值的任何时间获得通知,请执行以下操作.要获取所有内存池的列表,请调用ManagementFactory.getMemoryPoolMXBeans().然后选择对您重要的(一个).我的一台机器上,列表是:

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:

Code Cache
Eden Space
Survivor Space
Tenured Gen
Perm Gen
Perm Gen [shared-ro]
Perm Gen [shared-rw]

但是我认为对此没有故意的标准,它们在版本和实现之间有所不同.因此,首先获取您感兴趣的游泳池:

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:

MemoryPoolMXBean memPool = ...; // Get a MemoryPoolMXBean

然后,检查是否允许使用阈值.如果是这样,请设置一个(以字节为单位)

Then, check if a usage threshold is allowed. If so, set one (in bytes)

if(memPool.isUsageThresholdSupported())
{
  memPool.setUsageThreshold(5000000);
}

然后,当超过阈值时,通过将NotificationListener传递给MemoryMXBean的addNotificationListener来请求通知(实际上,您必须先将其强制转换为NotificationEmitter).在示例中,NotificationListener是一个匿名内部类,但是只要实现javax.management.NotificationListener接口,它就可以是您想要的任何类.基本上,我下面要做的是将愚蠢的消息打印到stdout/stderr,具体取决于所使用的内存量.当然,您可以加入JavaMail或第三方邮件框架来发送邮件.

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.

NotificationEmitter memBean = (NotificationEmitter)(ManagementFactory.getMemoryMXBean());
memBean.addNotificationListener(new NotificationListener()
{
  public void handleNotification(Notification n, Object handback)
  {
    CompositeData cd = (CompositeData)n.getUserData();
    MemoryNotificationInfo mni = MemoryNotificationInfo.from(cd);
    MemoryUsage memUsage = mni.getUsage();
    long bytesUsed = memUsage.getUsed();
    if(bytesUsed > 512000000)
      System.err.println("Oh, no, we're using more than 512M!");
    else
      System.out.println("It's okay.  We're only using " + bytesUsed + " bytes.");
  }
}

就线程而言,相关的bean显然是ThreadMXBean,它是从ManagementFactory.getThreadMXBean()获得的.但这似乎没有内置的方法来设置阈值或添加侦听器,因此您必须进行轮询.您可以仅从ManagementFactory.getThreadMXBean().getAllThreadIds().length获取线程计数,当然,在bean中还有更多可用信息.

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.

最后,数据库连接"只是模糊的.您正在使用什么数据库系统?问题JVM是服务器还是客户端等?

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.

这篇关于如果使用jmx,如果jvm中的内存消耗超过某个阈值,我如何生成邮件警报/通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆