weixin4j-server:优化WeixinMessageDispatcher类

This commit is contained in:
jinyu 2015-08-06 20:02:28 +08:00
parent 795620cf93
commit 418f63d501
6 changed files with 35 additions and 44 deletions

View File

@ -419,4 +419,8 @@
+ **weixin4j-server**: 新增base64解编码类(来自apache) + **weixin4j-server**: 新增base64解编码类(来自apache)
+ 在WeixinProxy类新增VERSION字段 + 在WeixinProxy类新增VERSION字段
* 2015-08-06
+ **weixin4j-server**: 调整`LocationEventMessage`类中的经纬度字段类型为double

View File

@ -40,7 +40,7 @@ weixin4j
<artifactId>weixin4j-qy</artifactId> <artifactId>weixin4j-qy</artifactId>
<version>1.5.1</version> <version>1.5.1</version>
</dependency> </dependency>
微信被动消息服务器(1.0.3,2015-07-04 released) 微信回调消息服务器(1.0.3,2015-07-04 released)
<dependency> <dependency>
<groupId>com.foxinmy</groupId> <groupId>com.foxinmy</groupId>
@ -72,8 +72,6 @@ weixin4j
接下来 接下来
------ ------
* 企业号消息服务
* 公众号第三方服务应用 * 公众号第三方服务应用
* 硬件设备 & 摇一摇周边 * 硬件设备 & 摇一摇周边

View File

@ -9,8 +9,6 @@ import io.netty.util.internal.logging.InternalLoggerFactory;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
@ -25,7 +23,6 @@ import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource; import javax.xml.transform.stream.StreamSource;
import com.foxinmy.weixin4j.exception.WeixinException; import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.handler.MessageHandlerAdapter;
import com.foxinmy.weixin4j.handler.WeixinMessageHandler; import com.foxinmy.weixin4j.handler.WeixinMessageHandler;
import com.foxinmy.weixin4j.interceptor.WeixinMessageInterceptor; import com.foxinmy.weixin4j.interceptor.WeixinMessageInterceptor;
import com.foxinmy.weixin4j.request.WeixinMessage; import com.foxinmy.weixin4j.request.WeixinMessage;
@ -210,28 +207,15 @@ public class WeixinMessageDispatcher {
ChannelHandlerContext context, WeixinRequest request, ChannelHandlerContext context, WeixinRequest request,
WeixinMessageKey messageKey, Object message, Set<String> nodeNames) WeixinMessageKey messageKey, Object message, Set<String> nodeNames)
throws WeixinException { throws WeixinException {
WeixinMessageHandler messageHandler = null;
WeixinMessageHandler[] messageHandlers = getMessageHandlers(); WeixinMessageHandler[] messageHandlers = getMessageHandlers();
if (messageHandlers == null) { if (messageHandlers == null) {
return null; return null;
} }
WeixinMessageHandler messageHandler = null;
for (WeixinMessageHandler handler : messageHandlers) { for (WeixinMessageHandler handler : messageHandlers) {
if (handler instanceof MessageHandlerAdapter) { if (handler.canHandle(request, message, nodeNames)) {
Class<?> genericType = genericTypeRead(handler); messageHandler = handler;
if (genericType == message.getClass() break;
&& handler.canHandle(request, message, nodeNames)) {
messageHandler = handler;
break;
}
}
}
if (messageHandler == null) {
for (WeixinMessageHandler handler : messageHandlers) {
if (!(handler instanceof MessageHandlerAdapter)
&& handler.canHandle(request, message, nodeNames)) {
messageHandler = handler;
break;
}
} }
} }
return new MessageHandlerExecutor(context, messageHandler, return new MessageHandlerExecutor(context, messageHandler,
@ -390,23 +374,6 @@ public class WeixinMessageDispatcher {
return unmarshaller; return unmarshaller;
} }
/**
* 获得泛型类型
*
* @param object
* @return
*/
private Class<?> genericTypeRead(Object object) {
Class<?> clazz = null;
Type type = object.getClass().getGenericSuperclass();
if (type instanceof ParameterizedType) {
ParameterizedType ptype = ((ParameterizedType) type);
Type[] args = ptype.getActualTypeArguments();
clazz = (Class<?>) args[0];
}
return clazz;
}
public void setMessageHandlerList( public void setMessageHandlerList(
List<WeixinMessageHandler> messageHandlerList) { List<WeixinMessageHandler> messageHandlerList) {
this.messageHandlerList = messageHandlerList; this.messageHandlerList = messageHandlerList;

View File

@ -6,9 +6,10 @@ import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.request.WeixinMessage; import com.foxinmy.weixin4j.request.WeixinMessage;
import com.foxinmy.weixin4j.request.WeixinRequest; import com.foxinmy.weixin4j.request.WeixinRequest;
import com.foxinmy.weixin4j.response.WeixinResponse; import com.foxinmy.weixin4j.response.WeixinResponse;
import com.foxinmy.weixin4j.util.ClassUtil;
/** /**
* 消息处理的适配,主要对微信消息进行泛型转换 * 消息适配器
* *
* @className MessageHandlerAdapter * @className MessageHandlerAdapter
* @author jy * @author jy
@ -23,7 +24,9 @@ public abstract class MessageHandlerAdapter<M extends WeixinMessage> implements
@Override @Override
public boolean canHandle(WeixinRequest request, Object message, public boolean canHandle(WeixinRequest request, Object message,
Set<String> nodeNames) throws WeixinException { Set<String> nodeNames) throws WeixinException {
return canHandle0(request, (M) message); return message != null
&& message.getClass() == ClassUtil.getGenericType(this)
&& canHandle0(request, (M) message);
} }
/** /**

View File

@ -7,6 +7,8 @@ import java.io.FilenameFilter;
import java.io.IOException; import java.io.IOException;
import java.io.ObjectInputStream; import java.io.ObjectInputStream;
import java.io.ObjectOutputStream; import java.io.ObjectOutputStream;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.net.JarURLConnection; import java.net.JarURLConnection;
import java.net.URL; import java.net.URL;
import java.util.Enumeration; import java.util.Enumeration;
@ -159,6 +161,23 @@ public final class ClassUtil {
} }
} }
/**
* 获得泛型类型
*
* @param object
* @return
*/
public static Class<?> getGenericType(Object object) {
Class<?> clazz = null;
Type type = object.getClass().getGenericSuperclass();
if (type instanceof ParameterizedType) {
ParameterizedType ptype = ((ParameterizedType) type);
Type[] args = ptype.getActualTypeArguments();
clazz = (Class<?>) args[0];
}
return clazz;
}
public static void main(String[] args) { public static void main(String[] args) {
System.err System.err
.println(getClasses(com.foxinmy.weixin4j.handler.WeixinMessageHandler.class .println(getClasses(com.foxinmy.weixin4j.handler.WeixinMessageHandler.class

View File

@ -113,6 +113,6 @@ public class MessageServerStartup {
} }
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
new MessageServerStartup().test1(); new MessageServerStartup().test3();
} }
} }