mercredi 22 juin 2016

UndeclaredThrowableException

I'm working with concurrency in Java. As a parameter I pass list, it isn't empty. When program try run command f.get() I have exception. Maybe you have some ideas what the problem can be in this situation?

private List<PerfInfo> getResults(List<ScheduledFuture<PerfInfo>> results) {
    return results.stream().map(f -> {
        try {
            return f.get();
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        }
        return null;
    }).collect(Collectors.toList());
}

Method that invoke getRezults

@Test
public void testGetPerformance() throws IOException {
    ReportService reportService = ArubaServiceFactory.getArubaServiceFactory().getReportService();
    ArubaHitService arubaHitService = ArubaServiceFactory.getArubaServiceFactory().getArubaHitService();

    ScheduledThreadPoolExecutor pool = new ScheduledThreadPoolExecutor(THREAD_COUNT);
    List<ScheduledFuture<PerfInfo>> results = new ArrayList<>();
    List<Long> ids = getReportIds();
    ids.forEach(id -> results.add(pool.schedule(
            new QueryGetASHit(reportService, arubaHitService, id), getDelay(), TimeUnit.SECONDS)));

    List<PerfInfo> perfInfos = getResults(results);
    writeAsText(perfInfos,"testGetPerformance");
    System.out.println(perfInfos);

    pool.shutdown();
    try {
        pool.awaitTermination(10, TimeUnit.MINUTES);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

stack trace

at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)
at com.tilr.tc1.reportrepository.perf.TestPerformance.getResults(TestPerformance.java:122)
at com.tilr.tc1.reportrepository.perf.TestPerformance.testGetPerformance(TestPerformance.java:63)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:232)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:89)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:175)
at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:119)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Caused by: java.lang.reflect.UndeclaredThrowableException
at com.sun.proxy.$Proxy50.getASHits(Unknown Source)
at com.tilr.tc1.reportrepository.perf.TestPerformance$QueryGetASHit.call(TestPerformance.java:204)
at com.tilr.tc1.reportrepository.perf.TestPerformance$QueryGetASHit.call(TestPerformance.java:188)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.GeneratedMethodAccessor58.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.tilr.tc1.reportrepository.common.service.ServiceFactory$1.invoke(ServiceFactory.java:549)
... 9 more

Method, about which ask Tamas

 @SuppressWarnings("unchecked") //unsafe cast needed because Proxy.newProxyInstance returns an object (it doesn't know which of the passed in interfaces it must return, even if there is only one)
protected <T> T getServiceToUse(final Class<T> proxyInterface, final T target) {
    if (logTimings) {
        InvocationHandler ih = new InvocationHandler() {
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                long begin = System.currentTimeMillis();
                Object o = method.invoke(target, args);
                long took = System.currentTimeMillis() - begin;
                if (logger.isDebugEnabled()) {
                    logger.debug("Call " + method.getName() + " took "
                            + took + "ms.");
                }
                return o;
            }
        };
        return (T) Proxy.newProxyInstance(proxyInterface.getClassLoader(), new Class[]{proxyInterface}, ih);
    } else {
        return target;
    }
}





Aucun commentaire:

Enregistrer un commentaire