【问题标题】:Call a method in Spring mvc service cost too much time在 Spring mvc 服务中调用方法花费太多时间
【发布时间】:2014-03-03 03:31:17
【问题描述】:

我的英语不太好。先写代码。

    @ResponseBody
    @RequestMapping(value = "testcall")
    public UnifiedResponse testMethodCall(
            HttpServletRequest request,
            HttpServletResponse response) {
        UnifiedResponse unifiedResponse = new UnifiedResponse();
        logger.info("t1:"+System.currentTimeMillis());
        Fun(0,1452,"abd",1);
        logger.info("t2:"+System.currentTimeMillis());
        userSignService.testcall();
        logger.info("t3:"+System.currentTimeMillis());

        return unifiedResponse;
    }


    String  Fun(int i,long l,String s,int ii){
        logger.info("f1:"+System.currentTimeMillis());
        return "";
    }

和日志输出:

 t1:1393816077311
 f1:1393816077312
 t2:1393816077312
 f2:1393816077345
 t3:1393816077410

testcall() 是服务中的方法,既没有参数也没有返回值。只需输出时间戳即可。

服务是这样实现的:

@Service
public class UserSignServiceImpl extends BaseServiceImpl implements IUserSignService {
 public void testcall() {
    logger.info("f1:"+System.currentTimeMillis());
 }

}

通过注解实例化

@Autowired
IUserSignService userSignService;

这只发生在某些服务器上。其他的 t3-t1 小于 2ms。所有服务器使用相同版本的 JDK 和 Resin。

为什么?

调用者和方法之间的堆栈跟踪:

UserSignServiceImpl.testcall() line: 448    
NativeMethodAccessorImpl.invoke0(Method, Object, Object[]) line: not available [native method]  
NativeMethodAccessorImpl.invoke(Object, Object[]) line: 39  
DelegatingMethodAccessorImpl.invoke(Object, Object[]) line: 25  
Method.invoke(Object, Object...) line: 597  
AopUtils.invokeJoinpointUsingReflection(Object, Method, Object[]) line: 319 
ReflectiveMethodInvocation.invokeJoinpoint() line: 183  
ReflectiveMethodInvocation.proceed() line: 150  
TransactionInterceptor.invoke(MethodInvocation) line: 110   
ReflectiveMethodInvocation.proceed() line: 172  
ExposeInvocationInterceptor.invoke(MethodInvocation) line: 90   
ReflectiveMethodInvocation.proceed() line: 172  
JdkDynamicAopProxy.invoke(Object, Method, Object[]) line: 202   
$Proxy49.testcall() line: not available 
OtherController.testMethodCall(HttpServletRequest, HttpServletResponse) line: 6329  

【问题讨论】:

  • userSignService 是从哪里来的?
  • 公共接口 IUserSignService ;像这样实现:@Service public class UserSignServiceImpl extends BaseServiceImpl implements IUserSignService
  • 很公平,但我看不到它在哪里声明或实例化。没有它,我们就没有合理的方式来回答这个问题。
  • @Autowired IUserSignService userSignService;
  • 可能是慢速服务器的磁盘比其他的慢。删除所有记录调用(除了最后一个,它会显示总时间),看看它是否有区别。

标签: java spring resin


【解决方案1】:

原因是拦截器。一旦我删除它,t3-t2 下降到 1ms。 org.springframework.orm.hibernate3.HibernateTransactionManager com.xxx.service 包中的所有方法都会触发这个管理器。我不知道内部机制,但我猜可能是这样的: 较快的服务器进程在 CPU 中有 12M L3 缓存,较慢的没有。服务方法处处调用,使其使用最频繁。较快的服务器可能缓存它,因此处理速度更快。 我不确定。还有其他解释吗?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-27
    • 1970-01-01
    • 2016-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多