使用 Spring @ManagedNotification 注解生成 JMX 通知的任何示例

发布于 2024-12-05 05:51:03 字数 108 浏览 1 评论 0原文

我环顾四周,没有找到任何使用 spring 注解生成 JMX 通知的示例。我找到了使用 @ManagedAttribute 和 @ManagedOperation 的示例。

谢谢 -账单

I've been looking around and haven't found any examples of using spring annotations to generate JMX notifications. I have found examples using @ManagedAttribute and @ManagedOperation.

Thanks
-Bill

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

披肩女神 2024-12-12 05:51:03

开始吧:

import java.util.concurrent.atomic.AtomicLong;

import javax.management.Notification;

import org.springframework.jmx.export.annotation.ManagedOperation;
import org.springframework.jmx.export.annotation.ManagedResource;
import org.springframework.jmx.export.notification.NotificationPublisher;
import org.springframework.jmx.export.notification.NotificationPublisherAware;

@ManagedResource
public class JMXDemo implements NotificationPublisherAware {
    private final AtomicLong notificationSequence = new AtomicLong();
    private NotificationPublisher notificationPublisher;

    @Override
    public void setNotificationPublisher(
            final NotificationPublisher notificationPublisher) {
        this.notificationPublisher = notificationPublisher;
    }

    @ManagedOperation
    public void trigger() {
        if (notificationPublisher != null) {
            final Notification notification = new Notification("type",
                    getClass().getName(),
                    notificationSequence.getAndIncrement(), "The message");
            notificationPublisher.sendNotification(notification);
        }
    }
}

在 Spring 配置文件中,您必须使用如下内容:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
    ">

    <context:mbean-server id="mbeanServer" />

    <context:mbean-export server="mbeanServer" />

    <bean class="org.springframework.jmx.export.MBeanExporter">
        <property name="server" ref="mbeanServer" />
        <property name="namingStrategy">
            <bean id="namingStrategy"
                class="org.springframework.jmx.export.naming.MetadataNamingStrategy">
                <property name="attributeSource">
                    <bean
                        class="org.springframework.jmx.export.annotation.AnnotationJmxAttributeSource" />
                </property>
            </bean>
        </property>
    </bean>

</beans>

然后您可以使用 JConsole 访问上述 bean,并使用 trigger() 操作触发通知。请务必订阅通知。 :)

Here you go:

import java.util.concurrent.atomic.AtomicLong;

import javax.management.Notification;

import org.springframework.jmx.export.annotation.ManagedOperation;
import org.springframework.jmx.export.annotation.ManagedResource;
import org.springframework.jmx.export.notification.NotificationPublisher;
import org.springframework.jmx.export.notification.NotificationPublisherAware;

@ManagedResource
public class JMXDemo implements NotificationPublisherAware {
    private final AtomicLong notificationSequence = new AtomicLong();
    private NotificationPublisher notificationPublisher;

    @Override
    public void setNotificationPublisher(
            final NotificationPublisher notificationPublisher) {
        this.notificationPublisher = notificationPublisher;
    }

    @ManagedOperation
    public void trigger() {
        if (notificationPublisher != null) {
            final Notification notification = new Notification("type",
                    getClass().getName(),
                    notificationSequence.getAndIncrement(), "The message");
            notificationPublisher.sendNotification(notification);
        }
    }
}

And in your Spring configuration file you must use something like this:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
    ">

    <context:mbean-server id="mbeanServer" />

    <context:mbean-export server="mbeanServer" />

    <bean class="org.springframework.jmx.export.MBeanExporter">
        <property name="server" ref="mbeanServer" />
        <property name="namingStrategy">
            <bean id="namingStrategy"
                class="org.springframework.jmx.export.naming.MetadataNamingStrategy">
                <property name="attributeSource">
                    <bean
                        class="org.springframework.jmx.export.annotation.AnnotationJmxAttributeSource" />
                </property>
            </bean>
        </property>
    </bean>

</beans>

You can then access above bean with JConsole and trigger notifications with the operation trigger(). Be sure to subscribe to the notifications. :)

妄司 2024-12-12 05:51:03

您还可以使用 @ManagedNotifications 注释将通知详细信息(通知元数据)添加到公开的 JMX MBean。

在 JMXDemo 类上应用以下注释以及 @ManagedResource 注释

@ManagedNotifications({ @ManagedNotification(name = "javax.management.Notification", notificationTypes = { "notification type" }, description = "notification description") })

以上详细信息将在 JConsole 通知详细信息选项下显示通知详细信息。

You can add notification details (Notification MetaData) to the exposed JMX MBean as well using @ManagedNotifications annotation.

Apply bellow annotation on Class JMXDemo along with @ManagedResource annotation

@ManagedNotifications({ @ManagedNotification(name = "javax.management.Notification", notificationTypes = { "notification type" }, description = "notification description") })

Above details will show the notification details under JConsole Notification details option.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文