Skip to content

Commit

Permalink
springside#10 使用Jolokia 将JMX数据输出成Restful JSON数据, JMX重获新生。
Browse files Browse the repository at this point in the history
  • Loading branch information
calvin1978 committed Apr 11, 2012
1 parent 9856086 commit bc0bb68
Show file tree
Hide file tree
Showing 20 changed files with 174 additions and 259 deletions.
2 changes: 1 addition & 1 deletion examples/mini-web/src/main/webapp/WEB-INF/web.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
<url-pattern>/*</url-pattern>
</filter-mapping>

<!-- Sitemesh filter -->
<!-- SiteMesh Web-Page Layout filter-->
<filter>
<filter-name>sitemeshFilter</filter-name>
<filter-class>com.opensymphony.sitemesh.webapp.SiteMeshFilter</filter-class>
Expand Down
6 changes: 6 additions & 0 deletions examples/showcase/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,12 @@
<artifactId>poi</artifactId>
</dependency>

<!-- jolokia Restful JMX -->
<dependency>
<groupId>org.jolokia</groupId>
<artifactId>jolokia-core</artifactId>
</dependency>

<!-- email -->
<dependency>
<groupId>javax.mail</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.springside.examples.showcase.common.dao.UserMyBatisDao;
import org.springside.examples.showcase.common.entity.User;
import org.springside.examples.showcase.jms.simple.NotifyMessageProducer;
import org.springside.examples.showcase.jmx.ApplicationStatistics;
import org.springside.examples.showcase.security.ShiroDbRealm;
import org.springside.examples.showcase.security.ShiroDbRealm.HashPassword;
import org.springside.modules.mapper.JsonMapper;
Expand Down Expand Up @@ -43,6 +44,8 @@ public class AccountManager {

private ShiroDbRealm shiroRealm;

private ApplicationStatistics applicationStatistics;

/**
* 在保存用户时,发送用户修改通知消息, 由消息接收者异步进行较为耗时的通知邮件发送.
*
Expand Down Expand Up @@ -70,10 +73,19 @@ public void saveUser(User user) {
shiroRealm.clearCachedAuthorizationInfo(user.getLoginName());
}

if (applicationStatistics != null) {
applicationStatistics.incrUpdateUserTimes();
}

sendNotifyMessage(user);

}

public List<User> getAllUser() {

if (applicationStatistics != null) {
applicationStatistics.incrUpdateUserTimes();
}
return (List<User>) userJpaDao.findAll();
}

Expand Down Expand Up @@ -193,4 +205,9 @@ public void setMemcachedClient(SpyMemcachedClient memcachedClient) {
public void setShiroRealm(ShiroDbRealm shiroRealm) {
this.shiroRealm = shiroRealm;
}

@Autowired(required = false)
public void setApplicationStatistics(ApplicationStatistics applicationStatistics) {
this.applicationStatistics = applicationStatistics;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package org.springside.examples.showcase.jmx;

import java.util.concurrent.atomic.AtomicInteger;

import org.springframework.jmx.export.annotation.ManagedAttribute;
import org.springframework.jmx.export.annotation.ManagedOperation;
import org.springframework.jmx.export.annotation.ManagedResource;

@ManagedResource(objectName = ApplicationStatistics.MBEAN_NAME, description = "Application Statistics Management Bean")
public class ApplicationStatistics {

public static final String MBEAN_NAME = "showcase:name=ApplicationStatistics";

private AtomicInteger listUserTimes = new AtomicInteger();
private AtomicInteger updateUserTimes = new AtomicInteger();

public void incrListUserTimes() {
listUserTimes.incrementAndGet();
}

public void incrUpdateUserTimes() {
updateUserTimes.incrementAndGet();
}

@ManagedAttribute(description = "Times of all users be listed")
public int getListUserTimes() {
return listUserTimes.get();
}

@ManagedAttribute(description = "Times of users be updated")
public int getUpdateUserTimes() {
return updateUserTimes.get();
}

@ManagedOperation(description = "Reset all statistics")
public void resetStatistics() {
listUserTimes.set(0);
updateUserTimes.set(0);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
<import resource="jms/applicationContext-jms-simple.xml" />
<import resource="jms/applicationContext-jms-advanced.xml" />
<import resource="jmx/applicationContext-jmx.xml" />
<import resource="log/applicationContext-log.xml" />
<import resource="schedule/applicationContext-jdk-timer.xml" />
<import resource="schedule/applicationContext-spring-cron.xml" />
<import resource="schedule/applicationContext-quartz-cron-local.xml" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@
<description>JMX服务端配置</description>

<!-- 配置MBean自动注册 -->
<context:mbean-export default-domain="Showcase" registration="replaceExisting" />
<context:mbean-export default-domain="showcase" registration="replaceExisting" />

<!-- Log4j控制 MBean -->
<bean id="log4jMBean" class="org.springside.modules.log.Log4jMBean" />
<bean class="org.springside.modules.log.Log4jManager">
<property name="projectLoggerName" value="org.springside.examples.showcase"/>
</bean>

<!-- 应用使用情况統計MBean -->
<bean class="org.springside.examples.showcase.jmx.ApplicationStatistics"/>

<!-- (可选)配置RMI注册服务器,可在命令行用-D定义实现 -->
<bean id="rmiRegistry" class="org.springframework.remoting.rmi.RmiRegistryFactoryBean" lazy-init="false">
Expand Down

This file was deleted.

44 changes: 0 additions & 44 deletions examples/showcase/src/main/resources/log4j-perf4j.xml

This file was deleted.

12 changes: 2 additions & 10 deletions examples/showcase/src/main/resources/log4j.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ log4j.rootLogger=WARN, Console, RollingFile

#Console Appender
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.Threshold=INFO
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p %-40.40c -%m%n

Expand All @@ -17,18 +16,11 @@ log4j.appender.RollingFile.File.DatePattern=.yyyy-MM-dd-HH
log4j.appender.RollingFile.layout=org.apache.log4j.PatternLayout
log4j.appender.RollingFile.layout.ConversionPattern=%d [%t] %-5p %-40.40c -%m%n

#TraceFile Appender (Store debug message, rolling with 10M, 5 files, threshold is DEBUG)
log4j.appender.TraceFile=org.apache.log4j.RollingFileAppender
log4j.appender.TraceFile.File=logs/showcase_trace.log
log4j.appender.TraceFile.Threshold=OFF
log4j.appender.TraceFile.MaxBackupIndex=5
log4j.appender.TraceFile.layout=org.apache.log4j.PatternLayout
log4j.appender.TraceFile.layout.ConversionPattern=%d %X{traceId} [%-40.40c] -%m%n

##Loggers##

#Project defalult level with TraceFile appender
log4j.logger.org.springside.examples.showcase=INFO,TraceFile
#Project defalult level
log4j.logger.org.springside.examples.showcase=INFO

#log4jdbc
log4j.logger.jdbc.sqltiming=INFO
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
<body>
<h2>Cache演示</h2>

<h4>技术说明:</h4>
<h3>技术说明:</h3>
<ul>
<li>演示Memcached的使用</li>
<li>演示Spring与Ehcache的集成</li>
</ul>

<h4>用户故事:</h4>
<h3>用户故事:</h3>
<ul>
<li>在AccountManager.java中演示了对Memcached的使用</li>
<li>EhcacheDemo.java演示了Ehcache与Spring的集成</li>
Expand Down
4 changes: 2 additions & 2 deletions examples/showcase/src/main/webapp/WEB-INF/views/story/jms.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
<body>
<h2>JMS演示</h2>

<h4>技术说明:</h4>
<h3>技术说明:</h3>
<ul>
<li>演示基于ActiveMQ的JMS Topic/Queue应用</li>
<li>演示基于Spring CachingConnectionFactory, JmsTemplate, DefaultMessageListener的应用</li>
<li>演示使用默认值的Simple模式</li>
<li>演示Advanced模式, 包括发送者的timeToLive等属性设置, 接受者的消息过滤器,消息确认模式与持久化订阅者</li>
</ul>

<h4>用户故事:</h4>
<h3>用户故事:</h3>
<ul>
<li>在综合演示用例中保存用户时,异步发送通知消息邮件</li>
<li>在servers/activemq目录演示优化过的activemq.xml配置文件</li>
Expand Down
35 changes: 27 additions & 8 deletions examples/showcase/src/main/webapp/WEB-INF/views/story/jmx.jsp
Original file line number Diff line number Diff line change
@@ -1,21 +1,40 @@
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="baseUrl" value="http://${pageContext.request.serverName}:${pageContext.request.serverPort}${pageContext.request.contextPath}"/>

<html>
<head>
<title>JMX演示用例</title>
<title>JMX演示用例</title>
</head>

<body>
<h2>JMX演示用例</h2>

<h4>技术说明:</h4>
<h3>技术说明:</h3>
<ul>
<li>服务端演示使用Spring annotation定义MBean</li>
<li>演示使用Spring annotation将POJO定义为MBean</li>
<li>演示使用jolokia将JMX输出为Restul JSON Monitor API</li>
</ul>

<h4>用户故事:</h4>
<div>
使用JMX动态配置查看和配置Log4J日志等级。<br /> 客户端可使用JConsole或JManager, 远程进程URL为 localhost:2099
或完整版的service:jmx:rmi:///jndi/rmi://localhost:2099/jmxrmi
</div>
<h3>MBean介绍:</h3>
<ul>
<li>Log4j Mbean,控制Log4j的Logger Level, name为log4j:name=Log4j</li>
<li>Application Statistics Mbean, 当用户在综合演示里查看/更新用户时,计数器将会递增, name为showcase:name=ApplicationStatistics</li>
</ul>

<h3>使用Jconsole或其他JMX客户端:</h3>
<ul>
<li>如果JConsole与应用在同一台机器,直接选择该进程。</li>
<li>否则远程进程URL为 localhost:2099 或完整版的service:jmx:rmi:///jndi/rmi://localhost:2099/jmxrmi</li>
</ul>

<h3>与国际接轨的Resultful API:</h3>
<ul>
<li>获取所有统计信息: <a href="${baseUrl}/jolokia/read/showcase:name=ApplicationStatistics">${baseUrl}/jolokia/read/showcase:name=ApplicationStatistics</a></li>
<li>获取展示用户列表次数: <a href="${baseUrl}/jolokia/read/showcase:name=ApplicationStatistics/ListUserTimes">${baseUrl}/jolokia/read/showcase:name=ApplicationStatistics/ListUserTimes</a></li>
<li>获取重置清零统计信息: <a href="${baseUrl}/jolokia/exec/showcase:name=ApplicationStatistics/resetStatistics">${baseUrl}/jolokia/exec/showcase:name=ApplicationStatistics/resetStatistics</a></li>
<li>获取showcase域下所有MBean的属性: <a href="${baseUrl}/jolokia/read/showcase:name=*">${baseUrl}/jolokia/read/showcase:name=*</a></li>
<li>获取特定Logger的Level: <a href="${baseUrl}/jolokia/exec/log4j:name=Log4j/getLoggerLevel/org.springside">${baseUrl}/jolokia/exec/log4j:name=Log4j/getLoggerLevel/org.springside</a></li>
</ul>
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
<body>
<h2>定时任务演示</h2>

<h4>技术说明:</h4>
<h3>技术说明:</h3>
<ul>
<li>JDK5.0 ScheduledExecutorService的Timer式任务定义, 支持Graceful Shutdown演示.</li>
<li>Spring的Cront式任务定义, 支持Graceful Shutdown演示.</li>
<li>Quartz的Timer式与Cron式任务定义.</li>
<li>Quartz的任务在内存或数据库中存储, 单机或集群执行演示.</li>
</ul>

<h4>用户故事:</h4>
<h3>用户故事:</h3>
<ul>
<li>简单的定时在Console打印当前用户数量.</li>
<li>设法同时运行两个实例, 演示Quartz集群运行的效果.</li>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
</head>

<body>
<h4>XML/JSON操作演示</h4>
<h3>XML/JSON操作演示</h3>
<ul>
<li>基于JAXB2.0的Java-XML绑定, 演示根元素是List, 属性是List与Map等特殊情况.</li>
<li>万能老倌Dom4j.</li>
Expand All @@ -14,29 +14,27 @@
<li>在JsonDemo.java中演示Jackson远超同类JSON库的转化能力.</li>
</ul>

<h4>Excel演示</h4>
<h3>Excel演示</h3>
<p>说明:演示基于POI的Excel操作。</p>
<ul>
<li><a href="/showcase/excel/export">导出Excel文件</a><br /> 演示冻结/合并单元格, 单元格字体/边框/颜色, 单元格数值格式/公式等特性.</li>
<li>读取Excel文件<br />见ExcelExportActionTest测试用例.</li>
</ul>

<h4>Email演示</h4>
<h3>Email演示</h3>
<ul>
<li>简单文本邮件演示.</li>
<li>带附件的MIME邮件演示, 使用Freemarker模板创建HTML内容.</li>
<li>在综合演示用例中保存用户时,发送两种邮件.<br /> 演示邮箱名为springside3.demo@gmail.com, 密码为demoforyou.</li>
</ul>

<h4>日志演示</h4>
<h3>日志演示</h3>
<ul>
<li>Log4JMBean: 通过JMX动态查询与改变Logger的日志等级与Appender.</li>
<li>MockLog4jAppender: 在测试用例中验证日志的输出.</li>
<li>使用JConsole动态修改log4j的日志等级.(路径service:jmx:rmi:///jndi/rmi://localhost:2099/jmxrmi,名称Log4j:name=log4j)</li>
<li>Schedule测试用例使用MockAppender校验日志输出.</li>
<li>Log4JManager: 通过JMX动态查询与改变Logger的日志等级与Appender, 详见JMX页面.</li>
<li>MockLog4jAppender: 在测试用例中验证日志的输出, 在Schedule测试用例使用MockAppender校验日志输出.</li>
</ul>

<h4>其他常用工具</h4>
<h3>其他常用工具</h3>
<p>全部演示在org.springside.examples.showcase.utilities目录</p>
<ul>
<li>JodaTime: 日期操作.</li>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<body>
<h2>Restful Service 高级演示</h2>

<h4>技术说明:</h4>
<h3>技术说明:</h3>
<ul>
<li>HttpBasic认证, 与Shiro权限控制结合.</li>
<li>Multi-part演示.</li>
Expand Down
Loading

0 comments on commit bc0bb68

Please sign in to comment.