首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java相关 >

优化一段代码,惊人

2011-12-11 
优化一段代码,紧急!Java codepublic final void run() {UserContextAssociator.getInstance().associate(u

优化一段代码,紧急!

Java code
    public final void run() {        UserContextAssociator.getInstance().associate(userContext);        final Scope systemInfoUserScope = ScopeFactory.createUserScope(Area.SYSTEM, Level.INFO);        final boolean hasInterestInSystemInfoScope = EventMediator.hasInterest(systemInfoUserScope);        final long starttime = System.currentTimeMillis();        while (!hasFinished()) {            final Command current_command = getCommand();            final Object current_target = getTarget();            ((ServiceCommand) current_command).setMessage(this);            final long time = System.currentTimeMillis();            try {                current_command.execute(current_target);                commandSucceeded();            } catch (final RequestProcessingException e) {                // TODO this isn't very elegant but Impala requires these error pages NOT to show up in the logs..                if (!(e.getCause() instanceof ErrorPageException)) {                    final Throwable detail = e.getDetail();                    if (detail instanceof AuthenticationExpiredException || detail instanceof UserDisabledException) {                        final Scope systemDebugUserScope = ScopeFactory.createUserScope(Area.SYSTEM, Level.DEBUG);                        if (EventMediator.hasInterest(systemDebugUserScope)) {                            EventMediator.mediate(EventFactory.createEvent(systemDebugUserScope, PayloadFactory.createUserPayload(new StringBuilder().append("Execute command '").append(current_command.getClass().getName()).append("' failed. ").append(e.getMessage()).toString())));                        }                    } else {                        final Scope systemErrorUserScope = ScopeFactory.createUserScope(Area.SYSTEM, Level.ERROR);                        if (EventMediator.hasInterest(systemErrorUserScope)) {                            EventMediator.mediate(EventFactory.createEvent(systemErrorUserScope, PayloadFactory.createUserPayload(new StringBuilder().append("Execute command '").append(current_command.getClass().getName()).append("' failed.").toString(), e)));                        }                    }                }                commandFailed(e);            } catch (final Throwable e) {                final Scope systemErrorUserScope = ScopeFactory.createUserScope(Area.SYSTEM, Level.ERROR);                if (EventMediator.hasInterest(systemErrorUserScope)) {                    EventMediator.mediate(EventFactory.createEvent(systemErrorUserScope, PayloadFactory.createUserPayload(new StringBuilder().append("Execute command '").append(current_command.getClass().getName()).append("' failed. Uncaught handler exception.").toString(), e)));                }                final RequestProcessingException re;                if (e instanceof Exception) {                    re = new RequestProcessingException(ErrorCode.SYSTEM, FrameworkErrorDetail.UNCAUGHTHANDLEREXCEPTION, current_target, (Exception) e);                } else {                    re = new RequestProcessingException(ErrorCode.SYSTEM, FrameworkErrorDetail.UNCAUGHTHANDLEREXCEPTION, current_target, new Exception(e.getMessage()));                }                commandFailed(re);            } finally {                if (hasInterestInSystemInfoScope) {                    EventMediator.mediate(EventFactory.createEvent(systemInfoUserScope, PayloadFactory.createUserPayload(new StringBuilder().append("Executed command '").append(current_command.getClass().getName()).append("' on '" + current_target.getClass().getName()).append("', duration: ").append((System.currentTimeMillis() - time)).append("ms.").toString())));                }            }        }        if (hasInterestInSystemInfoScope) {            EventMediator.mediate(EventFactory.createEvent(systemInfoUserScope, PayloadFactory.createUserPayload(new StringBuilder().append("Processed message '").append(getClass().getName()).append("', duration: ").append(System.currentTimeMillis() - starttime).append("ms.").toString())));        }    } 



[解决办法]
我认为这么长的一个函数,必须先做注释,越详细越好。


热点排行