Files
msgexchange-api/src/main/java/com/gzzn/omms/msgexchangeapi/msghandler/MsgHandlerDispatcher.java
T

62 lines
1.8 KiB
Java
Raw Normal View History

2018-12-03 10:44:14 +08:00
package com.gzzn.omms.msgexchangeapi.msghandler;
2018-12-04 11:37:45 +08:00
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
2018-12-03 10:44:14 +08:00
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.gzzn.omms.msgexchangeapi.entity.msg.Msg;
2018-12-03 10:44:14 +08:00
import com.gzzn.omms.msgexchangeapi.service.IExchangeService;
@Service
public class MsgHandlerDispatcher {
@Autowired
IExchangeService exchangeService;
2018-12-04 14:53:50 +08:00
public HandlerResult dispatch(String xmlMsg)
2018-12-03 10:44:14 +08:00
{
Msg msg = exchangeService.xmlstrToObject(xmlMsg, Msg.class);
String type = msg.getMeta().getType();
String subType = msg.getMeta().getStyp();
2018-12-04 11:37:45 +08:00
try
{
Class clazzHandler = Class.forName(getHandlerClassName(type,subType));
Object classObject = clazzHandler.newInstance();
Method runMethod = clazzHandler.getMethod("run", String.class);
2018-12-04 14:53:50 +08:00
HandlerResult result = (HandlerResult) runMethod.invoke(classObject, xmlMsg);
2018-12-04 11:37:45 +08:00
return result;
} catch (ClassNotFoundException
| InstantiationException
| IllegalAccessException
| NoSuchMethodException
| SecurityException
| IllegalArgumentException
| InvocationTargetException e) {
throw new RuntimeException(e);
}
}//end function
/**
* 获取handler 类的名称
* @param type
* @param subType
* @return
*/
private String getHandlerClassName(String type , String subType)
{
StringBuffer buffer = new StringBuffer();
buffer.append("com.gzzn.omms.msgexchangeapi.msghandler" );
buffer.append(".");
2018-12-04 14:53:50 +08:00
buffer.append(type.toLowerCase());
2018-12-04 11:37:45 +08:00
buffer.append(".");
buffer.append(subType);
buffer.append("Handler");
return buffer.toString();
} //end function
2018-12-03 10:44:14 +08:00
}