Skip to content

Commit

Permalink
更新
Browse files Browse the repository at this point in the history
之前的demo有些小问题。
路由器只有一个。所以只需初始化一次。
之前的demo中把初始化写在了doPost里。每一次请求都初始化一个router。这个是不对的!
  • Loading branch information
antgan committed Jul 10, 2016
1 parent e02b470 commit 3d9d47c
Show file tree
Hide file tree
Showing 14 changed files with 45 additions and 15 deletions.
2 changes: 1 addition & 1 deletion demo/.classpath
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jst.server.core.container/org.eclipse.jst.server.tomcat.runtimeTarget/Apache Tomcat v7.0"/>
<classpathentry kind="lib" path="E:/maven/demo/src/main/webapp/WEB-INF/lib/wx-tools-1.0.0.jar">
<classpathentry kind="lib" path="E:/maven/demo/src/main/webapp/WEB-INF/lib/wx-tools-1.0.1.jar">
<attributes>
<attribute name="source_encoding" value="UTF-8"/>
</attributes>
Expand Down
4 changes: 2 additions & 2 deletions demo/.project
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<name>org.eclipse.wst.validation.validationbuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.wst.validation.validationbuilder</name>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
Expand Down
1 change: 0 additions & 1 deletion demo/.settings/org.eclipse.wst.common.component
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
<wb-resource deploy-path="/" source-path="/src/main/webapp" tag="defaultRootSource"/>
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/java"/>
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/resources"/>
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/test/java"/>
<property name="context-root" value="demo"/>
<property name="java-output-path" value="/demo/target/classes"/>
</wb-module>
Expand Down
12 changes: 12 additions & 0 deletions demo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,17 @@
</dependencies>
<build>
<finalName>demo</finalName>
<plugins>
<!-- define the project compile level -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
7 changes: 4 additions & 3 deletions demo/src/main/java/wxtools/demo/servlet/DemoServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,14 @@ public class DemoServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private WxConfigStorage config;
private WxService service;
private WxMessageRouter router = null;//理论上,路由器只有一个。所以只初始化一次。
//之前是有错误的。之前把router放进doPost里,会导致每个请求多初始化一次,线程池多初始化一次。

public DemoServlet() {
config = WxInMemoryConfigStorage.getInstance();
service = WxServiceImpl.getInstance();
service.setWxConfigStorage(config);
router = new WxMessageRouter(WxServiceImpl.getInstance());
}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Expand All @@ -56,8 +59,6 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) t
if(WxServiceImpl.getInstance().checkSignature(signature, timestamp, nonce, echostr)){
out.print(echostr);
}


}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Expand All @@ -73,7 +74,7 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
String XMLMessage = XStreamTransformer.toXml(WxXmlMessage.class, wx);
System.out.println("消息(xml):\n "+XMLMessage);

WxMessageRouter router = new WxMessageRouter(WxServiceImpl.getInstance());
//路由器在整个程序中只有一个。
router.rule().msgType(WxConsts.XML_MSG_TEXT).async(false).matcher(new DemoMatcher()).interceptor(new DemoInterceptor()).handler(new DemoMessageHandler()).end();
xmlOutMsg = router.route(wx);
if(xmlOutMsg!=null)
Expand Down
Binary file not shown.
6 changes: 5 additions & 1 deletion demo/src/main/webapp/WEB-INF/web.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>demo</display-name>
<listener>
<display-name>DemoInitListener</display-name>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#Generated by Maven Integration for Eclipse
#Sun Jun 19 10:26:39 CST 2016
#Sun Jul 10 14:49:35 CST 2016
version=1
groupId=wxtools.demo
m2e.projectName=demo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,17 @@
</dependencies>
<build>
<finalName>demo</finalName>
<plugins>
<!-- define the project compile level -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

/**
* <pre>
* 默认消息重复检查器
* 默认消息重试检查器
* 将每个消息id保存在内存里,每隔5秒清理已经过期的消息id,每个消息id的过期时间是15秒
* </pre>
* @author antgan
Expand Down Expand Up @@ -74,7 +74,7 @@ public void run() {
Long now = System.currentTimeMillis();
for (Map.Entry<String, Long> entry : msgId2Timestamp.entrySet()) {
if (now - entry.getValue() > timeToLive) {
msgId2Timestamp.entrySet().remove(entry);
msgId2Timestamp.entrySet().remove(entry);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ public WxMessageRouterRule rule() {
*/
public WxXmlOutMessage route(final WxXmlMessage wxMessage) {
if (isDuplicateMessage(wxMessage)) {
System.out.println("重复消息");
// 如果是重复消息,那么就不做处理
return null;
}
Expand Down Expand Up @@ -157,15 +158,15 @@ public void run() {
}

/**
* 验证是否重复消息
* 验证重试消息
* @param wxMessage
* @return
*/
protected boolean isDuplicateMessage(WxXmlMessage wxMessage) {
String messageId = "";
if (wxMessage.getMsgId() == null) {
messageId = String.valueOf(wxMessage.getCreateTime()) + "-"
+ wxMessage.getFromUserName() + "-"
messageId = String.valueOf(wxMessage.getToUserName()) + "-"
+ wxMessage.getFromUserName() + "-"+wxMessage.getContent()+"-"
+ String.valueOf(wxMessage.getEventKey() == null ? "" : wxMessage.getEventKey()) + "-"
+ String.valueOf(wxMessage.getEvent() == null ? "" : wxMessage.getEvent());
} else {
Expand Down
1 change: 1 addition & 0 deletions java/src/main/webapp/WEB-INF/web.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>wx-tools</display-name>

</web-app>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#Generated by Maven Integration for Eclipse
#Sun Jun 19 10:26:42 CST 2016
#Sun Jul 10 14:49:38 CST 2016
version=1.0-SNAPSHOT
groupId=com.soecode.wx-tools
m2e.projectName=wx-tools
Expand Down
Binary file renamed lib/wx-tools-1.0.0.jar → lib/wx-tools-1.0.1.jar
Binary file not shown.

0 comments on commit 3d9d47c

Please sign in to comment.