diff --git a/weixin4j-example/pom.xml b/weixin4j-example/pom.xml
index a8ce7a5d..d3bae503 100644
--- a/weixin4j-example/pom.xml
+++ b/weixin4j-example/pom.xml
@@ -7,6 +7,7 @@
weixin4j
1.7.6
+ war
weixin4j-example
1.0
weixin4j-example
diff --git a/weixin4j-example/src/main/java/com/foxinmy/weixin4j/example/server/Weixin4jServerStartupWithThread.java b/weixin4j-example/src/main/java/com/foxinmy/weixin4j/example/server/Weixin4jServerStartupWithThread.java
index 1838cc80..1bd271cd 100644
--- a/weixin4j-example/src/main/java/com/foxinmy/weixin4j/example/server/Weixin4jServerStartupWithThread.java
+++ b/weixin4j-example/src/main/java/com/foxinmy/weixin4j/example/server/Weixin4jServerStartupWithThread.java
@@ -1,8 +1,5 @@
package com.foxinmy.weixin4j.example.server;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
-
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
@@ -19,76 +16,71 @@ import io.netty.util.internal.logging.InternalLoggerFactory;
* 微信消息服务:需要另外开启一个线程去启动服务,这里值得注意的时:weixin4j-serve本身是作为一个单独的服务来启动的,可以不依赖Spring容器,
* 但考虑到目前都是Spring mvc的架构,这里就需要使用一个独立的线程去启动服务,其实本身没有使用spring mvc的API,
* 以后会考虑支持servlet api去集成不同的web框架。
- *
+ *
* @className Weixin4jServerStartupWithThread
* @author jinyu(foxinmy@gmail.com)
* @date 2015年5月7日
* @since JDK 1.6
*/
public class Weixin4jServerStartupWithThread implements ApplicationContextAware {
- /**
- * 服务监听的端口号,目前微信只支持80端口,可以考虑用nginx做转发到此端口
- */
- private final int port;
- /**
- * 服务器token信息
- */
- /**
- * 明文模式:String aesToken = ""; 密文模式:AesToken aesToken = new
- * AesToken("公众号appid", "公众号token","公众号加密/解密消息的密钥");
- */
- private final AesToken aesToken;
- /**
- * 处理微信消息的全限包名(也可通过addHandler方式一个一个添加)
- */
- private final String handlerPackage;
- /**
- * 用spring去获取bean
- */
- private ApplicationContext applicationContext;
+ /**
+ * 服务监听的端口号,目前微信只支持80端口,可以考虑用nginx做转发到此端口
+ */
+ private final int port;
+ /**
+ * 服务器token信息
+ */
+ /**
+ * 明文模式:String aesToken = ""; 密文模式:AesToken aesToken = new
+ * AesToken("公众号appid", "公众号token","公众号加密/解密消息的密钥");
+ */
+ private final AesToken aesToken;
+ /**
+ * 处理微信消息的全限包名(也可通过addHandler方式一个一个添加)
+ */
+ private final String handlerPackage;
+ /**
+ * 用spring去获取bean
+ */
+ private ApplicationContext applicationContext;
- private Weixin4jServerStartupWithThread(int port, AesToken aesToken,
- String handlerPackage) {
- this.port = port;
- this.aesToken = aesToken;
- this.handlerPackage = handlerPackage;
- }
+ private Weixin4jServerStartupWithThread(int port, AesToken aesToken, String handlerPackage) {
+ this.port = port;
+ this.aesToken = aesToken;
+ this.handlerPackage = handlerPackage;
+ }
- @Override
- public void setApplicationContext(ApplicationContext applicationContext)
- throws BeansException {
- this.applicationContext = applicationContext;
- }
+ @Override
+ public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
+ this.applicationContext = applicationContext;
+ }
- private ExecutorService executor;
+ private WeixinServerBootstrap bootstrap;
- /**
- * 启动函数
- *
- * @throws WeixinException
- */
- public void start() {
- executor = Executors.newCachedThreadPool();
- executor.execute(new Runnable() {
- @Override
- public void run() {
- try {
- new WeixinServerBootstrap(aesToken) // 指定开发者token信息。
- .handlerPackagesToScan(handlerPackage) // 扫描处理消息的包。
- .resolveBeanFactory(
- new SpringBeanFactory(applicationContext)) // 声明处理消息类由Spring容器去实例化。
- .addHandler(DebugMessageHandler.global) // 当没有匹配到消息处理时输出调试信息,开发环境打开。
- .openAlwaysResponse() // 当没有匹配到消息处理时输出空白回复(公众号不会出现「该公众号无法提供服务的提示」),正式环境打开。
- .startup(port); // 绑定服务的端口号,即对外暴露(微信服务器URL地址)的服务端口。
- } catch (WeixinException e) {
- InternalLoggerFactory.getInstance(getClass()).error(
- "weixin4j server startup:FAIL", e);
- }
- }
- });
- }
+ /**
+ * 启动函数
+ *
+ * @throws WeixinException
+ */
+ public void start() {
+ new Thread(new Runnable() {
+ @Override
+ public void run() {
+ try {
+ bootstrap = new WeixinServerBootstrap(aesToken) // 指定开发者token信息。
+ .handlerPackagesToScan(handlerPackage) // 扫描处理消息的包。
+ .resolveBeanFactory(new SpringBeanFactory(applicationContext)) // 声明处理消息类由Spring容器去实例化。
+ .addHandler(DebugMessageHandler.global) // 当没有匹配到消息处理时输出调试信息,开发环境打开。
+ .openAlwaysResponse(); // 当没有匹配到消息处理时输出空白回复(公众号不会出现「该公众号无法提供服务的提示」),正式环境打开。
+ bootstrap.startup(port); // 绑定服务的端口号,即对外暴露(微信服务器URL地址)的服务端口。
+ } catch (WeixinException e) {
+ InternalLoggerFactory.getInstance(getClass()).error("weixin4j server startup:FAIL", e);
+ }
+ }
+ }).start();
+ }
- public void stop() {
- executor.shutdown();
- }
+ public void stop() {
+ bootstrap.shutdown();
+ }
}
diff --git a/weixin4j-example/src/main/resources/spring-bean.xml b/weixin4j-example/src/main/resources/spring-bean.xml
index d0e809d4..53d6e2a7 100644
--- a/weixin4j-example/src/main/resources/spring-bean.xml
+++ b/weixin4j-example/src/main/resources/spring-bean.xml
@@ -5,8 +5,8 @@
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
-
+
-
+
\ No newline at end of file
diff --git a/weixin4j-server/src/main/java/com/foxinmy/weixin4j/socket/WeixinMessageDecoder.java b/weixin4j-server/src/main/java/com/foxinmy/weixin4j/socket/WeixinMessageDecoder.java
index 75b04fa1..8d7a2e33 100644
--- a/weixin4j-server/src/main/java/com/foxinmy/weixin4j/socket/WeixinMessageDecoder.java
+++ b/weixin4j-server/src/main/java/com/foxinmy/weixin4j/socket/WeixinMessageDecoder.java
@@ -1,14 +1,5 @@
package com.foxinmy.weixin4j.socket;
-import io.netty.channel.ChannelHandler;
-import io.netty.channel.ChannelHandlerContext;
-import io.netty.handler.codec.MessageToMessageDecoder;
-import io.netty.handler.codec.http.FullHttpRequest;
-import io.netty.handler.codec.http.HttpMethod;
-import io.netty.handler.codec.http.QueryStringDecoder;
-import io.netty.util.internal.logging.InternalLogger;
-import io.netty.util.internal.logging.InternalLoggerFactory;
-
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
@@ -22,93 +13,78 @@ import com.foxinmy.weixin4j.util.MessageUtil;
import com.foxinmy.weixin4j.util.ServerToolkits;
import com.foxinmy.weixin4j.xml.EncryptMessageHandler;
+import io.netty.channel.ChannelHandler;
+import io.netty.channel.ChannelHandlerContext;
+import io.netty.handler.codec.MessageToMessageDecoder;
+import io.netty.handler.codec.http.FullHttpRequest;
+import io.netty.handler.codec.http.HttpMethod;
+import io.netty.handler.codec.http.QueryStringDecoder;
+import io.netty.util.internal.logging.InternalLogger;
+import io.netty.util.internal.logging.InternalLoggerFactory;
+
/**
* 微信消息解码类
- *
+ *
* @className WeixinMessageDecoder
* @author jinyu(foxinmy@gmail.com)
* @date 2014年11月13日
* @since JDK 1.6
- * @see 加密接入指引
+ * @see 加密接入指引
* @see com.foxinmy.weixin4j.request.WeixinRequest
*/
@ChannelHandler.Sharable
-public class WeixinMessageDecoder extends
- MessageToMessageDecoder {
- private final InternalLogger logger = InternalLoggerFactory
- .getInstance(getClass());
+public class WeixinMessageDecoder extends MessageToMessageDecoder {
+ private final InternalLogger logger = InternalLoggerFactory.getInstance(getClass());
- private Map aesTokenMap = new ConcurrentHashMap();
+ private Map aesTokenMap = new ConcurrentHashMap();
- public WeixinMessageDecoder(final Map aesTokenMap) {
- for (Entry entry : aesTokenMap.entrySet()) {
- this.aesTokenMap.put(entry.getKey() == null ? "" : entry.getKey(),
- entry.getValue());
- }
- }
+ public WeixinMessageDecoder(final Map aesTokenMap) {
+ for (Entry entry : aesTokenMap.entrySet()) {
+ this.aesTokenMap.put(entry.getKey() == null ? "" : entry.getKey(), entry.getValue());
+ }
+ }
- public int addAesToken(final AesToken asetoken) {
- AesToken token = aesTokenMap.get(asetoken.getWeixinId());
- if (token != null)
- return -1;
- aesTokenMap.put(asetoken.getWeixinId(), asetoken);
- return 0;
- }
+ public void addAesToken(final AesToken asetoken) {
+ aesTokenMap.put(asetoken.getWeixinId(), asetoken);
+ }
- @Override
- protected void decode(ChannelHandlerContext ctx, FullHttpRequest req,
- List