Mule ESB 学习笔记(17)Mule和JMX的整合

Agent的实现:

package com.easyway.esb.mule.mbean;

import org.mule.api.MuleException;
import org.mule.api.agent.Agent;
import org.mule.api.lifecycle.InitialisationException;

import java.util.Collections;
import java.util.List;

/**
必须继承自org.mule.api.agent.Agent接口.
 * Mock agent
 */
public class AutoTaskAgent implements Agent
{

    private String frobbit;

    public String getName()
    {
        return "Test Agent";
    }

    public void setName(String name)
    {
        // nothing to do
    }

    public String getDescription()
    {
        return "Test JMX Agent";
    }

    public void initialise() throws InitialisationException
    {
        // nothing to do
    }

    public void start() throws MuleException
    {
        // nothing to do
    }

    public void stop() throws MuleException
    {
        // nothing to do
    }

    public void dispose()
    {
        // nothing to do
    }

    public List<Class<? extends Agent>> getDependentAgents()
    {
        return Collections.emptyList();
    }

    public String getFrobbit()
    {
        return frobbit;
    }

    public void setFrobbit(String frobbit)
    {
        this.frobbit = frobbit;
    }
}

 

 

 

mule-mbean-config.xml的配置

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

    <management:jmx-server>
        <management:connector-server url="service:jmx:rmi:///jndi/rmi://localhost:9998/server"
          rebind="false" />
        <management:credentials>
            <spring:entry key="a" value="b"/>
            <spring:entry key="c" value="c"/>
        </management:credentials>
    </management:jmx-server>

    <management:jmx-log4j/>

    <management:jmx-mx4j-adaptor jmxAdaptorUrl="http://127.0.0.1:8000"/>

    <custom-agent name="test-custom-agent" class="com.easyway.esb.mule.mbean.AutoTaskAgent">
        <spring:property name="frobbit" value="woggle"/>
    </custom-agent>

    <management:jmx-notifications/>

    <management:log4j-notifications/>

    <management:chainsaw-notifications chainsawPort="8080" chainsawHost="127.0.0.1"/>

 </mule>

 

 

jmx测试代码:

public class MuleAgentMain {
    public static void main(String[] args) throws Exception, ConfigurationException {
        String configFile = "mule-mbean-config.xml";
        String[] configFileArr = new String[] {configFile };
        MuleContextFactory muleContextFactory = new DefaultMuleContextFactory();
        MuleContext muleContext = muleContextFactory
                .createMuleContext(new SpringXmlConfigurationBuilder(configFileArr));
        muleContext.start();
        
        Agent agent = muleContext.getRegistry().lookupObject(JmxAgent.class);
        System.out.println(agent.getClass());
        JmxAgent jmxAgent = (JmxAgent) agent;
        System.out.println(jmxAgent.isCreateServer());
        System.out.println(jmxAgent.isLocateServer());
        System.out.println(jmxAgent.isEnableStatistics());
        System.out.println(jmxAgent.getConnectorServerUrl());

        agent = muleContext.getRegistry().lookupAgent("jmx-log4j");
        System.out.println(agent.getClass());

        agent = muleContext.getRegistry().lookupAgent("jmx-mx4j-adaptor");
        System.out.println( agent.getClass());
        Mx4jAgent mx4jAgent = (Mx4jAgent) agent;
        System.out.println(mx4jAgent.getJmxAdaptorUrl());

        agent = muleContext.getRegistry().lookupAgent("jmx-notifications");
        System.out.println(agent.getClass());

        agent = muleContext.getRegistry().lookupAgent("log4j-notifications");
        System.out.println(agent.getClass());

        agent = muleContext.getRegistry().lookupAgent("chainsaw-notifications");
        System.out.println(agent.getClass());
        Log4jNotificationLoggerAgent lnlAgent = (Log4jNotificationLoggerAgent) agent;
        System.out.println(lnlAgent.getChainsawPort()+" "+lnlAgent.getChainsawHost());

        agent = muleContext.getRegistry().lookupAgent("test-custom-agent");
        AutoTaskAgent autoTaskAgent=(AutoTaskAgent) agent;
        System.out.println( autoTaskAgent.getFrobbit());
        autoTaskAgent.setFrobbit("aaa");
        agent = muleContext.getRegistry().lookupAgent("test-custom-agent");
        System.out.println(((AutoTaskAgent)agent).getFrobbit());
    }
}

 

你可能感兴趣的:(jmx,SOA,mule,ESB)