use concurrent map

This commit is contained in:
DOHA 2015-11-06 14:15:32 +02:00
parent 4a24341f71
commit d1b3420ccf
2 changed files with 16 additions and 15 deletions

View File

@ -2,26 +2,27 @@ package org.baeldung.web.metric;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.Set; import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@Service @Service
public class MetricService implements IMetricService { public class MetricService implements IMetricService {
private Map<String, HashMap<Integer, Integer>> metricMap; private ConcurrentMap<String, ConcurrentHashMap<Integer, Integer>> metricMap;
private Map<Integer, Integer> statusMetric; private ConcurrentMap<Integer, Integer> statusMetric;
private Map<String, HashMap<Integer, Integer>> timeMap; private ConcurrentMap<String, ConcurrentHashMap<Integer, Integer>> timeMap;
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm"); private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
public MetricService() { public MetricService() {
super(); super();
metricMap = new HashMap<String, HashMap<Integer, Integer>>(); metricMap = new ConcurrentHashMap<String, ConcurrentHashMap<Integer, Integer>>();
statusMetric = new HashMap<Integer, Integer>(); statusMetric = new ConcurrentHashMap<Integer, Integer>();
timeMap = new HashMap<String, HashMap<Integer, Integer>>(); timeMap = new ConcurrentHashMap<String, ConcurrentHashMap<Integer, Integer>>();
} }
// API // API
@ -58,8 +59,8 @@ public class MetricService implements IMetricService {
j++; j++;
} }
int i = 1; int i = 1;
Map<Integer, Integer> tempMap; ConcurrentMap<Integer, Integer> tempMap;
for (final Entry<String, HashMap<Integer, Integer>> entry : timeMap.entrySet()) { for (final Entry<String, ConcurrentHashMap<Integer, Integer>> entry : timeMap.entrySet()) {
result[i][0] = entry.getKey(); result[i][0] = entry.getKey();
tempMap = entry.getValue(); tempMap = entry.getValue();
for (j = 1; j < colCount; j++) { for (j = 1; j < colCount; j++) {
@ -77,9 +78,9 @@ public class MetricService implements IMetricService {
// NON-API // NON-API
private void increaseMainMetric(final String request, final int status) { private void increaseMainMetric(final String request, final int status) {
HashMap<Integer, Integer> statusMap = metricMap.get(request); ConcurrentHashMap<Integer, Integer> statusMap = metricMap.get(request);
if (statusMap == null) { if (statusMap == null) {
statusMap = new HashMap<Integer, Integer>(); statusMap = new ConcurrentHashMap<Integer, Integer>();
} }
Integer count = statusMap.get(status); Integer count = statusMap.get(status);
@ -103,9 +104,9 @@ public class MetricService implements IMetricService {
private void updateTimeMap(final int status) { private void updateTimeMap(final int status) {
final String time = dateFormat.format(new Date()); final String time = dateFormat.format(new Date());
HashMap<Integer, Integer> statusMap = timeMap.get(time); ConcurrentHashMap<Integer, Integer> statusMap = timeMap.get(time);
if (statusMap == null) { if (statusMap == null) {
statusMap = new HashMap<Integer, Integer>(); statusMap = new ConcurrentHashMap<Integer, Integer>();
} }
Integer count = statusMap.get(status); Integer count = statusMap.get(status);

View File

@ -2,9 +2,9 @@
<beans:beans xmlns="http://www.springframework.org/schema/security" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans" <beans:beans xmlns="http://www.springframework.org/schema/security" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
xsi:schemaLocation=" xsi:schemaLocation="
http://www.springframework.org/schema/security http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-4.0.xsd http://www.springframework.org/schema/security/spring-security.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd" http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"
> >
<http pattern="/securityNone" security="none"/> <http pattern="/securityNone" security="none"/>