bug 120
This commit is contained in:
parent
6464ba8842
commit
4c0f96fcfe
@ -7,6 +7,7 @@
|
|||||||
<artifactId>weixin4j</artifactId>
|
<artifactId>weixin4j</artifactId>
|
||||||
<version>1.7.6</version>
|
<version>1.7.6</version>
|
||||||
</parent>
|
</parent>
|
||||||
|
<packaging>war</packaging>
|
||||||
<artifactId>weixin4j-example</artifactId>
|
<artifactId>weixin4j-example</artifactId>
|
||||||
<version>1.0</version>
|
<version>1.0</version>
|
||||||
<name>weixin4j-example</name>
|
<name>weixin4j-example</name>
|
||||||
|
|||||||
@ -1,8 +1,5 @@
|
|||||||
package com.foxinmy.weixin4j.example.server;
|
package com.foxinmy.weixin4j.example.server;
|
||||||
|
|
||||||
import java.util.concurrent.ExecutorService;
|
|
||||||
import java.util.concurrent.Executors;
|
|
||||||
|
|
||||||
import org.springframework.beans.BeansException;
|
import org.springframework.beans.BeansException;
|
||||||
import org.springframework.context.ApplicationContext;
|
import org.springframework.context.ApplicationContext;
|
||||||
import org.springframework.context.ApplicationContextAware;
|
import org.springframework.context.ApplicationContextAware;
|
||||||
@ -47,20 +44,18 @@ public class Weixin4jServerStartupWithThread implements ApplicationContextAware
|
|||||||
*/
|
*/
|
||||||
private ApplicationContext applicationContext;
|
private ApplicationContext applicationContext;
|
||||||
|
|
||||||
private Weixin4jServerStartupWithThread(int port, AesToken aesToken,
|
private Weixin4jServerStartupWithThread(int port, AesToken aesToken, String handlerPackage) {
|
||||||
String handlerPackage) {
|
|
||||||
this.port = port;
|
this.port = port;
|
||||||
this.aesToken = aesToken;
|
this.aesToken = aesToken;
|
||||||
this.handlerPackage = handlerPackage;
|
this.handlerPackage = handlerPackage;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setApplicationContext(ApplicationContext applicationContext)
|
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
||||||
throws BeansException {
|
|
||||||
this.applicationContext = applicationContext;
|
this.applicationContext = applicationContext;
|
||||||
}
|
}
|
||||||
|
|
||||||
private ExecutorService executor;
|
private WeixinServerBootstrap bootstrap;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 启动函数
|
* 启动函数
|
||||||
@ -68,27 +63,24 @@ public class Weixin4jServerStartupWithThread implements ApplicationContextAware
|
|||||||
* @throws WeixinException
|
* @throws WeixinException
|
||||||
*/
|
*/
|
||||||
public void start() {
|
public void start() {
|
||||||
executor = Executors.newCachedThreadPool();
|
new Thread(new Runnable() {
|
||||||
executor.execute(new Runnable() {
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
try {
|
try {
|
||||||
new WeixinServerBootstrap(aesToken) // 指定开发者token信息。
|
bootstrap = new WeixinServerBootstrap(aesToken) // 指定开发者token信息。
|
||||||
.handlerPackagesToScan(handlerPackage) // 扫描处理消息的包。
|
.handlerPackagesToScan(handlerPackage) // 扫描处理消息的包。
|
||||||
.resolveBeanFactory(
|
.resolveBeanFactory(new SpringBeanFactory(applicationContext)) // 声明处理消息类由Spring容器去实例化。
|
||||||
new SpringBeanFactory(applicationContext)) // 声明处理消息类由Spring容器去实例化。
|
|
||||||
.addHandler(DebugMessageHandler.global) // 当没有匹配到消息处理时输出调试信息,开发环境打开。
|
.addHandler(DebugMessageHandler.global) // 当没有匹配到消息处理时输出调试信息,开发环境打开。
|
||||||
.openAlwaysResponse() // 当没有匹配到消息处理时输出空白回复(公众号不会出现「该公众号无法提供服务的提示」),正式环境打开。
|
.openAlwaysResponse(); // 当没有匹配到消息处理时输出空白回复(公众号不会出现「该公众号无法提供服务的提示」),正式环境打开。
|
||||||
.startup(port); // 绑定服务的端口号,即对外暴露(微信服务器URL地址)的服务端口。
|
bootstrap.startup(port); // 绑定服务的端口号,即对外暴露(微信服务器URL地址)的服务端口。
|
||||||
} catch (WeixinException e) {
|
} catch (WeixinException e) {
|
||||||
InternalLoggerFactory.getInstance(getClass()).error(
|
InternalLoggerFactory.getInstance(getClass()).error("weixin4j server startup:FAIL", e);
|
||||||
"weixin4j server startup:FAIL", e);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
}).start();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void stop() {
|
public void stop() {
|
||||||
executor.shutdown();
|
bootstrap.shutdown();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,14 +1,5 @@
|
|||||||
package com.foxinmy.weixin4j.socket;
|
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.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Map.Entry;
|
import java.util.Map.Entry;
|
||||||
@ -22,6 +13,15 @@ import com.foxinmy.weixin4j.util.MessageUtil;
|
|||||||
import com.foxinmy.weixin4j.util.ServerToolkits;
|
import com.foxinmy.weixin4j.util.ServerToolkits;
|
||||||
import com.foxinmy.weixin4j.xml.EncryptMessageHandler;
|
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;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 微信消息解码类
|
* 微信消息解码类
|
||||||
*
|
*
|
||||||
@ -29,84 +29,60 @@ import com.foxinmy.weixin4j.xml.EncryptMessageHandler;
|
|||||||
* @author jinyu(foxinmy@gmail.com)
|
* @author jinyu(foxinmy@gmail.com)
|
||||||
* @date 2014年11月13日
|
* @date 2014年11月13日
|
||||||
* @since JDK 1.6
|
* @since JDK 1.6
|
||||||
* @see <a
|
* @see <a href=
|
||||||
* href="http://mp.weixin.qq.com/wiki/0/61c3a8b9d50ac74f18bdf2e54ddfc4e0.html">加密接入指引</a>
|
* "http://mp.weixin.qq.com/wiki/0/61c3a8b9d50ac74f18bdf2e54ddfc4e0.html">加密接入指引</a>
|
||||||
* @see com.foxinmy.weixin4j.request.WeixinRequest
|
* @see com.foxinmy.weixin4j.request.WeixinRequest
|
||||||
*/
|
*/
|
||||||
@ChannelHandler.Sharable
|
@ChannelHandler.Sharable
|
||||||
public class WeixinMessageDecoder extends
|
public class WeixinMessageDecoder extends MessageToMessageDecoder<FullHttpRequest> {
|
||||||
MessageToMessageDecoder<FullHttpRequest> {
|
private final InternalLogger logger = InternalLoggerFactory.getInstance(getClass());
|
||||||
private final InternalLogger logger = InternalLoggerFactory
|
|
||||||
.getInstance(getClass());
|
|
||||||
|
|
||||||
private Map<String, AesToken> aesTokenMap = new ConcurrentHashMap<String, AesToken>();
|
private Map<String, AesToken> aesTokenMap = new ConcurrentHashMap<String, AesToken>();
|
||||||
|
|
||||||
public WeixinMessageDecoder(final Map<String, AesToken> aesTokenMap) {
|
public WeixinMessageDecoder(final Map<String, AesToken> aesTokenMap) {
|
||||||
for (Entry<String, AesToken> entry : aesTokenMap.entrySet()) {
|
for (Entry<String, AesToken> entry : aesTokenMap.entrySet()) {
|
||||||
this.aesTokenMap.put(entry.getKey() == null ? "" : entry.getKey(),
|
this.aesTokenMap.put(entry.getKey() == null ? "" : entry.getKey(), entry.getValue());
|
||||||
entry.getValue());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public int addAesToken(final AesToken asetoken) {
|
public void addAesToken(final AesToken asetoken) {
|
||||||
AesToken token = aesTokenMap.get(asetoken.getWeixinId());
|
|
||||||
if (token != null)
|
|
||||||
return -1;
|
|
||||||
aesTokenMap.put(asetoken.getWeixinId(), asetoken);
|
aesTokenMap.put(asetoken.getWeixinId(), asetoken);
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void decode(ChannelHandlerContext ctx, FullHttpRequest req,
|
protected void decode(ChannelHandlerContext ctx, FullHttpRequest req, List<Object> out) throws WeixinException {
|
||||||
List<Object> out) throws WeixinException {
|
|
||||||
String messageContent = req.content().toString(ServerToolkits.UTF_8);
|
String messageContent = req.content().toString(ServerToolkits.UTF_8);
|
||||||
QueryStringDecoder queryDecoder = new QueryStringDecoder(req.uri(),
|
QueryStringDecoder queryDecoder = new QueryStringDecoder(req.uri(), true);
|
||||||
true);
|
|
||||||
HttpMethod method = req.method();
|
HttpMethod method = req.method();
|
||||||
logger.info("decode request:{} use {} method invoking", req.uri(),
|
logger.info("decode request:{} use {} method invoking", req.uri(), method);
|
||||||
method);
|
|
||||||
Map<String, List<String>> parameters = queryDecoder.parameters();
|
Map<String, List<String>> parameters = queryDecoder.parameters();
|
||||||
EncryptType encryptType = parameters.containsKey("encrypt_type") ? EncryptType
|
EncryptType encryptType = parameters.containsKey("encrypt_type")
|
||||||
.valueOf(parameters.get("encrypt_type").get(0).toUpperCase())
|
? EncryptType.valueOf(parameters.get("encrypt_type").get(0).toUpperCase()) : EncryptType.RAW;
|
||||||
: EncryptType.RAW;
|
String echoStr = parameters.containsKey("echostr") ? parameters.get("echostr").get(0) : "";
|
||||||
String echoStr = parameters.containsKey("echostr") ? parameters.get(
|
String timeStamp = parameters.containsKey("timestamp") ? parameters.get("timestamp").get(0) : "";
|
||||||
"echostr").get(0) : "";
|
String nonce = parameters.containsKey("nonce") ? parameters.get("nonce").get(0) : "";
|
||||||
String timeStamp = parameters.containsKey("timestamp") ? parameters
|
String signature = parameters.containsKey("signature") ? parameters.get("signature").get(0) : "";
|
||||||
.get("timestamp").get(0) : "";
|
String msgSignature = parameters.containsKey("msg_signature") ? parameters.get("msg_signature").get(0) : "";
|
||||||
String nonce = parameters.containsKey("nonce") ? parameters
|
String weixinId = parameters.containsKey("weixin_id") ? parameters.get("weixin_id").get(0) : "";
|
||||||
.get("nonce").get(0) : "";
|
|
||||||
String signature = parameters.containsKey("signature") ? parameters
|
|
||||||
.get("signature").get(0) : "";
|
|
||||||
String msgSignature = parameters.containsKey("msg_signature") ? parameters
|
|
||||||
.get("msg_signature").get(0) : "";
|
|
||||||
String weixinId = parameters.containsKey("weixin_id") ? parameters.get(
|
|
||||||
"weixin_id").get(0) : "";
|
|
||||||
AesToken aesToken = aesTokenMap.get(weixinId);
|
AesToken aesToken = aesTokenMap.get(weixinId);
|
||||||
String encryptContent = null;
|
String encryptContent = null;
|
||||||
if (!ServerToolkits.isBlank(messageContent)
|
if (!ServerToolkits.isBlank(messageContent) && encryptType == EncryptType.AES) {
|
||||||
&& encryptType == EncryptType.AES) {
|
|
||||||
if (ServerToolkits.isBlank(aesToken.getAesKey())) {
|
if (ServerToolkits.isBlank(aesToken.getAesKey())) {
|
||||||
throw new WeixinException(
|
throw new WeixinException("EncodingAESKey not be empty in safety(AES) mode");
|
||||||
"EncodingAESKey not be empty in safety(AES) mode");
|
|
||||||
}
|
}
|
||||||
EncryptMessageHandler encryptHandler = EncryptMessageHandler
|
EncryptMessageHandler encryptHandler = EncryptMessageHandler.parser(messageContent);
|
||||||
.parser(messageContent);
|
|
||||||
encryptContent = encryptHandler.getEncryptContent();
|
encryptContent = encryptHandler.getEncryptContent();
|
||||||
/**
|
/**
|
||||||
* 企业号第三方套件 ╮(╯_╰)╭
|
* 企业号第三方套件 ╮(╯_╰)╭
|
||||||
*/
|
*/
|
||||||
if (aesToken.getWeixinId().startsWith("tj")) {
|
if (aesToken.getWeixinId().startsWith("tj")) {
|
||||||
aesToken = new AesToken(encryptHandler.getToUserName(),
|
aesToken = new AesToken(encryptHandler.getToUserName(), aesToken.getToken(), aesToken.getAesKey());
|
||||||
aesToken.getToken(), aesToken.getAesKey());
|
|
||||||
}
|
}
|
||||||
messageContent = MessageUtil.aesDecrypt(aesToken.getWeixinId(),
|
messageContent = MessageUtil.aesDecrypt(aesToken.getWeixinId(), aesToken.getAesKey(), encryptContent);
|
||||||
aesToken.getAesKey(), encryptContent);
|
|
||||||
}
|
}
|
||||||
logger.info("read original message {}", messageContent);
|
logger.info("read original message {}", messageContent);
|
||||||
WeixinRequest request = new WeixinRequest(req.headers(), method,
|
WeixinRequest request = new WeixinRequest(req.headers(), method, req.uri(), encryptType, echoStr, timeStamp,
|
||||||
req.uri(), encryptType, echoStr, timeStamp, nonce,
|
nonce, signature, msgSignature, messageContent, encryptContent, aesToken);
|
||||||
signature, msgSignature, messageContent, encryptContent,
|
|
||||||
aesToken);
|
|
||||||
request.setDecoderResult(req.decoderResult());
|
request.setDecoderResult(req.decoderResult());
|
||||||
request.setProtocolVersion(req.protocolVersion());
|
request.setProtocolVersion(req.protocolVersion());
|
||||||
out.add(request);
|
out.add(request);
|
||||||
|
|||||||
@ -1,16 +1,16 @@
|
|||||||
package com.foxinmy.weixin4j.socket;
|
package com.foxinmy.weixin4j.socket;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import com.foxinmy.weixin4j.dispatcher.WeixinMessageDispatcher;
|
||||||
|
import com.foxinmy.weixin4j.util.AesToken;
|
||||||
|
|
||||||
import io.netty.channel.ChannelInitializer;
|
import io.netty.channel.ChannelInitializer;
|
||||||
import io.netty.channel.ChannelPipeline;
|
import io.netty.channel.ChannelPipeline;
|
||||||
import io.netty.channel.socket.SocketChannel;
|
import io.netty.channel.socket.SocketChannel;
|
||||||
import io.netty.handler.codec.http.HttpObjectAggregator;
|
import io.netty.handler.codec.http.HttpObjectAggregator;
|
||||||
import io.netty.handler.codec.http.HttpServerCodec;
|
import io.netty.handler.codec.http.HttpServerCodec;
|
||||||
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import com.foxinmy.weixin4j.dispatcher.WeixinMessageDispatcher;
|
|
||||||
import com.foxinmy.weixin4j.util.AesToken;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 微信消息服务器初始化
|
* 微信消息服务器初始化
|
||||||
*
|
*
|
||||||
@ -25,14 +25,13 @@ public class WeixinServerInitializer extends ChannelInitializer<SocketChannel> {
|
|||||||
private final WeixinMessageDispatcher messageDispatcher;
|
private final WeixinMessageDispatcher messageDispatcher;
|
||||||
private final WeixinMessageDecoder messageDecoder;
|
private final WeixinMessageDecoder messageDecoder;
|
||||||
|
|
||||||
public WeixinServerInitializer(Map<String, AesToken> aesTokenMap,
|
public WeixinServerInitializer(Map<String, AesToken> aesTokenMap, WeixinMessageDispatcher messageDispatcher) {
|
||||||
WeixinMessageDispatcher messageDispatcher) {
|
|
||||||
this.messageDispatcher = messageDispatcher;
|
this.messageDispatcher = messageDispatcher;
|
||||||
this.messageDecoder = new WeixinMessageDecoder(aesTokenMap);
|
this.messageDecoder = new WeixinMessageDecoder(aesTokenMap);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int addAesToken(final AesToken asetoken) {
|
public void addAesToken(AesToken asetoken) {
|
||||||
return messageDecoder.addAesToken(asetoken);
|
messageDecoder.addAesToken(asetoken);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@ -1,17 +1,5 @@
|
|||||||
package com.foxinmy.weixin4j.startup;
|
package com.foxinmy.weixin4j.startup;
|
||||||
|
|
||||||
import io.netty.bootstrap.ServerBootstrap;
|
|
||||||
import io.netty.channel.Channel;
|
|
||||||
import io.netty.channel.ChannelOption;
|
|
||||||
import io.netty.channel.EventLoopGroup;
|
|
||||||
import io.netty.channel.nio.NioEventLoopGroup;
|
|
||||||
import io.netty.channel.socket.nio.NioServerSocketChannel;
|
|
||||||
import io.netty.handler.logging.LoggingHandler;
|
|
||||||
import io.netty.util.concurrent.Future;
|
|
||||||
import io.netty.util.concurrent.FutureListener;
|
|
||||||
import io.netty.util.internal.logging.InternalLogger;
|
|
||||||
import io.netty.util.internal.logging.InternalLoggerFactory;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
@ -30,6 +18,18 @@ import com.foxinmy.weixin4j.request.WeixinMessage;
|
|||||||
import com.foxinmy.weixin4j.socket.WeixinServerInitializer;
|
import com.foxinmy.weixin4j.socket.WeixinServerInitializer;
|
||||||
import com.foxinmy.weixin4j.util.AesToken;
|
import com.foxinmy.weixin4j.util.AesToken;
|
||||||
|
|
||||||
|
import io.netty.bootstrap.ServerBootstrap;
|
||||||
|
import io.netty.bootstrap.ServerBootstrapConfig;
|
||||||
|
import io.netty.channel.Channel;
|
||||||
|
import io.netty.channel.ChannelOption;
|
||||||
|
import io.netty.channel.nio.NioEventLoopGroup;
|
||||||
|
import io.netty.channel.socket.nio.NioServerSocketChannel;
|
||||||
|
import io.netty.handler.logging.LoggingHandler;
|
||||||
|
import io.netty.util.concurrent.Future;
|
||||||
|
import io.netty.util.concurrent.FutureListener;
|
||||||
|
import io.netty.util.internal.logging.InternalLogger;
|
||||||
|
import io.netty.util.internal.logging.InternalLoggerFactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 微信netty服务启动程序
|
* 微信netty服务启动程序
|
||||||
*
|
*
|
||||||
@ -45,8 +45,7 @@ import com.foxinmy.weixin4j.util.AesToken;
|
|||||||
*/
|
*/
|
||||||
public final class WeixinServerBootstrap {
|
public final class WeixinServerBootstrap {
|
||||||
|
|
||||||
private final InternalLogger logger = InternalLoggerFactory
|
private final InternalLogger logger = InternalLoggerFactory.getInstance(getClass());
|
||||||
.getInstance(getClass());
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* boss线程数,默认设置为cpu的核数
|
* boss线程数,默认设置为cpu的核数
|
||||||
@ -79,7 +78,7 @@ public final class WeixinServerBootstrap {
|
|||||||
*/
|
*/
|
||||||
private final Map<String, AesToken> aesTokenMap;
|
private final Map<String, AesToken> aesTokenMap;
|
||||||
|
|
||||||
private WeixinServerInitializer wechatInitializer;
|
private ServerBootstrap bootstrap;
|
||||||
|
|
||||||
static {
|
static {
|
||||||
DEFAULT_BOSSTHREADS = Runtime.getRuntime().availableProcessors();
|
DEFAULT_BOSSTHREADS = Runtime.getRuntime().availableProcessors();
|
||||||
@ -101,8 +100,8 @@ public final class WeixinServerBootstrap {
|
|||||||
/**
|
/**
|
||||||
* 明文模式 & 兼容模式 & 密文模式
|
* 明文模式 & 兼容模式 & 密文模式
|
||||||
* <dl>
|
* <dl>
|
||||||
* <font
|
* <font color=
|
||||||
* color="red">值得注意的是:企业号服务时需要在服务器URL后面加多一个`encrypt_type=aes`的参数</font>
|
* "red">值得注意的是:企业号服务时需要在服务器URL后面加多一个`encrypt_type=aes`的参数</font>
|
||||||
* </dl>
|
* </dl>
|
||||||
*
|
*
|
||||||
* @param weixinId
|
* @param weixinId
|
||||||
@ -118,13 +117,14 @@ public final class WeixinServerBootstrap {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 多个公众号的支持 <dt>值得注意的是:
|
* 多个公众号的支持
|
||||||
|
* <dt>值得注意的是:
|
||||||
* <dl>
|
* <dl>
|
||||||
* <font color="red">1).企业号服务时需要在服务器URL后面加多一个`encrypt_type=aes`的参数</font>
|
* <font color="red">1).企业号服务时需要在服务器URL后面加多一个`encrypt_type=aes`的参数</font>
|
||||||
* </dl>
|
* </dl>
|
||||||
* <dl>
|
* <dl>
|
||||||
* <font
|
* <font color=
|
||||||
* color="red">2).非明文模式下需要在服务器URL后面加多一个`weixin_id=对应的appid/corpid`的参数</font>
|
* "red">2).非明文模式下需要在服务器URL后面加多一个`weixin_id=对应的appid/corpid`的参数</font>
|
||||||
* </dl>
|
* </dl>
|
||||||
*
|
*
|
||||||
* @param aesTokens
|
* @param aesTokens
|
||||||
@ -136,13 +136,14 @@ public final class WeixinServerBootstrap {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 多个公众号的支持 <dt>值得注意的是:
|
* 多个公众号的支持
|
||||||
|
* <dt>值得注意的是:
|
||||||
* <dl>
|
* <dl>
|
||||||
* <font color="red">1).企业号服务时需要在服务器URL后面加多一个`encrypt_type=aes`的参数</font>
|
* <font color="red">1).企业号服务时需要在服务器URL后面加多一个`encrypt_type=aes`的参数</font>
|
||||||
* </dl>
|
* </dl>
|
||||||
* <dl>
|
* <dl>
|
||||||
* <font
|
* <font color=
|
||||||
* color="red">2).非明文模式下需要在服务器URL后面加多一个`weixin_id=对应的appid/corpid`的参数</font>
|
* "red">2).非明文模式下需要在服务器URL后面加多一个`weixin_id=对应的appid/corpid`的参数</font>
|
||||||
* </dl>
|
* </dl>
|
||||||
*
|
*
|
||||||
* @param messageMatcher
|
* @param messageMatcher
|
||||||
@ -151,8 +152,7 @@ public final class WeixinServerBootstrap {
|
|||||||
* 公众号信息
|
* 公众号信息
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public WeixinServerBootstrap(WeixinMessageMatcher messageMatcher,
|
public WeixinServerBootstrap(WeixinMessageMatcher messageMatcher, AesToken... aesTokens) {
|
||||||
AesToken... aesTokens) {
|
|
||||||
if (messageMatcher == null) {
|
if (messageMatcher == null) {
|
||||||
throw new IllegalArgumentException("MessageMatcher not be null");
|
throw new IllegalArgumentException("MessageMatcher not be null");
|
||||||
}
|
}
|
||||||
@ -197,33 +197,22 @@ public final class WeixinServerBootstrap {
|
|||||||
* @return
|
* @return
|
||||||
* @throws WeixinException
|
* @throws WeixinException
|
||||||
*/
|
*/
|
||||||
public void startup(int bossThreads, int workerThreads, final int serverPort)
|
public void startup(int bossThreads, int workerThreads, final int serverPort) throws WeixinException {
|
||||||
throws WeixinException {
|
|
||||||
messageDispatcher.setMessageHandlerList(messageHandlerList);
|
messageDispatcher.setMessageHandlerList(messageHandlerList);
|
||||||
messageDispatcher.setMessageInterceptorList(messageInterceptorList);
|
messageDispatcher.setMessageInterceptorList(messageInterceptorList);
|
||||||
|
|
||||||
EventLoopGroup bossGroup = new NioEventLoopGroup(bossThreads);
|
|
||||||
EventLoopGroup workerGroup = new NioEventLoopGroup(workerThreads);
|
|
||||||
try {
|
try {
|
||||||
wechatInitializer = new WeixinServerInitializer(aesTokenMap,
|
bootstrap = new ServerBootstrap();
|
||||||
messageDispatcher);
|
bootstrap.option(ChannelOption.SO_BACKLOG, 1024);
|
||||||
ServerBootstrap b = new ServerBootstrap();
|
bootstrap.group(new NioEventLoopGroup(bossThreads), new NioEventLoopGroup(workerThreads))
|
||||||
b.option(ChannelOption.SO_BACKLOG, 1024);
|
.channel(NioServerSocketChannel.class).handler(new LoggingHandler())
|
||||||
b.group(bossGroup, workerGroup)
|
.childHandler(new WeixinServerInitializer(aesTokenMap, messageDispatcher));
|
||||||
.channel(NioServerSocketChannel.class)
|
Channel ch = bootstrap.bind(serverPort).addListener(new FutureListener<Void>() {
|
||||||
.handler(new LoggingHandler())
|
|
||||||
.childHandler(wechatInitializer);
|
|
||||||
Channel ch = b.bind(serverPort)
|
|
||||||
.addListener(new FutureListener<Void>() {
|
|
||||||
@Override
|
@Override
|
||||||
public void operationComplete(Future<Void> future)
|
public void operationComplete(Future<Void> future) throws Exception {
|
||||||
throws Exception {
|
|
||||||
if (future.isSuccess()) {
|
if (future.isSuccess()) {
|
||||||
logger.info("weixin4j server startup OK:{}",
|
logger.info("weixin4j server startup OK:{}", serverPort);
|
||||||
serverPort);
|
|
||||||
} else {
|
} else {
|
||||||
logger.info("weixin4j server startup FAIL:{}",
|
logger.info("weixin4j server startup FAIL:{}", serverPort);
|
||||||
serverPort);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}).sync().channel();
|
}).sync().channel();
|
||||||
@ -231,11 +220,24 @@ public final class WeixinServerBootstrap {
|
|||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
throw new WeixinException("netty server startup FAIL", e);
|
throw new WeixinException("netty server startup FAIL", e);
|
||||||
} finally {
|
} finally {
|
||||||
bossGroup.shutdownGracefully();
|
shutdown();
|
||||||
workerGroup.shutdownGracefully();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean shutdown() {
|
||||||
|
if (bootstrap == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
ServerBootstrapConfig c = bootstrap.config();
|
||||||
|
c.group().shutdownGracefully();
|
||||||
|
c.childGroup().shutdownGracefully();
|
||||||
|
messageHandlerList = null;
|
||||||
|
messageInterceptorList = null;
|
||||||
|
messageDispatcher = null;
|
||||||
|
bootstrap = null;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 添加一个或者多个消息处理器
|
* 添加一个或者多个消息处理器
|
||||||
*
|
*
|
||||||
@ -243,8 +245,7 @@ public final class WeixinServerBootstrap {
|
|||||||
* 消息处理器
|
* 消息处理器
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public WeixinServerBootstrap addHandler(
|
public WeixinServerBootstrap addHandler(WeixinMessageHandler... messageHandler) {
|
||||||
WeixinMessageHandler... messageHandler) {
|
|
||||||
if (messageHandler == null) {
|
if (messageHandler == null) {
|
||||||
throw new IllegalArgumentException("messageHandler not be null");
|
throw new IllegalArgumentException("messageHandler not be null");
|
||||||
}
|
}
|
||||||
@ -259,8 +260,7 @@ public final class WeixinServerBootstrap {
|
|||||||
* 消息拦截器
|
* 消息拦截器
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public WeixinServerBootstrap addInterceptor(
|
public WeixinServerBootstrap addInterceptor(WeixinMessageInterceptor... messageInterceptor) {
|
||||||
WeixinMessageInterceptor... messageInterceptor) {
|
|
||||||
if (messageInterceptor == null) {
|
if (messageInterceptor == null) {
|
||||||
throw new IllegalArgumentException("messageInterceptor not be null");
|
throw new IllegalArgumentException("messageInterceptor not be null");
|
||||||
}
|
}
|
||||||
@ -275,11 +275,9 @@ public final class WeixinServerBootstrap {
|
|||||||
* 消息处理器所在的包名
|
* 消息处理器所在的包名
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public WeixinServerBootstrap handlerPackagesToScan(
|
public WeixinServerBootstrap handlerPackagesToScan(String... messageHandlerPackages) {
|
||||||
String... messageHandlerPackages) {
|
|
||||||
if (messageHandlerPackages == null) {
|
if (messageHandlerPackages == null) {
|
||||||
throw new IllegalArgumentException(
|
throw new IllegalArgumentException("messageHandlerPackages not be null");
|
||||||
"messageHandlerPackages not be null");
|
|
||||||
}
|
}
|
||||||
messageDispatcher.setMessageHandlerPackages(messageHandlerPackages);
|
messageDispatcher.setMessageHandlerPackages(messageHandlerPackages);
|
||||||
return this;
|
return this;
|
||||||
@ -292,14 +290,11 @@ public final class WeixinServerBootstrap {
|
|||||||
* 消息拦截器所在的包名
|
* 消息拦截器所在的包名
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public WeixinServerBootstrap interceptorPackagesToScan(
|
public WeixinServerBootstrap interceptorPackagesToScan(String... messageInterceptorPackages) {
|
||||||
String... messageInterceptorPackages) {
|
|
||||||
if (messageInterceptorPackages == null) {
|
if (messageInterceptorPackages == null) {
|
||||||
throw new IllegalArgumentException(
|
throw new IllegalArgumentException("messageInterceptorPackages not be null");
|
||||||
"messageInterceptorPackages not be null");
|
|
||||||
}
|
}
|
||||||
messageDispatcher
|
messageDispatcher.setMessageInterceptorPackages(messageInterceptorPackages);
|
||||||
.setMessageInterceptorPackages(messageInterceptorPackages);
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -324,8 +319,7 @@ public final class WeixinServerBootstrap {
|
|||||||
* 消息类
|
* 消息类
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public WeixinServerBootstrap registMessageClass(
|
public WeixinServerBootstrap registMessageClass(WeixinMessageKey messageKey,
|
||||||
WeixinMessageKey messageKey,
|
|
||||||
Class<? extends WeixinMessage> messageClass) {
|
Class<? extends WeixinMessage> messageClass) {
|
||||||
messageDispatcher.registMessageClass(messageKey, messageClass);
|
messageDispatcher.registMessageClass(messageKey, messageClass);
|
||||||
return this;
|
return this;
|
||||||
@ -340,13 +334,18 @@ public final class WeixinServerBootstrap {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* aesTokenMap 最好是线程安全的
|
* 动态添加aesToken
|
||||||
*
|
*
|
||||||
* @param aesToken
|
* @param aesToken
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public int addAesToken(AesToken aesToken) {
|
public boolean addAesToken(AesToken aesToken) {
|
||||||
return wechatInitializer.addAesToken(aesToken);
|
if (bootstrap == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
ServerBootstrapConfig c = bootstrap.config();
|
||||||
|
((WeixinServerInitializer) c.childHandler()).addAesToken(aesToken);
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public final static String VERSION = "1.1.8";
|
public final static String VERSION = "1.1.8";
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user