我正在计算页面上查看用户的数量。基本上,当用户查看URL localhost:8080/itemDetail.do?itemId=1
时,该页面将显示有多少用户同时在该页面上查看。计数当前用户查看页面
解决办法
当用户在浏览特定网页,该网页会继续发送Ajax请求每隔1秒回服务器,然后服务器将使用Map<String, Map<String, Integer>>
(都使用ConcurrentHashMap
用于初始化)至包含itemId
(要知道哪个页面被查看)和IP
和timeout
数(在Map
的内部)。每次获取请求时,计数将增加1.在请求过程中会有一个线程触发,以每2秒减少超时计数。如果最后,超时计数等于0,则应用程序将认为用户停止查看该页面,然后线程将删除该条目,从而减少用户数量。1.此方法的一个小问题是自超时如果用户打开页面足够长时间并关闭页面,则应用程序需要一段时间才能知道该用户已经离开该页面,因为此刻的超时数量相当大
实施
// Controller
@RequestMapping(value = AppConstants.ACTION_NUMBER_VIEWING, method = GET)
@ResponseBody
public int userItemViewing(HttpServletRequest request, @RequestParam(value = "itemId") String itemId) {
try {
return eventService.countUserOnline(request, itemId);
} catch (CAServiceException e) {
e.printStackTrace();
LOG.error("========== Error when counting number of online user ==========" + e.getMessage());
}
return 0;
}
// Service
private static Map<String, Map<String, Integer>> numberOfCurrentViews;
private Thread countDownOnlineThread;
class OnlineCountingDownRunnable implements Runnable {
private List<String> timeoutList = new ArrayList<String>();
private void cleanTimeoutIps() {
for (String itemId : numberOfCurrentViews.keySet()) {
Map<String, Integer> currentIps = numberOfCurrentViews.get(itemId);
for(String ip : timeoutList){
currentIps.remove(ip);
}
}
}
@Override
public void run() {
try {
for (String itemId : numberOfCurrentViews.keySet()) {
Map<String, Integer> currentIps = numberOfCurrentViews.get(itemId);
for(String ip : currentIps.keySet()){
Integer timeout = new Integer(currentIps.get(ip).intValue() - 1);
if (timeout == 0) {
timeoutList.add(ip);
}
currentIps.put(ip, timeout);
}
}
cleanTimeoutIps();
// counting down time must be double increasing time
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
LOG.error("---------------- Thread error in counting down online user: " + e.getMessage());
}
}
}
public int countUserOnline(HttpServletRequest request, String itemId) throws CAServiceException {
// create a count down timer to detect if the user does not view the page anymore
String ip = request.getRemoteAddr();
// init counting down online user map
if (numberOfCurrentViews == null) {
numberOfCurrentViews = new ConcurrentHashMap<String, Map<String, Integer>>();
}
// start thread to check user online
if (countDownOnlineThread == null) {
countDownOnlineThread = new Thread(new OnlineCountingDownRunnable());
countDownOnlineThread.start();
}
LOG.debug("---------------- Requested IP: " + ip);
if (ip == null || ip.isEmpty()) {
throw new CAServiceException("======= Cannot detect Ip of the client =======");
}
if (numberOfCurrentViews.get(itemId) != null) {
Map<String, Integer> userView = numberOfCurrentViews.get(itemId);
if (userView.get(ip) != null) {
userView.put(ip, userView.get(ip).intValue() + 1);
} else {
userView.put(ip, 1);
}
numberOfCurrentViews.put(itemId, userView);
} else {
Map<String, Integer> ips = new ConcurrentHashMap<String, Integer>();
ips.put(ip, 1);
numberOfCurrentViews.put(itemId, ips);
}
LOG.debug(String.format(
"============= %s is seeing there is %s users viewing item %s =============",
ip, numberOfCurrentViews.get(itemId).size(), itemId
));
return numberOfCurrentViews.get(itemId).size();
}
问题
我不知道如何测试这个功能,因为它需要多个IP地址来查看页面。我曾试图建立的JMeter并设置IP欺骗这样link,但没有成功,所以我做了一个小的模拟测试像这样
@Test
public void testCountUserOnline() throws Exception {
List<HttpServletRequest> requests = new ArrayList<HttpServletRequest>();
for (int i = 0; i < 10; i ++) {
HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
Mockito.when(request.getRemoteAddr()).thenReturn(String.format("192.168.1.%s", i));
requests.add(request);
}
List<Thread> threads = new ArrayList<Thread>();
for (int i = 0; i < 10; i ++) {
Thread thread = new Thread(new RequestRunnable(requests.get(i)));
threads.add(thread);
thread.start();
}
for (Thread thread : threads) {
thread.join();
}
}
class RequestRunnable implements Runnable {
private HttpServletRequest request;
public RequestRunnable(HttpServletRequest request) {
this.request = request;
}
public void run() {
try {
int i = 0;
while (i < 10) {
eventService.countUserOnline(request, "1");
i++;
System.out.println(i);
Thread.sleep(1000);
}
} catch (CAServiceException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
但再次查看日志,我没有那么自信了实施
此外,这是人类用来计算页面上查看用户数量的正常方式吗?我只是想确保我不会错过任何事情,以防这部分有任何捷径。我正在使用Spring MVC 3.x
。我需要支持IE 9以及:(((,所以Web Socket
不能被广泛使用
这应该是一个评论,而不是答案... – 2014-11-01 14:13:16