Skip to content

Commit

Permalink
springside#21 Threads增加sleep(long,TimeUnit)的方法
Browse files Browse the repository at this point in the history
         MockLog4jAppender增加getLogCount()方法并修正之前存在的NPE问题.
  • Loading branch information
calvin1978 committed Mar 28, 2012
1 parent cb23f45 commit c9f27dd
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
*/
package org.springside.modules.log;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.spi.LoggingEvent;

import com.google.common.collect.Lists;

/**
* 在List中保存日志的Appender, 用于测试Log4j的日志输出.
*
Expand All @@ -22,7 +23,7 @@
*/
public class MockLog4jAppender extends AppenderSkeleton {

private List<LoggingEvent> logs = new ArrayList<LoggingEvent>();
private List<LoggingEvent> logs = Lists.newArrayList();

/**
* 返回之前append的第一个log.
Expand All @@ -38,13 +39,19 @@ public LoggingEvent getFirstLog() {
* 返回之前append的第一个log的信息.
*/
public String getFirstMessage() {
if (logs.isEmpty()) {
return null;
}
return getFirstLog().getMessage().toString();
}

/**
* 返回之前appender的第一个log的按layout pattern格式化的信息.
* 返回之前append的第一个log的按layout pattern格式化的信息.
*/
public String getFirstRenderedMessage() {
if (logs.isEmpty()) {
return null;
}
return getLayout().format(getFirstLog());
}

Expand All @@ -62,13 +69,19 @@ public LoggingEvent getLastLog() {
* 返回之前append的最后一个log的信息.
*/
public String getLastMessage() {
if (getLastLog() == null) {
return null;
}
return getLastLog().getMessage().toString();
}

/**
* 返回之前appender的最后一个log的按layout pattern格式化的信息.
* 返回之前append的最后一个log的按layout pattern格式化的信息.
*/
public String getLastRenderedMessage() {
if (logs.isEmpty()) {
return null;
}
return getLayout().format(getLastLog());
}

Expand All @@ -79,6 +92,13 @@ public List<LoggingEvent> getAllLogs() {
return logs;
}

/**
* 返回Log的数量。
*/
public int getLogCount() {
return logs.size();
}

/**
* 判断是否有log.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ private Threads() {
}

/**
* sleep等待,单位毫秒,忽略InterruptedException.
* sleep等待,单位为毫秒,忽略InterruptedException.
*/
public static void sleep(long millis) {
try {
Expand All @@ -29,6 +29,17 @@ public static void sleep(long millis) {
}
}

/**
* sleep等待,忽略InterruptedException.
*/
public static void sleep(long duration, TimeUnit unit) {
try {
Thread.sleep(unit.toMillis(duration));
} catch (InterruptedException e) {
// Ignore.
}
}

/**
* 按照ExecutorService JavaDoc示例代码编写的Graceful Shutdown方法.
* 先使用shutdown, 停止接收新任务并尝试完成所有已存在任务.
Expand Down

0 comments on commit c9f27dd

Please sign in to comment.