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做转发到此端口
*/
private static int port = 10000;
private static int port = 30000;
/**
* 服务器token信息
*/

View File

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