【问题标题】:Any examples of using Spring @ManagedNotification annotation to generate JMX notfications使用 Spring @ManagedNotification 注解生成 JMX 通知的任何示例
【发布时间】:2011-09-16 14:12:38
【问题描述】:

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

谢谢 -比尔

【问题讨论】:

    标签: java spring annotations jmx


    【解决方案1】:

    给你:

    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() 触发通知。请务必订阅通知。 :)

    【讨论】:

    • 感谢您的回复。如果可以的话,我会给你更多的选票。我很难找到这个问题的答案,而且我认为 spring/JMX 组合非常强大。
    • 它确实非常强大,因为它可以让您在运行时调整应用程序的细节(如查询策略)。我在我创建的每个服务器应用程序中都使用它。
    • 这是一个非常简单有效的例子!谢谢!顺便说一句,bean JMXDemo 是否必须由 Spring 创建和管理? Spring 如何知道在这个实例中扫描哪个类/包来查找 JMX 注释?
    • 在我的示例中,将扫描每个 Spring 托管 bean 以查找注释 @ManagedResource 并导出(如果存在)。有关如何控制 JMX 导出的更多信息,请参阅static.springsource.org/spring/docs/current/…
    【解决方案2】:

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

    在 Class JMXDemo 上应用下面的注释以及 @ManagedResource 注释

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

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

    【讨论】:

      猜你喜欢
      • 2017-06-24
      • 1970-01-01
      • 2015-04-03
      • 1970-01-01
      • 1970-01-01
      • 2018-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多