JMX Notification

本文介绍了Java Management Extensions (JMX)中Notification机制的基本概念及其组成部分,并通过一个HelloWorld示例详细展示了如何利用Notification实现MBeans间的通信。
 

JMX Notification 收藏

 

Mbean 之间的通信是必不可少, Notification 就起到了 Mbean 之间沟通桥梁的作用。 JMX notification model java event model 类似,将一些重要的信息,状态的转变,数据的变更传递给 notification listener 。以使资源更容易管理。

 

JMX notification 由四部分组成:

1、  Notification

2、  Notification broadcaster

3、  Notification listerner

4、  Notification filter

 

 

通过简单的 HelloWorld 例子来说明 Notification 的实现过程吧。

 

HelloWorldMBean.java

 

package notification;

 

public interface HelloWorldMBean {

    public void setGreeting(String greeting);

    public String getGreeting();

    public void printGreeting();

}

 

 

HelloWorld.java

 

/*

  * Created on 2004-12-27

  * @author roson

  */

package notification;

 

import java.io.*;

import javax.management.*;

public class HelloWorld extends NotificationBroadcasterSupport

                                          implements HelloWorldMBean

{

    private String greeting;

    public HelloWorld()

    {

        this.greeting = "Hello World! I am a Standard MBean";

    }

 

    public HelloWorld( String greeting )

    {

        this.greeting = greeting;

    }

    public void setGreeting( String greeting )

    {   // 更改属性这里增加了 notification 只要更改属性就可以看到。

         this.greeting = greeting;

        Notification notification = new Notification(

                "jmxbook.ch2.helloWorld.test"

                this,                                      -1,

                System.currentTimeMillis(),        

                greeting);

                      

        sendNotification( notification );

    }

 

    public String getGreeting()

    {

        return greeting;

    }

 

    public void printGreeting()

    {

        System.out.println( greeting );

    }

   

}

 

 

Notification 的结构体一开始是觉的比较麻烦的,不知道各个参数代表什么意思。可以看下 sun 提供的关于 jmx api , 这里看下 notification 的结构体

1、  Notification ( String  type, Object  source, long sequenceNumber)

2、  Notification ( String  type, Object  source,long sequenceNumber,l ong timeStamp)

3、  Notification ( String  type, Object  source,long sequenceNumber,long timeStamp, String  message)

4、 Notification ( String  type, Object  source,long sequenceNumber, String  message)

 

type 用来标示通知, source 为产生通知的 MBean sequenceNumber 为一系列通知中的序号, timeStamp 为通知创建的时间, message 为具体的通知消息。

 

HelloAgent.java

 

/*

  * Created on 2004-12-27

  * @author roson

  */

package notification;

 

import javax.management.*;

import com.sun.jdmk.comm.HtmlAdaptorServer;

 

public class HelloAgent implements NotificationListener

{

    private MBeanServer mbs = null;

 

    public HelloAgent ( )

    {

        mbs = MBeanServerFactory.createMBeanServer("HelloWorld Server");

        HtmlAdaptorServer adapter = new HtmlAdaptorServer();

        HelloWorld hw = new HelloWorld();

 

        ObjectName adapterName = null;

        ObjectName helloWorldName = null;

 

        try

        {

            adapter.setPort(9092);   //8082 是默认的端口可以不设

            adapterName = new ObjectName("HelloAgent:name=htmladapter,port=9092" );

            mbs.registerMBean(adapter,adapterName );

           

            helloWorldName = new ObjectName("HelloAgent:name=helloWorld1" );

            mbs.registerMBean( hw, helloWorldName );

 

            hw.addNotificationListener( this, null, null );

 

        }

        catch( Exception e )

        {

            e.printStackTrace();

        }

        adapter.start();

    }//constructor

 

    public void handleNotification(Notification notif, Object handback )

{

       // 可以在这里顺便看下 notification 结构体的参数。

        System.out.println( "Receiving notification..." );

        System.out.println( notif.getType());

         System.out.println(notif.getSource());

        System.out.println( notif.getMessage());

    }

 

    public static void main( String args[] )

    {

        System.out.println("Start agent");

        HelloAgent agent = new HelloAgent();

    }

}

 

 

好了可以运行了,打开浏览器 输入 http://localhost:9092 然后在注册 Mbean 的地方找到

 

List of registered MBeans by domain:

 

然后找到 Mbean 的属性

 

List of MBean attributes:

Name

Type

Access

Value

Greeting

java.lang.String

RW

 

 

更改 Value 值,按 Apply ,搞定(如果下一个页面显示成功)。看下 Console 输出什么呢。
顺便说下 Access的值 有三个 RW (Read Write) ,RO(ReadOnly) ,WO(WriteOnly)

内容概要:本研究聚焦于“绿电直连型电氢氨园区”的优化运行,提出一种直接利用绿色电力驱动制氢与合成氨的综合能源系统架构。通过构建包含风/光发电、电解水制氢、氢气储存、合成氨反应及电能直供等关键环节的系统模型,研究旨在实现能源的高效转化与梯级利用,降低对外部电网依赖,提升园区能源自洽率与经济性。研究综合运用Matlab与Python工具进行建模与仿真,结合实际气象与负荷数据,对系统在不同工况下的运行策略、能量流动、设备容量配置及经济技术指标进行深入分析与优化,并形成完整的Word论文文档,为新型零碳产业园区的规划与建设提供了理论依据和技术支撑。; 适合人群:具备新能源、电力系统、化工或综合能源系统背景的科研人员,以及从事园区规划、能源管理、低碳技术开发的工程技术人员。; 使用场景及目标:①研究绿电如何高效耦合至化工生产流程,实现“电-氢-氨”多能互补;②掌握综合能源系统(IES)的建模、仿真与优化方法,特别是多时间尺度下的运行调度策略;③为撰写高水平学术论文或完成相关课题研究积累数据、代码与写作模板。; 阅读建议:此资源包含代码、数据和完整论文,建议使用者先通读Word论文以理解整体框架与理论基础,再结合Matlab/Python代码进行复现与调试,最后可基于提供的数据和模型进行二次开发,以深化对绿电综合利用技术的理解。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值