commit
102b229ada
@ -10,6 +10,7 @@ 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.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||||
import com.foxinmy.weixin4j.request.WeixinRequest;
|
import com.foxinmy.weixin4j.request.WeixinRequest;
|
||||||
@ -35,10 +36,24 @@ public class WeixinMessageDecoder extends
|
|||||||
private final InternalLogger logger = InternalLoggerFactory
|
private final InternalLogger logger = InternalLoggerFactory
|
||||||
.getInstance(getClass());
|
.getInstance(getClass());
|
||||||
|
|
||||||
private Map<String, AesToken> aesTokenMap;
|
private Map<String, AesToken> aesTokenMap = new ConcurrentHashMap<String, AesToken>();
|
||||||
|
|
||||||
public WeixinMessageDecoder(Map<String, AesToken> aesTokenMap) {
|
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
|
@Override
|
||||||
|
|||||||
@ -22,21 +22,27 @@ import com.foxinmy.weixin4j.util.AesToken;
|
|||||||
*/
|
*/
|
||||||
public class WeixinServerInitializer extends ChannelInitializer<SocketChannel> {
|
public class WeixinServerInitializer extends ChannelInitializer<SocketChannel> {
|
||||||
|
|
||||||
private final Map<String, AesToken> aesTokenMap;
|
|
||||||
private final WeixinMessageDispatcher messageDispatcher;
|
private final WeixinMessageDispatcher messageDispatcher;
|
||||||
|
|
||||||
|
private WeixinMessageDecoder messageDecoder;
|
||||||
|
|
||||||
public WeixinServerInitializer(Map<String, AesToken> aesTokenMap,
|
public WeixinServerInitializer(Map<String, AesToken> aesTokenMap,
|
||||||
WeixinMessageDispatcher messageDispatcher) {
|
WeixinMessageDispatcher messageDispatcher) {
|
||||||
this.aesTokenMap = aesTokenMap;
|
|
||||||
this.messageDispatcher = messageDispatcher;
|
this.messageDispatcher = messageDispatcher;
|
||||||
|
messageDecoder = new WeixinMessageDecoder(aesTokenMap);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int addAesToken(final AesToken asetoken){
|
||||||
|
return messageDecoder.addAesToken(asetoken);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void initChannel(SocketChannel channel) {
|
protected void initChannel(SocketChannel channel) {
|
||||||
|
|
||||||
ChannelPipeline pipeline = channel.pipeline();
|
ChannelPipeline pipeline = channel.pipeline();
|
||||||
pipeline.addLast(new HttpServerCodec());
|
pipeline.addLast(new HttpServerCodec());
|
||||||
pipeline.addLast(new HttpObjectAggregator(65536));
|
pipeline.addLast(new HttpObjectAggregator(65536));
|
||||||
pipeline.addLast(new WeixinMessageDecoder(aesTokenMap));
|
pipeline.addLast(messageDecoder);
|
||||||
pipeline.addLast(new WeixinResponseEncoder());
|
pipeline.addLast(new WeixinResponseEncoder());
|
||||||
pipeline.addLast(new SingleResponseEncoder());
|
pipeline.addLast(new SingleResponseEncoder());
|
||||||
pipeline.addLast(new WeixinRequestHandler(messageDispatcher));
|
pipeline.addLast(new WeixinRequestHandler(messageDispatcher));
|
||||||
|
|||||||
@ -78,6 +78,8 @@ public final class WeixinServerBootstrap {
|
|||||||
*/
|
*/
|
||||||
private final Map<String, AesToken> aesTokenMap;
|
private final Map<String, AesToken> aesTokenMap;
|
||||||
|
|
||||||
|
WeixinServerInitializer wechatInitializer;
|
||||||
|
|
||||||
static {
|
static {
|
||||||
DEFAULT_BOSSTHREADS = Runtime.getRuntime().availableProcessors();
|
DEFAULT_BOSSTHREADS = Runtime.getRuntime().availableProcessors();
|
||||||
DEFAULT_WORKERTHREADS = DEFAULT_BOSSTHREADS * 4;
|
DEFAULT_WORKERTHREADS = DEFAULT_BOSSTHREADS * 4;
|
||||||
@ -189,10 +191,13 @@ public final class WeixinServerBootstrap {
|
|||||||
EventLoopGroup bossGroup = new NioEventLoopGroup(bossThreads);
|
EventLoopGroup bossGroup = new NioEventLoopGroup(bossThreads);
|
||||||
EventLoopGroup workerGroup = new NioEventLoopGroup(workerThreads);
|
EventLoopGroup workerGroup = new NioEventLoopGroup(workerThreads);
|
||||||
try {
|
try {
|
||||||
|
|
||||||
|
wechatInitializer = new WeixinServerInitializer(aesTokenMap, messageDispatcher);
|
||||||
|
|
||||||
ServerBootstrap b = new ServerBootstrap();
|
ServerBootstrap b = new ServerBootstrap();
|
||||||
b.option(ChannelOption.SO_BACKLOG, 1024);
|
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(new WeixinServerInitializer(aesTokenMap, messageDispatcher));
|
.childHandler(wechatInitializer);
|
||||||
Channel ch = b.bind(serverPort).addListener(new FutureListener<Void>() {
|
Channel ch = b.bind(serverPort).addListener(new FutureListener<Void>() {
|
||||||
@Override
|
@Override
|
||||||
public void operationComplete(Future<Void> future) throws Exception {
|
public void operationComplete(Future<Void> future) throws Exception {
|
||||||
@ -307,5 +312,21 @@ public final class WeixinServerBootstrap {
|
|||||||
return this;
|
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";
|
public final static String VERSION = "1.1.8";
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user