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;
}
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;
}
相关文章推荐
- httpInvoker提供远程服务调用总结(二)
- httpInvoker提供远程服务调用总结(一
- httpInvoker提供远程服务调用总结(一)
- WebService学习总结(四)——调用第三方提供的webService服务
- WebService学习总结(四)——调用第三方提供的webService服务
- WebService学习总结(四)——调用第三方提供的webService服务
- WebService学习总结(四)——调用第三方提供的webService服务
- WebService学习总结(四)——调用第三方提供的webService服务
- WebService学习总结(三)——调用第三方提供的webService服务
- WebService学习总结(四)——调用第三方提供的webService服务
- WebService学习总结(四)——调用第三方提供的webService服务
- WebService学习总结(四)——调用第三方提供的webService服务(还未学习到,先保留着)
- SpringCloud03 Ribbon知识点、 Feign知识点、利用RestTemplate+Ribbon调用远程服务提供的资源、利用feign调用远程服务提供的资源、熔断
- WebService学习总结——调用第三方提供的webService服务
- 本地提供/调用服务与远程提供/调用服务(dubbo)的区别比较
- WebService学习总结(四)——调用第三方提供的webService服务
- WebService学习总结(四)——调用第三方提供的webService服务
- WebService学习总结(四)——调用第三方提供的webService服务
- WebService学习总结(四)——调用第三方提供的webService服务,使用cxf
- 用于 J2ME 的 Web 服务 API 进行远程服务调用 API