JMX的客户端和服务端(Spring配置的方式和编码的方式)

一、Spring配置的方式:
1、客户端:
[color=red]client.xml[/color]

<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
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-2.0.xsd">

<bean id="mbeanClient" class="org.springframework.jmx.support.MBeanServerConnectionFactoryBean">
<property name="serviceUrl" value="service:jmx:rmi://localhost/jndi/rmi://localhost:2012/springjmx"></property>
</bean>

</beans>



[color=red]SpringJmxClientMain [/color]

package client;

import javax.management.Attribute;
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringJmxClientMain {

/**
* @param args
*/
public static void main(String[] args) throws Exception {
ApplicationContext context = new ClassPathXmlApplicationContext(
"client.xml");
MBeanServerConnection client = (MBeanServerConnection) context
.getBean("mbeanClient");
ObjectName objName = new ObjectName("springjmx:name=TestBean");
String result = (String) client.invoke(objName,
"printMessage", new Object[] { "Mike" },
new String[] { "java.lang.String" });

System.out.println("Client: result "+result);
String orgMessage = (String) client.getAttribute(objName, "Message"); //Why Message is Pascal style?
System.out.println("Client: orgMessage "+orgMessage);
Thread.sleep(20000);
client.setAttribute(objName, new Attribute("Message","Welcome"));
result = (String) client.invoke(objName,
"printMessage", new Object[] { "Mike" },
new String[] { "java.lang.String" });
System.out.println("Client: result "+result);

// logback jmx configurator
ObjectName logName = new ObjectName("ch.qos.logback.classic:Name=GatewayLog,Type=ch.qos.logback.classic.jmx.JMXConfigurator");
Object loggerList = client.getAttribute(logName, "LoggerList");
System.out.println(loggerList.getClass());
System.out.println(loggerList);
}

}

2、服务端
[color=red]server.xml[/color]


<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
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-2.0.xsd">

<bean id="testBean" class="server.TestBean"></bean>

<bean id="assembler" class="org.springframework.jmx.export.assembler.InterfaceBasedMBeanInfoAssembler">
<property name="managedInterfaces">
<list>
<value>server.ITestBean</value>
</list>
</property>
</bean>

<bean id="exporter" class="org.springframework.jmx.export.MBeanExporter">
<property name="beans">
<map>
<entry key="springjmx:name=TestBean" value-ref="testBean"></entry>
</map>
</property>
<property name="assembler" ref="assembler"></property>
</bean>

<bean id="rmiReg" class="org.springframework.remoting.rmi.RmiRegistryFactoryBean" destroy-method="destroy">
<property name="alwaysCreate" value="true"></property><!-- without alwaysCreate may throw a ConnectionException -->
<property name="port" value="2012"></property>
</bean>

<bean class="org.springframework.jmx.support.ConnectorServerFactoryBean" depends-on="rmiReg">
<property name="serviceUrl" value="service:jmx:rmi://localhost/jndi/rmi://localhost:2012/springjmx"></property>
</bean>

</beans>


[color=red]SpringJmxServerMain [/color]

package server;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringJmxServerMain {

/**
* @param args
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
ApplicationContext context = new ClassPathXmlApplicationContext(
"server.xml");
TestBean bean = (TestBean) context.getBean("testBean");
System.out.println("Server started");
Thread.sleep(1000 * 1000);
}

}



package server;

public interface ITestBean {
public String getMessage();

public void setMessage(String message);

public String printMessage(String name);
}



package server;

public class TestBean implements ITestBean {
private String message = "Hello";

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

public String printMessage(String name) {
String str = message + " " + name;
System.out.println(str);
return str;
}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值