后端

Solr删除或清空索引

删除索引

1
2
<delete><id>1</id></delete>
<commit/>


1
2
<delete><query>id:1</query></delete>
<commit/>

清空索引

1
2
<delete><query>*:*</query></delete>
<commit/>

时间复杂度

常见的时间复杂度有:常数阶O(1),对数阶O(log2n),线性阶O(n), 线性对数阶O(nlog2n),平方阶O(n2),立方阶O(n3),…, k次方阶O(nk),指数阶O(2n)。

Solr获取document的score及组合排序

获取每个document的score

查询是时需要将score作为返回字段

1
fl=*,score

先根据具体字段再根据score排序

Solr默认是按照score排序的,如果有下面需求,需要先按照某个字段排序(如类型),再按照score排序则需要传值

1
sort=字段名 ASC,score DESC

获取当前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)