以服务实现类NewsService为例:
思考:不明白作者为什么要这样生成服务代理类,根据服务名用反射获取服务实例不行吗?性能貌似也不低啊。 还是说为了热部署才这么做的,我能想到的只有这么个需求才会这么做。
ProxyFactoryCreater类动态生成的代码:
//动态代理工厂类
public class ProxyFactory1439795760927 implements IProxyFactory{
private IProxyStub proxyNewsServicedemo =null;
private IProxyStub demo1 =null;
private IProxyStub demo2 =null;
public void IProxyFactory(java.util.List $1);
{
proxyNewsServicedemo=(com.bj58.spat.gaea.server.contract.context.IProxyStub)$1.get(0);
demo1=(com.bj58.spat.gaea.server.contract.context.IProxyStub)$1.get(1);
demo2=(com.bj58.spat.gaea.server.contract.context.IProxyStub)$1.get(2);
}
//动态生成代理工程的getProxy方法
public IProxyStub getProxy(String lookup) {
if(lookup.equalsIgnoreCase("NewsService"))
{
return proxyNewsServicedemo;
}
if(..){
..
}
if(..){
..
}
if(..){
..
}
return null;
}
}
}
ProxyClassCreater类动态生成的代码:
//动态代理类
public class NewsServiceProxyStub1439795944146{
/******************服务实现类对象start*************************/
private static INewsService serviceProxy = new NewsService();
/******************服务实现类对象end*************************/
/**********************具体的服务调用start********************************************/
public GaeaResponse getNewsByID(GaeaContext context) ServiceFrameException
{
IConvert convert = ConvertFacotry.getConvert(context.getGaeaRequest().getProtocol());
RequestProtocol request = (RequestProtocol)context.getGaeaRequest().getProtocol().getSdpEntity();
java.util.List listKV = request.getParaKVList();
if(listKV.size() == 1 && (((KeyValuePair)listKV.get(0)).getKey().toString().equalsIgnoreCase("int")|| ((KeyValuePair)listKV.get(0)).getKey().toString().equalsIgnoreCase("Integer")))
{
java.util.List listOutPara = new java.util.ArrayList();
int arg0 = 0;
try {
arg0 = convert.convertToint(((KeyValuePair)listKV.get(0)).getValue());
}
catch (Exception e)
{
throw new ServiceFrameException("method:NewsServiceProxyStub1439546304065.getNewsByID--msg:parse gaeaRequest error",
context.getChannel().getRemoteIP(),
context.getChannel().getLocalIP(),
context.getGaeaRequest().getProtocol().getSdpEntity(),
com.bj58.spat.gaea.server.util.ErrorState.ParaException, e);
}
try {
entity.News returnValue = serviceProxy.getNewsByID(arg0);
return new com.bj58.spat.gaea.server.contract.context.GaeaResponse(returnValue, listOutPara);
}
catch (Exception e) {
throw new ServiceFrameException("method:NewsServiceProxyStub1439546304065.getNewsByID--msg:invoke real service error",
context.getChannel().getRemoteIP(),
context.getChannel().getLocalIP(),
context.getGaeaRequest().getProtocol().getSdpEntity(),
com.bj58.spat.gaea.server.util.ErrorState.ServiceException, e);
}
}
throw newServiceFrameException("method:NewsServiceProxyStub1439546304065.getNewsByID--msg:not fond method error",
context.getChannel().getRemoteIP(),
context.getChannel().getLocalIP(),
context.getGaeaRequest().getProtocol().getSdpEntity(),
com.bj58.spat.gaea.server.util.ErrorState.NotFoundMethodException, null);
}
//............省去多个服务
/**********************具体的服务调用end ********************************************/
public GaeaResponse invoke(GaeaContext context) throws ServiceFrameException
{
String methodName = ((RequestProtocol)context.getGaeaRequest().getProtocol().getSdpEntity()).getMethodName();
if(methodName.equalsIgnoreCase("getNewsByID"))
{
return getNewsByID(context);
}
if(methodName.equalsIgnoreCase("getNewsByCateID"))
{
return getNewsByCateID(context);
}
if(..){
..
}
if(..){
..
}
if(..){
..
}
if(..){
..
}
throw new ServiceFrameException("method:NewsServiceProxyStub1439547291816.invoke--msg:not found method ("+methodName+")",
context.getChannel().getRemoteIP(),
context.getChannel().getLocalIP(),
context.getGaeaRequest().getProtocol().getSdpEntity(),
com.bj58.spat.gaea.server.util.ErrorState.NotFoundMethodException, null);
}
}