This commit is contained in:
jinyu 2016-04-12 16:39:08 +08:00
parent 5b7ed61713
commit 80c794b8ae
2 changed files with 362 additions and 382 deletions

View File

@ -20,7 +20,7 @@ public class Weixin4jServerStartupWithoutThread {
/** /**
* 服务监听的端口号,目前微信只支持80端口,可以考虑用nginx做转发到此端口 * 服务监听的端口号,目前微信只支持80端口,可以考虑用nginx做转发到此端口
*/ */
private static int port = 10000; private static int port = 30000;
/** /**
* 服务器token信息 * 服务器token信息
*/ */

View File

@ -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的核数
@ -98,6 +97,7 @@ public final class WeixinServerBootstrap {
/** /**
* 明文模式 & 兼容模式 & 密文模式 * 明文模式 & 兼容模式 & 密文模式
* <dl><font color="red">值得注意的是企业号服务时需要在服务器URL后面加多一个`encrypt_type=aes`的参数</font></dl>
* *
* @param weixinId * @param weixinId
* 公众号的应用ID(appid/corpid) 密文&兼容模式下需要填写 * 公众号的应用ID(appid/corpid) 密文&兼容模式下需要填写
@ -113,10 +113,9 @@ public final class WeixinServerBootstrap {
/** /**
* 多个公众号的支持 * 多个公众号的支持
* <p> * <dt>值得注意的是
* <font color="red">请注意需在服务接收事件的URL中附加一个名为wexin_id的参数,其值请填写公众号的appid/ * <dl><font color="red">1).企业号服务时需要在服务器URL后面加多一个`encrypt_type=aes`的参数</font></dl>
* corpid</font> * <dl><font color="red">2).非明文模式下需要在服务器URL后面加多一个`weixin_id={对应的appid/corpid}`的参数</font></dl>
* <p>
* *
* @param aesTokens * @param aesTokens
* 多个公众号 * 多个公众号
@ -128,10 +127,9 @@ public final class WeixinServerBootstrap {
/** /**
* 多个公众号的支持 * 多个公众号的支持
* <p> * <dt>值得注意的是
* <font color="red">请注意需在服务接收事件的URL中附加一个名为wexin_id的参数,其值请填写公众号的appid/ * <dl><font color="red">1).企业号服务时需要在服务器URL后面加多一个`encrypt_type=aes`的参数</font></dl>
* corpid</font> * <dl><font color="red">2).非明文模式下需要在服务器URL后面加多一个`weixin_id={对应的appid/corpid}`的参数</font></dl>
* <p>
* *
* @param messageMatcher * @param messageMatcher
* 消息匹配器 * 消息匹配器
@ -139,8 +137,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");
} }
@ -158,7 +155,7 @@ public final class WeixinServerBootstrap {
} }
/** /**
* 默认端口启动服务 * 默认端口(30000)启动服务
* *
*/ */
public void startup() throws WeixinException { public void startup() throws WeixinException {
@ -185,8 +182,7 @@ 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);
@ -195,29 +191,21 @@ public final class WeixinServerBootstrap {
try { try {
ServerBootstrap b = new ServerBootstrap(); ServerBootstrap b = new ServerBootstrap();
b.option(ChannelOption.SO_BACKLOG, 1024); b.option(ChannelOption.SO_BACKLOG, 1024);
b.group(bossGroup, workerGroup) b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).handler(new LoggingHandler())
.channel(NioServerSocketChannel.class) .childHandler(new WeixinServerInitializer(aesTokenMap, messageDispatcher));
.handler(new LoggingHandler()) Channel ch = b.bind(serverPort).addListener(new FutureListener<Void>() {
.childHandler(
new WeixinServerInitializer(aesTokenMap,
messageDispatcher));
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();
ch.closeFuture().sync(); ch.closeFuture().sync();
} catch (InterruptedException e) { } catch (InterruptedException e) {
throw new WeixinException(e); throw new WeixinException("netty server startup FAIL", e);
} finally { } finally {
bossGroup.shutdownGracefully(); bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully(); workerGroup.shutdownGracefully();
@ -231,8 +219,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");
} }
@ -247,8 +234,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");
} }
@ -263,11 +249,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;
@ -280,14 +264,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;
} }
@ -312,8 +293,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;