Skip to content

Commit 6368ca6

Browse files
committed
issue binarywang#69 添加Session的支持,添加测试用例
1 parent d18b66c commit 6368ca6

File tree

16 files changed

+514
-132
lines changed

16 files changed

+514
-132
lines changed

weixin-java-common/src/main/java/me/chanjar/weixin/common/session/InternalSessionManager.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,19 @@
22

33
public interface InternalSessionManager {
44

5+
/**
6+
* Return the active Session, associated with this Manager, with the
7+
* specified session id (if any); otherwise return <code>null</code>.
8+
*
9+
* @param id The session id for the session to be returned
10+
*
11+
* @exception IllegalStateException if a new session cannot be
12+
* instantiated for any reason
13+
* @exception java.io.IOException if an input/output error occurs while
14+
* processing this request
15+
*/
16+
InternalSession findSession(String id);
17+
518
/**
619
* Construct and return a new session object, based on the default
720
* settings specified by this Manager's properties. The session
@@ -68,7 +81,33 @@ public interface InternalSessionManager {
6881
*/
6982
void setMaxInactiveInterval(int interval);
7083

84+
/**
85+
* <pre>
86+
* Set the manager checks frequency.
87+
* 设置每尝试多少次清理过期session,才真的会执行一次清理动作
88+
* 要和{@link #setBackgroundProcessorDelay(int)}联合起来看
89+
* 如果把这个数字设置为6(默认),那么就是说manager要等待 6 * backgroundProcessorDay的时间才会清理过期session
90+
* </pre>
91+
* @param processExpiresFrequency the new manager checks frequency
92+
*/
7193
void setProcessExpiresFrequency(int processExpiresFrequency);
7294

95+
/**
96+
* <pre>
97+
* Set the manager background processor delay
98+
* 设置manager sleep几秒,尝试执行一次background操作(清理过期session)
99+
* </pre>
100+
* @param backgroundProcessorDelay
101+
*/
73102
void setBackgroundProcessorDelay(int backgroundProcessorDelay);
103+
104+
105+
/**
106+
* Set the maximum number of active Sessions allowed, or -1 for
107+
* no limit.
108+
* 设置最大活跃session数,默认无限
109+
* @param max The new maximum number of sessions
110+
*/
111+
void setMaxActiveSessions(int max);
112+
74113
}

weixin-java-common/src/main/java/me/chanjar/weixin/common/session/SessionImpl.java renamed to weixin-java-common/src/main/java/me/chanjar/weixin/common/session/StandardSession.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import java.util.concurrent.ConcurrentHashMap;
77
import java.util.concurrent.atomic.AtomicInteger;
88

9-
public class SessionImpl implements WxSession, InternalSession {
9+
public class StandardSession implements WxSession, InternalSession {
1010

1111
/**
1212
* The string manager for this package.
@@ -129,15 +129,15 @@ public void invalidate() {
129129
* The facade associated with this session. NOTE: This value is not
130130
* included in the serialized version of this object.
131131
*/
132-
protected transient InternalSessionFacade facade = null;
132+
protected transient StandardSessionFacade facade = null;
133133

134134
/**
135135
* The access count for this session.
136136
*/
137137
protected transient AtomicInteger accessCount = null;
138138

139139

140-
public SessionImpl(InternalSessionManager manager) {
140+
public StandardSession(InternalSessionManager manager) {
141141
this.manager = manager;
142142
this.accessCount = new AtomicInteger();
143143
}
@@ -147,7 +147,7 @@ public SessionImpl(InternalSessionManager manager) {
147147
public WxSession getSession() {
148148

149149
if (facade == null){
150-
facade = new InternalSessionFacade(this);
150+
facade = new StandardSessionFacade(this);
151151
}
152152
return (facade);
153153

@@ -281,7 +281,6 @@ public void setCreationTime(long time) {
281281

282282
@Override
283283
public void setMaxInactiveInterval(int interval) {
284-
int oldMaxInactiveInterval = this.maxInactiveInterval;
285284
this.maxInactiveInterval = interval;
286285
}
287286

@@ -311,9 +310,9 @@ protected String[] keys() {
311310
@Override
312311
public boolean equals(Object o) {
313312
if (this == o) return true;
314-
if (!(o instanceof SessionImpl)) return false;
313+
if (!(o instanceof StandardSession)) return false;
315314

316-
SessionImpl session = (SessionImpl) o;
315+
StandardSession session = (StandardSession) o;
317316

318317
if (creationTime != session.creationTime) return false;
319318
if (expiring != session.expiring) return false;

weixin-java-common/src/main/java/me/chanjar/weixin/common/session/InternalSessionFacade.java renamed to weixin-java-common/src/main/java/me/chanjar/weixin/common/session/StandardSessionFacade.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,21 @@
22

33
import java.util.Enumeration;
44

5-
public class InternalSessionFacade implements WxSession {
5+
public class StandardSessionFacade implements WxSession {
66

77
/**
88
* Wrapped session object.
99
*/
1010
private WxSession session = null;
1111

12-
public InternalSessionFacade(WxSession session) {
12+
public StandardSessionFacade(StandardSession session) {
1313
this.session = session;
1414
}
1515

16+
public InternalSession getInternalSession() {
17+
return (InternalSession) session;
18+
}
19+
1620
@Override
1721
public Object getAttribute(String name) {
1822
return session.getAttribute(name);
@@ -37,4 +41,5 @@ public void removeAttribute(String name) {
3741
public void invalidate() {
3842
session.invalidate();
3943
}
44+
4045
}

weixin-java-common/src/main/java/me/chanjar/weixin/common/session/InMemorySessionManager.java renamed to weixin-java-common/src/main/java/me/chanjar/weixin/common/session/StandardSessionManager.java

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@
88
import java.util.concurrent.ConcurrentHashMap;
99
import java.util.concurrent.atomic.AtomicBoolean;
1010

11-
public class InMemorySessionManager implements WxSessionManager, InternalSessionManager {
11+
/**
12+
* 基于内存的session manager
13+
*/
14+
public class StandardSessionManager implements WxSessionManager, InternalSessionManager {
1215

13-
protected final Logger log = LoggerFactory.getLogger(InMemorySessionManager.class);
16+
protected final Logger log = LoggerFactory.getLogger(StandardSessionManager.class);
1417

1518
protected static final StringManager sm =
1619
StringManager.getManager(Constants.Package);
@@ -128,18 +131,9 @@ public void remove(InternalSession session, boolean update) {
128131
}
129132

130133

131-
/**
132-
* Return the active Session, associated with this Manager, with the
133-
* specified session id (if any); otherwise return <code>null</code>.
134-
*
135-
* @param id The session id for the session to be returned
136-
*
137-
* @exception IllegalStateException if a new session cannot be
138-
* instantiated for any reason
139-
* @exception java.io.IOException if an input/output error occurs while
140-
* processing this request
141-
*/
142-
protected InternalSession findSession(String id) {
134+
135+
@Override
136+
public InternalSession findSession(String id) {
143137

144138
if (id == null)
145139
return (null);
@@ -189,12 +183,11 @@ public InternalSession createEmptySession() {
189183
return (getNewSession());
190184
}
191185

192-
193186
/**
194187
* Get new session class to be used in the doLoad() method.
195188
*/
196189
protected InternalSession getNewSession() {
197-
return new SessionImpl(this);
190+
return new StandardSession(this);
198191
}
199192

200193

@@ -312,4 +305,17 @@ public String getName() {
312305

313306
}
314307

308+
/**
309+
* Set the maximum number of active Sessions allowed, or -1 for
310+
* no limit.
311+
*
312+
* @param max The new maximum number of sessions
313+
*/
314+
@Override
315+
public void setMaxActiveSessions(int max) {
316+
317+
this.maxActiveSessions = max;
318+
319+
}
320+
315321
}

weixin-java-common/src/main/java/me/chanjar/weixin/common/util/WxMessageInMemoryDuplicateChecker.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ public void run() {
8282

8383
@Override
8484
public boolean isDuplicate(Long wxMsgId) {
85+
if (wxMsgId == null) {
86+
return false;
87+
}
8588
checkBackgroundProcessStarted();
8689
Long timestamp = msgId2Timestamp.putIfAbsent(wxMsgId, System.currentTimeMillis());
8790
if (timestamp == null) {
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
package me.chanjar.weixin.common.session;
2+
3+
import org.testng.Assert;
4+
import org.testng.annotations.DataProvider;
5+
import org.testng.annotations.Test;
6+
7+
@Test
8+
public class SessionTest {
9+
10+
@DataProvider
11+
public Object[][] getSessionManager() {
12+
13+
return new Object[][] {
14+
new Object[] { new StandardSessionManager() }
15+
};
16+
17+
}
18+
19+
20+
@Test(dataProvider = "getSessionManager", expectedExceptions = IllegalStateException.class)
21+
public void testInvalidate(WxSessionManager sessionManager) {
22+
23+
WxSession session = sessionManager.getSession("abc");
24+
session.invalidate();
25+
session.getAttributeNames();
26+
27+
}
28+
29+
@Test(dataProvider = "getSessionManager")
30+
public void testInvalidate2(InternalSessionManager sessionManager) {
31+
32+
Assert.assertEquals(sessionManager.getActiveSessions(), 0);
33+
WxSession session = ((WxSessionManager) sessionManager).getSession("abc");
34+
Assert.assertEquals(sessionManager.getActiveSessions(), 1);
35+
session.invalidate();
36+
Assert.assertEquals(sessionManager.getActiveSessions(), 0);
37+
38+
}
39+
40+
@Test(dataProvider = "getSessionManager")
41+
public void testGetSession(WxSessionManager sessionManager) {
42+
43+
WxSession session1 = sessionManager.getSession("abc");
44+
WxSession session2 = sessionManager.getSession("abc");
45+
Assert.assertEquals(session1, session2);
46+
Assert.assertTrue(session1 == session2);
47+
48+
WxSession abc1 = sessionManager.getSession("abc1");
49+
Assert.assertNotEquals(session1, abc1);
50+
51+
WxSession abc1b = sessionManager.getSession("abc1", false);
52+
Assert.assertEquals(abc1, abc1b);
53+
54+
WxSession def = sessionManager.getSession("def", false);
55+
Assert.assertNull(def);
56+
57+
}
58+
59+
@Test(dataProvider = "getSessionManager")
60+
public void testInvalidateAngGet(WxSessionManager sessionManager) {
61+
62+
WxSession session1 = sessionManager.getSession("abc");
63+
session1.invalidate();
64+
WxSession session2 = sessionManager.getSession("abc");
65+
Assert.assertNotEquals(session1, session2);
66+
InternalSessionManager ism = (InternalSessionManager) sessionManager;
67+
Assert.assertEquals(ism.getActiveSessions(), 1);
68+
69+
}
70+
71+
@Test(dataProvider = "getSessionManager")
72+
public void testBackgroundProcess(WxSessionManager sessionManager) throws InterruptedException {
73+
74+
InternalSessionManager ism = (InternalSessionManager) sessionManager;
75+
ism.setMaxInactiveInterval(1);
76+
ism.setProcessExpiresFrequency(1);
77+
ism.setBackgroundProcessorDelay(1);
78+
79+
Assert.assertEquals(ism.getActiveSessions(), 0);
80+
81+
InternalSession abc = ism.createSession("abc");
82+
abc.endAccess();
83+
84+
Thread.sleep(2000l);
85+
Assert.assertEquals(ism.getActiveSessions(), 0);
86+
87+
}
88+
89+
@Test(dataProvider = "getSessionManager")
90+
public void testBackgroundProcess2(WxSessionManager sessionManager) throws InterruptedException {
91+
92+
InternalSessionManager ism = (InternalSessionManager) sessionManager;
93+
ism.setMaxInactiveInterval(100);
94+
ism.setProcessExpiresFrequency(1);
95+
ism.setBackgroundProcessorDelay(1);
96+
97+
Assert.assertEquals(ism.getActiveSessions(), 0);
98+
99+
InternalSession abc = ism.createSession("abc");
100+
abc.setMaxInactiveInterval(1);
101+
abc.endAccess();
102+
103+
Thread.sleep(2000l);
104+
Assert.assertEquals(ism.getActiveSessions(), 0);
105+
106+
}
107+
108+
@Test(dataProvider = "getSessionManager")
109+
public void testMaxActive(WxSessionManager sessionManager) throws InterruptedException {
110+
111+
InternalSessionManager ism = (InternalSessionManager) sessionManager;
112+
ism.setMaxActiveSessions(2);
113+
114+
ism.createSession("abc");
115+
ism.createSession("abc");
116+
ism.createSession("def");
117+
118+
}
119+
120+
@Test(dataProvider = "getSessionManager", expectedExceptions = TooManyActiveSessionsException.class)
121+
public void testMaxActive2(WxSessionManager sessionManager) throws InterruptedException {
122+
123+
InternalSessionManager ism = (InternalSessionManager) sessionManager;
124+
ism.setMaxActiveSessions(2);
125+
126+
ism.createSession("abc");
127+
ism.createSession("abc");
128+
ism.createSession("def");
129+
ism.createSession("xyz");
130+
131+
}
132+
}

0 commit comments

Comments
 (0)