listener
This commit is contained in:
parent
bcda642178
commit
4ccc11d8cf
@ -36,9 +36,8 @@ public final class Weixin4jServerStartup {
|
|||||||
* assembly%E6%89%93%E5%8C%85
|
* assembly%E6%89%93%E5%8C%85
|
||||||
*
|
*
|
||||||
* @param args
|
* @param args
|
||||||
* @throws WeixinException
|
|
||||||
*/
|
*/
|
||||||
public static void main(String[] args) throws WeixinException {
|
public static void main(String[] args){
|
||||||
new WeixinServerBootstrap(new AesToken("wxa652fc930afe9b22", "weixin4j", "3XItJRSSkTqH7etjBUbLfLBecLdFfGjbGPfmIHuGftD")) // 指定开发者token信息。
|
new WeixinServerBootstrap(new AesToken("wxa652fc930afe9b22", "weixin4j", "3XItJRSSkTqH7etjBUbLfLBecLdFfGjbGPfmIHuGftD")) // 指定开发者token信息。
|
||||||
.handlerPackagesToScan(handlerPackage) // 扫描处理消息的包。
|
.handlerPackagesToScan(handlerPackage) // 扫描处理消息的包。
|
||||||
.addHandler(DebugMessageHandler.global) // 当没有匹配到消息处理时输出调试信息,开发环境打开。
|
.addHandler(DebugMessageHandler.global) // 当没有匹配到消息处理时输出调试信息,开发环境打开。
|
||||||
|
|||||||
@ -1,17 +1,16 @@
|
|||||||
package com.foxinmy.weixin4j.example.server;
|
package com.foxinmy.weixin4j.example.server;
|
||||||
|
|
||||||
import org.springframework.beans.BeansException;
|
import javax.servlet.ServletContextEvent;
|
||||||
import org.springframework.context.ApplicationContext;
|
import javax.servlet.ServletContextListener;
|
||||||
import org.springframework.context.ApplicationContextAware;
|
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
|
import org.springframework.web.context.support.WebApplicationContextUtils;
|
||||||
|
|
||||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
|
||||||
import com.foxinmy.weixin4j.handler.DebugMessageHandler;
|
import com.foxinmy.weixin4j.handler.DebugMessageHandler;
|
||||||
import com.foxinmy.weixin4j.spring.SpringBeanFactory;
|
import com.foxinmy.weixin4j.spring.SpringBeanFactory;
|
||||||
import com.foxinmy.weixin4j.startup.WeixinServerBootstrap;
|
import com.foxinmy.weixin4j.startup.WeixinServerBootstrap;
|
||||||
import com.foxinmy.weixin4j.util.AesToken;
|
import com.foxinmy.weixin4j.util.AesToken;
|
||||||
|
|
||||||
import io.netty.util.internal.logging.InternalLoggerFactory;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 微信消息服务:需要另外开启一个线程去启动服务,这里值得注意的时:weixin4j-serve本身是作为一个单独的服务来启动的,可以不依赖Spring容器,
|
* 微信消息服务:需要另外开启一个线程去启动服务,这里值得注意的时:weixin4j-serve本身是作为一个单独的服务来启动的,可以不依赖Spring容器,
|
||||||
* 但考虑到目前都是Spring mvc的架构,这里就需要使用一个独立的线程去启动服务,其实本身没有使用spring mvc的API,
|
* 但考虑到目前都是Spring mvc的架构,这里就需要使用一个独立的线程去启动服务,其实本身没有使用spring mvc的API,
|
||||||
@ -22,7 +21,7 @@ import io.netty.util.internal.logging.InternalLoggerFactory;
|
|||||||
* @date 2015年5月7日
|
* @date 2015年5月7日
|
||||||
* @since JDK 1.6
|
* @since JDK 1.6
|
||||||
*/
|
*/
|
||||||
public class Weixin4jServerStartupWithThread implements ApplicationContextAware {
|
public class Weixin4jServerStartupListener implements ServletContextListener {
|
||||||
/**
|
/**
|
||||||
* 服务监听的端口号,目前微信只支持80端口,可以考虑用nginx做转发到此端口
|
* 服务监听的端口号,目前微信只支持80端口,可以考虑用nginx做转发到此端口
|
||||||
*/
|
*/
|
||||||
@ -39,30 +38,22 @@ public class Weixin4jServerStartupWithThread implements ApplicationContextAware
|
|||||||
* 处理微信消息的全限包名(也可通过addHandler方式一个一个添加)
|
* 处理微信消息的全限包名(也可通过addHandler方式一个一个添加)
|
||||||
*/
|
*/
|
||||||
private final String handlerPackage;
|
private final String handlerPackage;
|
||||||
/**
|
|
||||||
* 用spring去获取bean
|
|
||||||
*/
|
|
||||||
private ApplicationContext applicationContext;
|
|
||||||
|
|
||||||
private Weixin4jServerStartupWithThread(int port, AesToken aesToken, String handlerPackage) {
|
public Weixin4jServerStartupListener() {
|
||||||
this.port = port;
|
// 可以考虑通过参数获取
|
||||||
this.aesToken = aesToken;
|
this.port = 30000;
|
||||||
this.handlerPackage = handlerPackage;
|
this.aesToken = new AesToken("weixin4j");
|
||||||
}
|
this.handlerPackage = "com.foxinmy.weixin4j.example.server.handler";
|
||||||
|
|
||||||
@Override
|
|
||||||
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
|
||||||
this.applicationContext = applicationContext;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private WeixinServerBootstrap bootstrap;
|
private WeixinServerBootstrap bootstrap;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 启动函数
|
* 启动服务
|
||||||
*
|
*
|
||||||
* @throws WeixinException
|
* @param applicationContext
|
||||||
*/
|
*/
|
||||||
public void start() {
|
public void start(final ApplicationContext applicationContext) {
|
||||||
new Thread(new Runnable() {
|
new Thread(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
@ -76,7 +67,20 @@ public class Weixin4jServerStartupWithThread implements ApplicationContextAware
|
|||||||
}).start();
|
}).start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 关闭服务
|
||||||
|
*/
|
||||||
public void stop() {
|
public void stop() {
|
||||||
bootstrap.shutdown();
|
bootstrap.shutdown(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void contextInitialized(ServletContextEvent sce) {
|
||||||
|
start(WebApplicationContextUtils.getRequiredWebApplicationContext(sce.getServletContext()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void contextDestroyed(ServletContextEvent sce) {
|
||||||
|
stop();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -11,28 +11,7 @@ weixin4j-serve本身是作为一个单独的服务来启动的,可以不依赖
|
|||||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
|
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
|
||||||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
|
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
|
||||||
|
|
||||||
<!-- 微信消息服务~start -->
|
<!-- 详见:com.foxinmy.weixin4j.example.server.Weixin4jServerStartupListener和web.xml配置 -->
|
||||||
<bean
|
|
||||||
class="com.foxinmy.weixin4j.example.server.Weixin4jServerStartupWithThread"
|
|
||||||
init-method="start" destroy-method="stop">
|
|
||||||
<!-- 端口号 微信暂时只支持80端口 所以需要自己把微信被动消息请求转发(nginx等)到这个端口上来 -->
|
|
||||||
<constructor-arg index="0" value="30000" />
|
|
||||||
<!-- token信息 -->
|
|
||||||
<constructor-arg index="1">
|
|
||||||
<!-- 明文模式 -->
|
|
||||||
<bean class="com.foxinmy.weixin4j.util.AesToken">
|
|
||||||
<constructor-arg index="0" value="开发者Token" />
|
|
||||||
</bean>
|
|
||||||
<!-- 加密模式 -->
|
|
||||||
<!-- bean class="com.foxinmy.weixin4j.util.AesToken"> <constructor-arg
|
|
||||||
index="0" value="公众号的应用ID(appid/corpid)" /> <constructor-arg index="1" value="开发者Token"
|
|
||||||
/> <constructor-arg index="2" value="解密的EncodingAESKey" /> </bean -->
|
|
||||||
</constructor-arg>
|
|
||||||
<!-- 处理微信消息的全限包名 -->
|
|
||||||
<constructor-arg index="2"
|
|
||||||
value="com.foxinmy.weixin4j.example.server.handler" />
|
|
||||||
</bean>
|
|
||||||
<!-- 微信消息服务~end -->
|
|
||||||
|
|
||||||
<!-- spring扫描加载消息处理类~start -->
|
<!-- spring扫描加载消息处理类~start -->
|
||||||
<context:component-scan base-package="com.foxinmy.weixin4j.example.server.handler" />
|
<context:component-scan base-package="com.foxinmy.weixin4j.example.server.handler" />
|
||||||
|
|||||||
@ -42,4 +42,7 @@
|
|||||||
<listener>
|
<listener>
|
||||||
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
|
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
|
||||||
</listener>
|
</listener>
|
||||||
|
<listener>
|
||||||
|
<listener-class>com.foxinmy.weixin4j.example.server.Weixin4jServerStartupListener</listener-class>
|
||||||
|
</listener>
|
||||||
</web-app>
|
</web-app>
|
||||||
@ -219,17 +219,28 @@ public final class WeixinServerBootstrap {
|
|||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
} finally {
|
} finally {
|
||||||
shutdown();
|
shutdown(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean shutdown() {
|
/**
|
||||||
|
* 关闭微信服务
|
||||||
|
*
|
||||||
|
* @param blocking
|
||||||
|
* 阻塞关闭
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public boolean shutdown(boolean blocking) {
|
||||||
if (bootstrap == null) {
|
if (bootstrap == null) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
ServerBootstrapConfig c = bootstrap.config();
|
ServerBootstrapConfig c = bootstrap.config();
|
||||||
c.group().shutdownGracefully();
|
Future<?> bossF = c.group().shutdownGracefully();
|
||||||
c.childGroup().shutdownGracefully();
|
Future<?> workerF = c.childGroup().shutdownGracefully();
|
||||||
|
if (blocking) {
|
||||||
|
bossF.awaitUninterruptibly();
|
||||||
|
workerF.awaitUninterruptibly();
|
||||||
|
}
|
||||||
messageHandlerList = null;
|
messageHandlerList = null;
|
||||||
messageInterceptorList = null;
|
messageInterceptorList = null;
|
||||||
messageDispatcher = null;
|
messageDispatcher = null;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user