您的位置:首页 > 理论基础 > 计算机网络

httpInvoker提供远程服务调用总结(三)

2013-04-24 16:31 211 查看
对外服务接口实现

public class RideServiceImpl implements RideService {
private static final Log
logger = LogFactory.getLog(RideServiceImpl.class);

/**
* 客户端远程调用的方法.
*
* @param service
* 服务
* @param params
* 参数
* @return 方法返回值
*/
public Object invoke(RemoteService service, Object[] params) {
if(logger.isInfoEnabled()){

logger.info("receive remote call " + service);

if(params != null){

for(int i = 0, len = params.length; i < len; i++){

logger.info("param[" + i + "]=" + params[i] + " class=" + (params[i] ==
null
?null:params[i].getClass()));
}
}
}
RemoteRes res = new RemoteRes();
res.setCode(0);
res.setErrmsg("");
if (service ==
null) {
res.setCode(-1);
res.setErrmsg("request error");

return
res;
}
String sessionId = service.getAttach();
if(logger.isInfoEnabled()){

logger.info("in rideserviceImpl sesessionId=service.getAttach()=" + sessionId);
}
if (sessionId ==
null) {
res.setCode(-2);
res.setErrmsg("session id can't be null");

return
res;
}
ApplicationData.setSessionId(sessionId);
switch (service.getType()) {
// service from spring bean
case 0:
Object obj = WebUtils.getSpringBean(service.getBean());

if
(obj == null) {
res.setCode(-2);
res.setErrmsg("service name error");

return res;
}

try
{
Object reObj =
invoke(obj.getClass(), obj, service
.getMethodName(), service.getParamType(), params);

if (reObj == null) {

if (returnVoid(obj.getClass(), service.getMethodName(),
service.getParamType())) {
res.setCode(0);
}
else {
res.setCode(0);
res.setErrmsg("method return null");
}
}
else if (reObj instanceof BFOList) {
BFOListHelper helper =
new BFOListHelper();
helper.setList((BFOList) reObj);
res.setCode(0);
res.setValue(helper);
}
else {
res.setCode(0);
res.setValue(reObj);
}

return res;
}
catch (InvocationTargetException ite) {
Throwable tr = ite.getCause();
String errMsg = tr.toString();

if(tr instanceof BizException){
BizException be = (BizException)tr;
MessageSource messageSource = ((MessageSource) ContextInit.getContext().getBean("messageSource"));

if( messageSource != null){
errMsg = messageSource.getMessage(be.getSysMsg().getKey(),
null, Locale.CHINA);
}

}

logger.error("cause " + tr);
res.setCode(-9999);
res.setErrmsg(errMsg);

return res;
}
catch
(Exception e) {

logger.error("invoke method error", e);
res.setCode(-9999);
res.setErrmsg(e.getMessage());

return res;
}
// class's static method
case 2:

try
{
Class clz = Class.forName(service.getBean());
Object reObj =
invoke(clz, service.getMethodName(), service
.getParamType(), params);

if (reObj == null) {

if (returnVoid(clz, service.getMethodName(), service
.getParamType())) {
res.setCode(0);
}
else {
res.setCode(0);
res.setErrmsg("method return null");
}
}
else if (reObj instanceof BFOList) {
BFOListHelper helper =
new BFOListHelper();
helper.setList((BFOList) reObj);
res.setCode(0);
res.setValue(helper);
}
else {
res.setCode(0);
res.setValue(reObj);
}

return res;
}
catch
(Exception e) {

logger.error("invoke method error", e);
res.setCode(-9999);
res.setErrmsg(e.getMessage());

return res;
}
case 3: //SQL引擎的调用
sqlEngineCall(service,params,res);

return
res;
default:

break
;

}
ApplicationData.setSessionId(null);
return res;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐