This commit is contained in:
jinyu
2017-06-23 18:07:31 +08:00
parent 6464ba8842
commit 4c0f96fcfe
6 changed files with 441 additions and 474 deletions
+1
View File
@@ -7,6 +7,7 @@
<artifactId>weixin4j</artifactId>
<version>1.7.6</version>
</parent>
<packaging>war</packaging>
<artifactId>weixin4j-example</artifactId>
<version>1.0</version>
<name>weixin4j-example</name>
@@ -1,8 +1,5 @@
package com.foxinmy.weixin4j.example.server;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
@@ -19,76 +16,71 @@ import io.netty.util.internal.logging.InternalLoggerFactory;
* 微信消息服务:需要另外开启一个线程去启动服务,这里值得注意的时:weixin4j-serve本身是作为一个单独的服务来启动的,可以不依赖Spring容器,
* 但考虑到目前都是Spring mvc的架构,这里就需要使用一个独立的线程去启动服务,其实本身没有使用spring mvc的API
* 以后会考虑支持servlet api去集成不同的web框架。
*
*
* @className Weixin4jServerStartupWithThread
* @author jinyu(foxinmy@gmail.com)
* @date 2015年5月7日
* @since JDK 1.6
*/
public class Weixin4jServerStartupWithThread implements ApplicationContextAware {
/**
* 服务监听的端口号,目前微信只支持80端口,可以考虑用nginx做转发到此端口
*/
private final int port;
/**
* 服务器token信息
*/
/**
* 明文模式:String aesToken = ""; 密文模式:AesToken aesToken = new
* AesToken("公众号appid", "公众号token","公众号加密/解密消息的密钥");
*/
private final AesToken aesToken;
/**
* 处理微信消息的全限包名(也可通过addHandler方式一个一个添加)
*/
private final String handlerPackage;
/**
* 用spring去获取bean
*/
private ApplicationContext applicationContext;
/**
* 服务监听的端口号,目前微信只支持80端口,可以考虑用nginx做转发到此端口
*/
private final int port;
/**
* 服务器token信息
*/
/**
* 明文模式:String aesToken = ""; 密文模式:AesToken aesToken = new
* AesToken("公众号appid", "公众号token","公众号加密/解密消息的密钥");
*/
private final AesToken aesToken;
/**
* 处理微信消息的全限包名(也可通过addHandler方式一个一个添加)
*/
private final String handlerPackage;
/**
* 用spring去获取bean
*/
private ApplicationContext applicationContext;
private Weixin4jServerStartupWithThread(int port, AesToken aesToken,
String handlerPackage) {
this.port = port;
this.aesToken = aesToken;
this.handlerPackage = handlerPackage;
}
private Weixin4jServerStartupWithThread(int port, AesToken aesToken, String handlerPackage) {
this.port = port;
this.aesToken = aesToken;
this.handlerPackage = handlerPackage;
}
@Override
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
this.applicationContext = applicationContext;
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
private ExecutorService executor;
private WeixinServerBootstrap bootstrap;
/**
* 启动函数
*
* @throws WeixinException
*/
public void start() {
executor = Executors.newCachedThreadPool();
executor.execute(new Runnable() {
@Override
public void run() {
try {
new WeixinServerBootstrap(aesToken) // 指定开发者token信息
.handlerPackagesToScan(handlerPackage) // 扫描处理消息的包
.resolveBeanFactory(
new SpringBeanFactory(applicationContext)) // 声明处理消息类由Spring容器去实例化
.addHandler(DebugMessageHandler.global) // 当没有匹配到消息处理时输出调试信息,开发环境打开
.openAlwaysResponse() // 当没有匹配到消息处理时输出空白回复(公众号不会出现「该公众号无法提供服务的提示」),正式环境打开。
.startup(port); // 绑定服务的端口号,即对外暴露(微信服务器URL地址)的服务端口。
} catch (WeixinException e) {
InternalLoggerFactory.getInstance(getClass()).error(
"weixin4j server startup:FAIL", e);
}
}
});
}
/**
* 启动函数
*
* @throws WeixinException
*/
public void start() {
new Thread(new Runnable() {
@Override
public void run() {
try {
bootstrap = new WeixinServerBootstrap(aesToken) // 指定开发者token信息。
.handlerPackagesToScan(handlerPackage) // 扫描处理消息的包
.resolveBeanFactory(new SpringBeanFactory(applicationContext)) // 声明处理消息类由Spring容器去实例化
.addHandler(DebugMessageHandler.global) // 当没有匹配到消息处理时输出调试信息,开发环境打开。
.openAlwaysResponse(); // 当没有匹配到消息处理时输出空白回复(公众号不会出现「该公众号无法提供服务的提示」),正式环境打开
bootstrap.startup(port); // 绑定服务的端口号,即对外暴露(微信服务器URL地址)的服务端口
} catch (WeixinException e) {
InternalLoggerFactory.getInstance(getClass()).error("weixin4j server startup:FAIL", e);
}
}
}).start();
}
public void stop() {
executor.shutdown();
}
public void stop() {
bootstrap.shutdown();
}
}
@@ -5,8 +5,8 @@
xsi:schemaLocation="
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">
<import resource="spring-weixin4j-proxy.xml" />
<import resource="spring-weixin4j-server.xml" />
</beans>