Java

获取当前Java进程的活动线程数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/**
* 获取当前Java进程的活动线程数
* @return
*/

public Map<String, Object> getThreadsTotal() {
Map<String, Object> threadDataMap = new HashMap<String, Object>();

Integer currentThreadCount = 0;
Integer currentThreadsBusy = 0;

MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();
List<ObjectName> threadPools = new ArrayList<ObjectName>();
try {
threadPools.addAll(mbeanServer.queryNames(new ObjectName("*:type=ThreadPool,*"), null));

for (final ObjectName threadPool : threadPools) {
//获取所有线程池下的线程总数
currentThreadCount += (Integer) mbeanServer.getAttribute(threadPool, "currentThreadCount");
currentThreadsBusy += (Integer) mbeanServer.getAttribute(threadPool, "currentThreadsBusy");

}
} catch (MalformedObjectNameException e) {
logger.error("Get threads information error.", e);
} catch (ReflectionException e) {
logger.error("Reflecttion error", e);
} catch (InstanceNotFoundException e) {
logger.error("Instance not found error.", e);
} catch (MBeanException e) {
logger.error("Mean error", e);
} catch (AttributeNotFoundException e) {
logger.error("Could not get attribute", e);
}

threadDataMap.put("currentThreadCount", currentThreadCount);
threadDataMap.put("currentThreadsBusy", currentThreadsBusy);

return threadDataMap;
}

获得Tomcat的内存信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/**
* 获得Tomcat的内存信息
* @return
*/

private Map<String, Object> getMemoryInfo() {
Map<String, Object> memDataMap = new HashMap<String, Object>();

double MB = 1024.0 * 1024.0;

//当前JVM的最大可用内存
double maxMemory = (Runtime.getRuntime().maxMemory()) / MB;

//当前JVM占用的内存总数
double totalMemory = (Runtime.getRuntime().totalMemory()) / MB;

//因为JVM只有在需要内存时才占用物理内存使用,所以freeMemory()的值一般情况下都很小
double freeMemory = (Runtime.getRuntime().freeMemory()) / MB;

//JVM实际可用内存
double totalUsableMemory = maxMemory - totalMemory + freeMemory;

memDataMap.put("totalUsableMemory", totalUsableMemory);
memDataMap.put("totalMemory", totalMemory);
memDataMap.put("maxMemory", maxMemory);

NumberFormat nt = NumberFormat.getPercentInstance();
nt.setMinimumFractionDigits(2);
memDataMap.put("memUsage", nt.format(totalMemory / maxMemory));

return memDataMap;
}

tomcat启动不报错但是服务也没有启动成功

之前遇到了一次tomcat启动时没有报任何错误,但是服务也没有启动成功,访问不了,后来同事帮忙在网上找到这样一篇文章,这里记录下
DEBUGGING THE DREADED “SEVERE: ERROR LISTENERSTART” AND “SEVERE: ERROR FILTERSTART” TOMCAT ERROR MESSAGES

具体操作:

  1. 中web application下的WEB-INF/classes路径中创建logging.properties文件,其内容为:

    1
    org.apache.catalina.core.ContainerBase.[Catalina].level = INFO //INFO级别可以更改
    org.apache.catalina.core.ContainerBase.[Catalina].handlers = java.util.logging.ConsoleHandler
  2. 启动服务,这个时候错误就会打印到控制台(catalina.out)

Jmesa使用总结-自定义Toolbar

web.xml引入jmesa

1
2
3
4
5
6
7
8
<context-param>
<param-name>jmesaMessagesLocation</param-name>
<param-value>messages</param-value>
</context-param>
<context-param>
<param-name>jmesaPreferencesLocation</param-name>
<param-value>jmesa.properties</param-value>
</context-param>

Maven多模块中关于final static常量的一个坑

【背景】

多个独立的web项目合并到同一个Maven多模块项目中,目的是抽取公共模块,减少维护成本。
各个web项目的客户编码有不同的前缀,所以中各个web项目中都定义了一个常量,但是各个站点的值是不同的,中合并到一个Maven多模块项目后,抽取公共部分时由于中公共部分代码中需要用到这个常量,所以就随便将其中一个项目的该常量拿到了公共模块中,而各个站点的该常量保留,可是项目真正启动后,当使用该常量时发现其值一直是公共模块定义的,导致各个站点的客户编码使用了同样的前缀,从而出现了问题。

【结论】

如果公共模块中一个常量是各个站点都需要使用的,但是中各个单点的值又不一致,那么千万不要将该常量定义成final的。

【解决方法】

  • 去除final定义即可
  • 将该常量定义到各个项目的配置文件中

【举例】

Drools中常用但易混淆的概念

KIE/Drools/jBPM

官方的一句话解释了比较到位:
The process of researching an integration knowledge solution for Drools and jBPM has simply used the “droolsjbpm” group name. This name permeates GitHub accounts and Maven POMs. As scopes broadened and new projects were spun KIE, an acronym for Knowledge Is Everything, was chosen as the new group name. The KIE name is also used for the shared aspects of the system; such as the unified build, deploy and utilization.
kie

KieServices/KieRepository/KieProject/KieContainer/KieModule/KieBase/KieSession区别,部分转自Drools入门系列(六)——KIE之基础API详解

使用Maven模板创建项目

列出Maven模板列表(1000+),并选择模板创建项目

mvn archetype:generate

导出Maven模板列表到txt

mvn archetype:generate > templates.txt

导入Eclipse

mvn eclipse:eclipse

根据常用模板创建项目

maven-archetype-quickstart (Java Project)

1
$ mvn archetype:generate -DgroupId=com.yiibai.core -DartifactId=ProjectName -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

maven-archetype-webapp (Java Web Project)

1
$ mvn archetype:generate -DgroupId=com.yiibai.web -DartifactId=ProjectName -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false