支持动态添加微信账号AesToken
This commit is contained in:
parent
21b1f9ecad
commit
f522dbe0b9
@ -10,6 +10,7 @@ import io.netty.util.internal.logging.InternalLoggerFactory;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||
import com.foxinmy.weixin4j.request.WeixinRequest;
|
||||
@ -35,10 +36,24 @@ public class WeixinMessageDecoder extends
|
||||
private final InternalLogger logger = InternalLoggerFactory
|
||||
.getInstance(getClass());
|
||||
|
||||
private Map<String, AesToken> aesTokenMap;
|
||||
private Map<String, AesToken> aesTokenMap = new ConcurrentHashMap<String, AesToken>();
|
||||
|
||||
public WeixinMessageDecoder(Map<String, AesToken> aesTokenMap) {
|
||||
this.aesTokenMap = aesTokenMap;
|
||||
//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){
|
||||
AesToken token = aesTokenMap.get(asetoken.getWeixinId());
|
||||
if (token != null)
|
||||
return -1;
|
||||
|
||||
aesTokenMap.put(asetoken.getWeixinId(), asetoken);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@ -22,21 +22,27 @@ import com.foxinmy.weixin4j.util.AesToken;
|
||||
*/
|
||||
public class WeixinServerInitializer extends ChannelInitializer<SocketChannel> {
|
||||
|
||||
private final Map<String, AesToken> aesTokenMap;
|
||||
private final WeixinMessageDispatcher messageDispatcher;
|
||||
|
||||
private WeixinMessageDecoder messageDecoder;
|
||||
|
||||
public WeixinServerInitializer(Map<String, AesToken> aesTokenMap,
|
||||
WeixinMessageDispatcher messageDispatcher) {
|
||||
this.aesTokenMap = aesTokenMap;
|
||||
this.messageDispatcher = messageDispatcher;
|
||||
messageDecoder = new WeixinMessageDecoder(aesTokenMap);
|
||||
}
|
||||
|
||||
public int addAesToken(final AesToken asetoken){
|
||||
return messageDecoder.addAesToken(asetoken);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void initChannel(SocketChannel channel) {
|
||||
|
||||
ChannelPipeline pipeline = channel.pipeline();
|
||||
pipeline.addLast(new HttpServerCodec());
|
||||
pipeline.addLast(new HttpObjectAggregator(65536));
|
||||
pipeline.addLast(new WeixinMessageDecoder(aesTokenMap));
|
||||
pipeline.addLast(messageDecoder);
|
||||
pipeline.addLast(new WeixinResponseEncoder());
|
||||
pipeline.addLast(new SingleResponseEncoder());
|
||||
pipeline.addLast(new WeixinRequestHandler(messageDispatcher));
|
||||
|
||||
@ -78,6 +78,8 @@ public final class WeixinServerBootstrap {
|
||||
*/
|
||||
private final Map<String, AesToken> aesTokenMap;
|
||||
|
||||
WeixinServerInitializer wechatInitializer;
|
||||
|
||||
static {
|
||||
DEFAULT_BOSSTHREADS = Runtime.getRuntime().availableProcessors();
|
||||
DEFAULT_WORKERTHREADS = DEFAULT_BOSSTHREADS * 4;
|
||||
@ -189,10 +191,13 @@ public final class WeixinServerBootstrap {
|
||||
EventLoopGroup bossGroup = new NioEventLoopGroup(bossThreads);
|
||||
EventLoopGroup workerGroup = new NioEventLoopGroup(workerThreads);
|
||||
try {
|
||||
|
||||
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())
|
||||
.childHandler(new WeixinServerInitializer(aesTokenMap, messageDispatcher));
|
||||
.childHandler(wechatInitializer);
|
||||
Channel ch = b.bind(serverPort).addListener(new FutureListener<Void>() {
|
||||
@Override
|
||||
public void operationComplete(Future<Void> future) throws Exception {
|
||||
@ -307,5 +312,21 @@ 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);
|
||||
|
||||
return wechatInitializer.addAesToken(aesToken);
|
||||
}
|
||||
|
||||
public final static String VERSION = "1.1.8";
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user