javax.servlet.http.HttpSession.getLastAccessedTime()方法的使用及代码示例

x33g5p2x  于2022-01-19 转载在 其他  
字(13.4k)|赞(0)|评价(0)|浏览(230)

本文整理了Java中javax.servlet.http.HttpSession.getLastAccessedTime()方法的一些代码示例,展示了HttpSession.getLastAccessedTime()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。HttpSession.getLastAccessedTime()方法的具体详情如下:
包路径:javax.servlet.http.HttpSession
类名称:HttpSession
方法名:getLastAccessedTime

HttpSession.getLastAccessedTime介绍

[英]Returns the last time the client sent a request associated with this session, as the number of milliseconds since midnight January 1, 1970 GMT, and marked by the time the container received the request.

Actions that your application takes, such as getting or setting a value associated with the session, do not affect the access time.
[中]返回客户端上次发送与此会话关联的请求的时间,以自1970年1月1日格林威治标准时间午夜以来的毫秒数表示,并以容器接收请求的时间标记。
应用程序执行的操作(例如获取或设置与会话关联的值)不会影响访问时间。

代码示例

代码示例来源:origin: perwendel/spark

/**
 * @return the last time the client sent a request associated with this session,
 * as the number of milliseconds since midnight January 1, 1970 GMT, and marked
 * by the time the container received the request.
 */
public long lastAccessedTime() {
  return session.getLastAccessedTime();
}

代码示例来源:origin: jfinal/jfinal

public long getLastAccessedTime() {
  return session.getLastAccessedTime();
}

代码示例来源:origin: jfinal/jfinal

public long getLastAccessedTime() {
  return session.getLastAccessedTime();
}

代码示例来源:origin: apache/shiro

public Date getLastAccessTime() {
  return new Date(httpSession.getLastAccessedTime());
}

代码示例来源:origin: apache/geode

@Override
 public void call(HttpServletRequest request, HttpServletResponse response)
   throws IOException {
  HttpSession session = request.getSession();
  // Hack to expose the session to our test context
  session.getServletContext().setAttribute("session", session);
  session.setAttribute("lastAccessTime", session.getLastAccessedTime());
  try {
   Thread.sleep(100);
  } catch (InterruptedException ex) {
  }
  session.setAttribute("somethingElse", 1);
  request.getSession();
  response.getWriter().write("done");
 }
};

代码示例来源:origin: Atmosphere/atmosphere

public FakeHttpSession(HttpSession session) {
  this(session.getId(), session.getServletContext(), session.getLastAccessedTime(), session.getMaxInactiveInterval());
  copyAttributes(session);
}

代码示例来源:origin: linkedin/cruise-control

private void expireOldSessions() {
 long now = _time.milliseconds();
 synchronized (_sessionToUserTaskIdMap) {
  Iterator<Map.Entry<SessionKey, UUID>> iter = _sessionToUserTaskIdMap.entrySet().iterator();
  while (iter.hasNext()) {
   Map.Entry<SessionKey, UUID> entry = iter.next();
   SessionKey sessionKey = entry.getKey();
   HttpSession session = sessionKey.httpSession();
   if (LOG.isTraceEnabled()) {
    LOG.trace("Session {} was last accessed at {}, age is {} ms", session, session.getLastAccessedTime(),
         now - session.getLastAccessedTime());
   }
   if (now >= session.getLastAccessedTime() + _sessionExpiryMs) {
    LOG.info("Expiring SessionKey {}", entry.getKey());
    iter.remove();
    session.invalidate();
   }
  }
 }
}

代码示例来源:origin: apache/ignite

/**
 * @param id Session ID.
 * @param ses Session.
 */
WebSession(String id, HttpSession ses) {
  assert id != null;
  assert ses != null;
  this.id = id;
  createTime = ses.getCreationTime();
  accessTime = ses.getLastAccessedTime();
  maxInactiveInterval = ses.getMaxInactiveInterval();
  isNew = ses.isNew();
  attrs = new HashMap<>();
  Enumeration<String> names = ses.getAttributeNames();
  while (names.hasMoreElements()) {
    String name = names.nextElement();
    attrs.put(name, ses.getAttribute(name));
  }
}

代码示例来源:origin: linkedin/cruise-control

/**
 * Expire the sessions that have been inactive for more than configured expiration time.
 */
synchronized void expireOldSessions() {
 long now = _time.milliseconds();
 Iterator<Map.Entry<HttpSession, SessionInfo>> iter = _inProgressSessions.entrySet().iterator();
 while (iter.hasNext()) {
  Map.Entry<HttpSession, SessionInfo> entry = iter.next();
  HttpSession session = entry.getKey();
  SessionInfo info = entry.getValue();
  if (LOG.isTraceEnabled()) {
   LOG.trace("Session {} was last accessed at {}, age is {} ms", session, session.getLastAccessedTime(),
        now - session.getLastAccessedTime());
  }
  if (now >= session.getLastAccessedTime() + _sessionExpiryMs) {
   LOG.info("Expiring session {}.", session);
   iter.remove();
   session.invalidate();
   _sessionLifetimeTimer.update(System.nanoTime() - info.requestStartTimeNs(), TimeUnit.NANOSECONDS);
   if (info.lastFuture().isDone() && info.executionTime() > 0) {
    _successfulRequestExecutionTimer.get(info.endPoint()).update(info.executionTime(), TimeUnit.NANOSECONDS);
   } else {
    info.lastFuture().cancel(true);
   }
  }
 }
}

代码示例来源:origin: spring-projects/spring-session

@Override
  public void doFilter(HttpServletRequest wrappedRequest) {
    HttpSession session = wrappedRequest.getSession();
    long now = System.currentTimeMillis();
    long fiveSecondsAgo = now - TimeUnit.SECONDS.toMillis(5);
    assertThat(session.getLastAccessedTime()).isLessThanOrEqualTo(now);
    assertThat(session.getLastAccessedTime())
        .isGreaterThanOrEqualTo(fiveSecondsAgo);
  }
});

代码示例来源:origin: spring-projects/spring-session

@Override
  public void doFilter(HttpServletRequest wrappedRequest) {
    HttpSession session = wrappedRequest.getSession();
    session.invalidate();
    try {
      session.getLastAccessedTime();
      fail("Expected Exception");
    }
    catch (IllegalStateException ignored) {
    }
  }
});

代码示例来源:origin: javamelody/javamelody

void unregisterSessionIfNeeded(HttpSession session) {
  if (session != null) {
    try {
      session.getCreationTime();
      // https://issues.jenkins-ci.org/browse/JENKINS-20532
      // https://bugs.eclipse.org/bugs/show_bug.cgi?id=413019
      session.getLastAccessedTime();
    } catch (final IllegalStateException e) {
      // session.getCreationTime() lance IllegalStateException si la session est invalidée
      synchronized (session) {
        sessionDestroyed(new HttpSessionEvent(session));
      }
    }
  }
}

代码示例来源:origin: spring-projects/spring-session

@Override
  public void doFilter(HttpServletRequest wrappedRequest) {
    long lastAccessed = wrappedRequest.getSession().getLastAccessedTime();
    assertThat(lastAccessed)
        .isGreaterThan(wrappedRequest.getSession().getCreationTime());
  }
});

代码示例来源:origin: spring-projects/spring-session

@Override
  public void doFilter(HttpServletRequest wrappedRequest) {
    long lastAccessed = wrappedRequest.getSession().getLastAccessedTime();
    assertThat(lastAccessed).isCloseTo(
        wrappedRequest.getSession().getCreationTime(), Offset.offset(5L));
    SessionRepositoryFilterTests.this.request.setAttribute(ACCESS_ATTR,
        lastAccessed);
  }
});

代码示例来源:origin: linkedin/cruise-control

private TestContext prepareRequests(boolean expectSessionInvalidation, int numRequests) {
 Time time = new MockTime();
 List<HttpServletRequest> requests = new ArrayList<>(numRequests);
 List<HttpSession> sessions = new ArrayList<>();
 for (int i = 0; i < numRequests; i++) {
  HttpServletRequest request = EasyMock.mock(HttpServletRequest.class);
  HttpSession session = EasyMock.mock(HttpSession.class);
  requests.add(request);
  sessions.add(session);
  EasyMock.expect(request.getSession()).andReturn(session).anyTimes();
  EasyMock.expect(request.getSession(false)).andReturn(session).anyTimes();
  EasyMock.expect(request.getMethod()).andReturn("GET").anyTimes();
  EasyMock.expect(request.getRequestURI()).andReturn("/test").anyTimes();
  EasyMock.expect(request.getParameterMap()).andReturn(Collections.emptyMap()).anyTimes();
  EasyMock.expect(session.getLastAccessedTime()).andReturn(time.milliseconds()).anyTimes();
  if (expectSessionInvalidation) {
   session.invalidate();
   EasyMock.expectLastCall().once();
  }
 }
 EasyMock.replay(requests.toArray());
 EasyMock.replay(sessions.toArray());
 return new TestContext(requests, time);
}

代码示例来源:origin: linkedin/cruise-control

@Test
public void testExpireSession() throws Exception {
 UUID testUserTaskId = UUID.randomUUID();
 UserTaskManager.UUIDGenerator mockUUIDGenerator = EasyMock.mock(UserTaskManager.UUIDGenerator.class);
 EasyMock.expect(mockUUIDGenerator.randomUUID()).andReturn(testUserTaskId).anyTimes();
 Time mockTime = new MockTime();
 HttpSession mockHttpSession = EasyMock.mock(HttpSession.class);
 EasyMock.expect(mockHttpSession.getLastAccessedTime()).andReturn(mockTime.milliseconds()).anyTimes();
 mockHttpSession.invalidate();
 HttpServletRequest mockHttpServletRequest = prepareRequest(mockHttpSession, null);
 OperationFuture future = new OperationFuture("future");
 UserTaskManager userTaskManager = new UserTaskManager(1000, 1, TimeUnit.HOURS.toMillis(6),
                            100, mockTime, mockUUIDGenerator);
 HttpServletResponse mockHttpServletResponse = EasyMock.mock(HttpServletResponse.class);
 mockHttpServletResponse.setHeader(EasyMock.anyString(), EasyMock.anyString());
 EasyMock.replay(mockUUIDGenerator, mockHttpSession, mockHttpServletResponse);
 // test-case: test if the sessions are removed on expiration
 OperationFuture future1 =
   userTaskManager.getOrCreateUserTask(mockHttpServletRequest, mockHttpServletResponse, uuid -> future, 0).get(0);
 Assert.assertEquals(future, future1);
 mockTime.sleep(1001);
 Thread.sleep(TimeUnit.SECONDS.toMillis(UserTaskManager.USER_TASK_SCANNER_PERIOD_SECONDS + 1));
 OperationFuture future2 = userTaskManager.getFuture(mockHttpServletRequest);
 Assert.assertNull(future2);
 userTaskManager.close();
}

代码示例来源:origin: linkedin/cruise-control

@Test
public void testMaximumActiveTasks() {
 HttpSession mockHttpSession1 = EasyMock.mock(HttpSession.class);
 EasyMock.expect(mockHttpSession1.getLastAccessedTime()).andReturn(100L).anyTimes();
 HttpServletRequest mockHttpServletRequest1 = prepareRequest(mockHttpSession1, null);
 OperationFuture future = new OperationFuture("future");
 UserTaskManager userTaskManager = new UserTaskManager(1000, 1, TimeUnit.HOURS.toMillis(6),
                            100, new MockTime());
 HttpServletResponse mockHttpServletResponse = EasyMock.mock(HttpServletResponse.class);
 mockHttpServletResponse.setHeader(EasyMock.anyString(), EasyMock.anyString());
 EasyMock.replay(mockHttpSession1, mockHttpServletResponse);
 // test-case: test max limitation active tasks
 OperationFuture future1 =
   userTaskManager.getOrCreateUserTask(mockHttpServletRequest1, mockHttpServletResponse, uuid -> future, 0).get(0);
 Assert.assertEquals(future, future1);
 HttpSession mockHttpSession2 = EasyMock.mock(HttpSession.class);
 EasyMock.expect(mockHttpSession2.getLastAccessedTime()).andReturn(100L).anyTimes();
 EasyMock.replay(mockHttpSession2);
 EasyMock.reset(mockHttpServletResponse);
 HttpServletRequest mockHttpServletRequest2 = prepareRequest(mockHttpSession2, null, "/test2", Collections.emptyMap());
 try {
  OperationFuture future2 =
    userTaskManager.getOrCreateUserTask(mockHttpServletRequest2, mockHttpServletResponse, uuid -> future, 0).get(0);
  Assert.assertEquals(future, future2);
 } catch (RuntimeException e) {
  userTaskManager.close();
  return;
 }
 Assert.fail("Don't expect to be here!");
}

代码示例来源:origin: linkedin/cruise-control

@Test
public void testCompletedTasks() throws Exception {
 HttpSession mockHttpSession = EasyMock.mock(HttpSession.class);
 EasyMock.expect(mockHttpSession.getLastAccessedTime()).andReturn(100L).anyTimes();
 mockHttpSession.invalidate();
 HttpServletRequest mockHttpServletRequest = prepareRequest(mockHttpSession, null);
 UserTaskManager.UUIDGenerator mockUUIDGenerator = EasyMock.mock(UserTaskManager.UUIDGenerator.class);
 EasyMock.expect(mockUUIDGenerator.randomUUID()).andReturn(UUID.randomUUID()).anyTimes();
 OperationFuture future = new OperationFuture("future");
 UserTaskManager userTaskManager = new UserTaskManager(1000, 1, TimeUnit.HOURS.toMillis(6),
                            100, new MockTime(), mockUUIDGenerator);
 HttpServletResponse mockHttpServletResponse = EasyMock.mock(HttpServletResponse.class);
 Capture<String> userTaskHeader = Capture.newInstance();
 Capture<String> userTaskHeaderValue = Capture.newInstance();
 mockHttpServletResponse.setHeader(EasyMock.capture(userTaskHeader), EasyMock.capture(userTaskHeaderValue));
 EasyMock.replay(mockUUIDGenerator, mockHttpSession, mockHttpServletResponse);
 // test-case: verify if the background cleaner task removes tasks that are completed
 OperationFuture future1 =
   userTaskManager.getOrCreateUserTask(mockHttpServletRequest, mockHttpServletResponse, uuid -> future, 0).get(0);
 Assert.assertEquals(future, future1);
 future1.cancel(true);
 Thread.sleep(TimeUnit.SECONDS.toMillis(UserTaskManager.USER_TASK_SCANNER_PERIOD_SECONDS * 4));
 Assert.assertTrue(future.isDone());
 Assert.assertTrue(future.isCancelled());
 userTaskManager.close();
}

代码示例来源:origin: linkedin/cruise-control

EasyMock.expect(mockHttpSession.getLastAccessedTime()).andReturn(100L).anyTimes();

代码示例来源:origin: linkedin/cruise-control

@Test
public void testAddStepsFutures() {
 UUID testUserTaskId = UUID.randomUUID();
 UserTaskManager.UUIDGenerator mockUUIDGenerator = EasyMock.mock(UserTaskManager.UUIDGenerator.class);
 EasyMock.expect(mockUUIDGenerator.randomUUID()).andReturn(testUserTaskId).anyTimes();
 HttpSession mockHttpSession = EasyMock.mock(HttpSession.class);
 EasyMock.expect(mockHttpSession.getLastAccessedTime()).andReturn(100L).anyTimes();
 HttpServletRequest mockHttpServletRequest = prepareRequest(mockHttpSession, null);
 HttpServletResponse mockHttpServletResponse = EasyMock.mock(HttpServletResponse.class);
 mockHttpServletResponse.setHeader(EasyMock.anyString(), EasyMock.anyString());
 EasyMock.replay(mockUUIDGenerator, mockHttpSession, mockHttpServletResponse);
 UserTaskManager userTaskManager = new UserTaskManager(1000, 1, TimeUnit.HOURS.toMillis(6),
                            100, new MockTime(), mockUUIDGenerator);
 OperationFuture testFuture1 = new OperationFuture("testFuture1");
 OperationFuture testFuture2 = new OperationFuture("testFuture2");
 OperationFuture insertedFuture1 =
   userTaskManager.getOrCreateUserTask(mockHttpServletRequest, mockHttpServletResponse, uuid -> testFuture1, 0).get(0);
 Assert.assertEquals(testFuture1, insertedFuture1);
 EasyMock.reset(mockHttpServletResponse);
 OperationFuture insertedFuture2 =
   userTaskManager.getOrCreateUserTask(mockHttpServletRequest, mockHttpServletResponse, uuid -> testFuture2, 1).get(1);
 Assert.assertEquals(testFuture2, insertedFuture2);
 Assert.assertEquals(userTaskManager.getFuturesByUserTaskId(testUserTaskId, mockHttpServletRequest).size(), 2);
 userTaskManager.close();
}

相关文章