添加@ChannelHandler.Sharable
This commit is contained in:
parent
e02b1da3af
commit
363aa4bc9b
@ -1,5 +1,6 @@
|
||||
package com.foxinmy.weixin4j.socket;
|
||||
|
||||
import io.netty.channel.ChannelHandler;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.handler.codec.MessageToMessageEncoder;
|
||||
import io.netty.util.internal.logging.InternalLogger;
|
||||
@ -21,6 +22,7 @@ import com.foxinmy.weixin4j.util.ServerToolkits;
|
||||
* @since JDK 1.6
|
||||
* @see com.foxinmy.weixin4j.response.SingleResponse
|
||||
*/
|
||||
@ChannelHandler.Sharable
|
||||
public class SingleResponseEncoder extends
|
||||
MessageToMessageEncoder<SingleResponse> {
|
||||
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
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;
|
||||
@ -31,6 +32,7 @@ import com.foxinmy.weixin4j.xml.EncryptMessageHandler;
|
||||
* href="http://mp.weixin.qq.com/wiki/0/61c3a8b9d50ac74f18bdf2e54ddfc4e0.html">加密接入指引</a>
|
||||
* @see com.foxinmy.weixin4j.request.WeixinRequest
|
||||
*/
|
||||
@ChannelHandler.Sharable
|
||||
public class WeixinMessageDecoder extends
|
||||
MessageToMessageDecoder<FullHttpRequest> {
|
||||
private final InternalLogger logger = InternalLoggerFactory
|
||||
@ -39,20 +41,18 @@ public class WeixinMessageDecoder extends
|
||||
private Map<String, AesToken> aesTokenMap = new ConcurrentHashMap<String, AesToken>();
|
||||
|
||||
public WeixinMessageDecoder(Map<String, AesToken> aesTokenMap) {
|
||||
//this.aesTokenMap = aesTokenMap;
|
||||
AesToken[]tokens = aesTokenMap.values().toArray(new AesToken[0]);
|
||||
for (AesToken token:tokens){
|
||||
// this.aesTokenMap = aesTokenMap;
|
||||
AesToken[] tokens = aesTokenMap.values().toArray(new AesToken[0]);
|
||||
for (AesToken token : tokens) {
|
||||
aesTokenMap.put(token.getWeixinId(), token);
|
||||
}
|
||||
}
|
||||
|
||||
public int addAesToken(final AesToken asetoken){
|
||||
public int addAesToken(final AesToken asetoken) {
|
||||
AesToken token = aesTokenMap.get(asetoken.getWeixinId());
|
||||
if (token != null)
|
||||
return -1;
|
||||
|
||||
aesTokenMap.put(asetoken.getWeixinId(), asetoken);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
package com.foxinmy.weixin4j.socket;
|
||||
|
||||
import io.netty.channel.ChannelHandler;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.handler.codec.MessageToMessageEncoder;
|
||||
import io.netty.util.internal.logging.InternalLogger;
|
||||
@ -26,6 +27,7 @@ import com.foxinmy.weixin4j.util.ServerToolkits;
|
||||
* href="http://mp.weixin.qq.com/wiki/0/61c3a8b9d50ac74f18bdf2e54ddfc4e0.html">加密接入指引</a>
|
||||
* @see com.foxinmy.weixin4j.response.WeixinResponse
|
||||
*/
|
||||
@ChannelHandler.Sharable
|
||||
public class WeixinResponseEncoder extends
|
||||
MessageToMessageEncoder<WeixinResponse> {
|
||||
|
||||
|
||||
@ -38,7 +38,6 @@ public class WeixinServerInitializer extends ChannelInitializer<SocketChannel> {
|
||||
|
||||
@Override
|
||||
protected void initChannel(SocketChannel channel) {
|
||||
|
||||
ChannelPipeline pipeline = channel.pipeline();
|
||||
pipeline.addLast(new HttpServerCodec());
|
||||
pipeline.addLast(new HttpObjectAggregator(65536));
|
||||
|
||||
@ -45,7 +45,8 @@ import com.foxinmy.weixin4j.util.AesToken;
|
||||
*/
|
||||
public final class WeixinServerBootstrap {
|
||||
|
||||
private final InternalLogger logger = InternalLoggerFactory.getInstance(getClass());
|
||||
private final InternalLogger logger = InternalLoggerFactory
|
||||
.getInstance(getClass());
|
||||
|
||||
/**
|
||||
* boss线程数,默认设置为cpu的核数
|
||||
@ -99,7 +100,10 @@ public final class WeixinServerBootstrap {
|
||||
|
||||
/**
|
||||
* 明文模式 & 兼容模式 & 密文模式
|
||||
* <dl><font color="red">值得注意的是:企业号服务时需要在服务器URL后面加多一个`encrypt_type=aes`的参数</font></dl>
|
||||
* <dl>
|
||||
* <font
|
||||
* color="red">值得注意的是:企业号服务时需要在服务器URL后面加多一个`encrypt_type=aes`的参数</font>
|
||||
* </dl>
|
||||
*
|
||||
* @param weixinId
|
||||
* 公众号的应用ID(appid/corpid) 密文&兼容模式下需要填写
|
||||
@ -114,10 +118,14 @@ public final class WeixinServerBootstrap {
|
||||
}
|
||||
|
||||
/**
|
||||
* 多个公众号的支持
|
||||
* <dt>值得注意的是:
|
||||
* <dl><font color="red">1).企业号服务时需要在服务器URL后面加多一个`encrypt_type=aes`的参数</font></dl>
|
||||
* <dl><font color="red">2).非明文模式下需要在服务器URL后面加多一个`weixin_id=对应的appid/corpid`的参数</font></dl>
|
||||
* 多个公众号的支持 <dt>值得注意的是:
|
||||
* <dl>
|
||||
* <font color="red">1).企业号服务时需要在服务器URL后面加多一个`encrypt_type=aes`的参数</font>
|
||||
* </dl>
|
||||
* <dl>
|
||||
* <font
|
||||
* color="red">2).非明文模式下需要在服务器URL后面加多一个`weixin_id=对应的appid/corpid`的参数</font>
|
||||
* </dl>
|
||||
*
|
||||
* @param aesTokens
|
||||
* 多个公众号
|
||||
@ -128,10 +136,14 @@ public final class WeixinServerBootstrap {
|
||||
}
|
||||
|
||||
/**
|
||||
* 多个公众号的支持
|
||||
* <dt>值得注意的是:
|
||||
* <dl><font color="red">1).企业号服务时需要在服务器URL后面加多一个`encrypt_type=aes`的参数</font></dl>
|
||||
* <dl><font color="red">2).非明文模式下需要在服务器URL后面加多一个`weixin_id=对应的appid/corpid`的参数</font></dl>
|
||||
* 多个公众号的支持 <dt>值得注意的是:
|
||||
* <dl>
|
||||
* <font color="red">1).企业号服务时需要在服务器URL后面加多一个`encrypt_type=aes`的参数</font>
|
||||
* </dl>
|
||||
* <dl>
|
||||
* <font
|
||||
* color="red">2).非明文模式下需要在服务器URL后面加多一个`weixin_id=对应的appid/corpid`的参数</font>
|
||||
* </dl>
|
||||
*
|
||||
* @param messageMatcher
|
||||
* 消息匹配器
|
||||
@ -139,7 +151,8 @@ public final class WeixinServerBootstrap {
|
||||
* 公众号信息
|
||||
* @return
|
||||
*/
|
||||
public WeixinServerBootstrap(WeixinMessageMatcher messageMatcher, AesToken... aesTokens) {
|
||||
public WeixinServerBootstrap(WeixinMessageMatcher messageMatcher,
|
||||
AesToken... aesTokens) {
|
||||
if (messageMatcher == null) {
|
||||
throw new IllegalArgumentException("MessageMatcher not be null");
|
||||
}
|
||||
@ -184,7 +197,8 @@ public final class WeixinServerBootstrap {
|
||||
* @return
|
||||
* @throws WeixinException
|
||||
*/
|
||||
public void startup(int bossThreads, int workerThreads, final int serverPort) throws WeixinException {
|
||||
public void startup(int bossThreads, int workerThreads, final int serverPort)
|
||||
throws WeixinException {
|
||||
messageDispatcher.setMessageHandlerList(messageHandlerList);
|
||||
messageDispatcher.setMessageInterceptorList(messageInterceptorList);
|
||||
|
||||
@ -192,19 +206,25 @@ public final class WeixinServerBootstrap {
|
||||
EventLoopGroup workerGroup = new NioEventLoopGroup(workerThreads);
|
||||
try {
|
||||
|
||||
wechatInitializer = new WeixinServerInitializer(aesTokenMap, messageDispatcher);
|
||||
|
||||
wechatInitializer = new WeixinServerInitializer(aesTokenMap,
|
||||
messageDispatcher);
|
||||
ServerBootstrap b = new ServerBootstrap();
|
||||
b.option(ChannelOption.SO_BACKLOG, 1024);
|
||||
b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).handler(new LoggingHandler())
|
||||
b.group(bossGroup, workerGroup)
|
||||
.channel(NioServerSocketChannel.class)
|
||||
.handler(new LoggingHandler())
|
||||
.childHandler(wechatInitializer);
|
||||
Channel ch = b.bind(serverPort).addListener(new FutureListener<Void>() {
|
||||
Channel ch = b.bind(serverPort)
|
||||
.addListener(new FutureListener<Void>() {
|
||||
@Override
|
||||
public void operationComplete(Future<Void> future) throws Exception {
|
||||
public void operationComplete(Future<Void> future)
|
||||
throws Exception {
|
||||
if (future.isSuccess()) {
|
||||
logger.info("weixin4j server startup OK:{}", serverPort);
|
||||
logger.info("weixin4j server startup OK:{}",
|
||||
serverPort);
|
||||
} else {
|
||||
logger.info("weixin4j server startup FAIL:{}", serverPort);
|
||||
logger.info("weixin4j server startup FAIL:{}",
|
||||
serverPort);
|
||||
}
|
||||
}
|
||||
}).sync().channel();
|
||||
@ -224,7 +244,8 @@ public final class WeixinServerBootstrap {
|
||||
* 消息处理器
|
||||
* @return
|
||||
*/
|
||||
public WeixinServerBootstrap addHandler(WeixinMessageHandler... messageHandler) {
|
||||
public WeixinServerBootstrap addHandler(
|
||||
WeixinMessageHandler... messageHandler) {
|
||||
if (messageHandler == null) {
|
||||
throw new IllegalArgumentException("messageHandler not be null");
|
||||
}
|
||||
@ -239,7 +260,8 @@ public final class WeixinServerBootstrap {
|
||||
* 消息拦截器
|
||||
* @return
|
||||
*/
|
||||
public WeixinServerBootstrap addInterceptor(WeixinMessageInterceptor... messageInterceptor) {
|
||||
public WeixinServerBootstrap addInterceptor(
|
||||
WeixinMessageInterceptor... messageInterceptor) {
|
||||
if (messageInterceptor == null) {
|
||||
throw new IllegalArgumentException("messageInterceptor not be null");
|
||||
}
|
||||
@ -254,9 +276,11 @@ public final class WeixinServerBootstrap {
|
||||
* 消息处理器所在的包名
|
||||
* @return
|
||||
*/
|
||||
public WeixinServerBootstrap handlerPackagesToScan(String... messageHandlerPackages) {
|
||||
public WeixinServerBootstrap handlerPackagesToScan(
|
||||
String... messageHandlerPackages) {
|
||||
if (messageHandlerPackages == null) {
|
||||
throw new IllegalArgumentException("messageHandlerPackages not be null");
|
||||
throw new IllegalArgumentException(
|
||||
"messageHandlerPackages not be null");
|
||||
}
|
||||
messageDispatcher.setMessageHandlerPackages(messageHandlerPackages);
|
||||
return this;
|
||||
@ -269,11 +293,14 @@ public final class WeixinServerBootstrap {
|
||||
* 消息拦截器所在的包名
|
||||
* @return
|
||||
*/
|
||||
public WeixinServerBootstrap interceptorPackagesToScan(String... messageInterceptorPackages) {
|
||||
public WeixinServerBootstrap interceptorPackagesToScan(
|
||||
String... messageInterceptorPackages) {
|
||||
if (messageInterceptorPackages == null) {
|
||||
throw new IllegalArgumentException("messageInterceptorPackages not be null");
|
||||
throw new IllegalArgumentException(
|
||||
"messageInterceptorPackages not be null");
|
||||
}
|
||||
messageDispatcher.setMessageInterceptorPackages(messageInterceptorPackages);
|
||||
messageDispatcher
|
||||
.setMessageInterceptorPackages(messageInterceptorPackages);
|
||||
return this;
|
||||
}
|
||||
|
||||
@ -298,7 +325,8 @@ public final class WeixinServerBootstrap {
|
||||
* 消息类
|
||||
* @return
|
||||
*/
|
||||
public WeixinServerBootstrap registMessageClass(WeixinMessageKey messageKey,
|
||||
public WeixinServerBootstrap registMessageClass(
|
||||
WeixinMessageKey messageKey,
|
||||
Class<? extends WeixinMessage> messageClass) {
|
||||
messageDispatcher.registMessageClass(messageKey, messageClass);
|
||||
return this;
|
||||
@ -312,19 +340,13 @@ public final class WeixinServerBootstrap {
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* aesTokenMap 最好是线程安全的
|
||||
*
|
||||
* @param aesToken
|
||||
* @return
|
||||
*/
|
||||
public int addAesToken(AesToken aesToken){
|
||||
AesToken token = aesTokenMap.get(aesToken.getWeixinId());
|
||||
if (token != null)
|
||||
return -1;
|
||||
|
||||
this.aesTokenMap.put(aesToken.getWeixinId(), aesToken);
|
||||
|
||||
public int addAesToken(AesToken aesToken) {
|
||||
return wechatInitializer.addAesToken(aesToken);
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user