首页 > 解决方案 > 如何在异常块中获取 java.util.function 输入参数

问题描述

如何在异常块中获取 java.util.function 的输入参数/参数

在下面的代码中,我想获取提供给函数的输入参数。当 processWithRetry(..) 抛出 ApplicationException 或任何其他异常并且我们重试 10 次尝试重试时。当重试失败 10 次时,我们需要在 Exception 块中哪个 Employee 对象失败

当我在运行时调试和检查函数参数有 mulipleArgyments args1、arg2、arg3 时。其中一个参数指向 Employee Object。如何在下面的异常块中获取函数参数。

当我说 function.applyThen(..) 方法存在但没有 function.getParameters() 或 getArgs().. 方法可用。

catch (ApplicationException e) {
    if (i == nRetries - 1) {
        throw e;
    } 
} catch (Exception e) {             
    e.printStackTrace();    
    Long failed = 0L;
    result = (T) failed;                
}

下面是完整的代码和流程。

Public class EmployeeProcessor {

    public void processEmployee() {

        BlockingQueue<Optional<Employee>> queue = new ArrayBlockingQueue<>(100000);
        Thread[] threads = new Thread[5];
        for (int i = 0; i < 5; i++) {
            threads[i] = new Thread(() -> {
                try (for every DB connection from pool) {
                    while (true) {
                        Optional<Employee> take = queue.take();
                        if (!take.isPresent()) {
                            break;
                        } else {
                            Employee employee = take.get();
                            long count = processWithRetry(connectionObject, 10, dbSession -> {                          
                                // many statements here, but no try & catch block, but processing might throw ApplicationException      
                                return 0L;
                            });
                        }
                    }       
                });
                threads[i].start();
            }
        }   

        for (String filePath : fileList) {          
            processFileByFile(filePath, queue, connectionObject);

        }
    }

    protected void processFileByFile(String filePath, BlockingQueue<Optional<Employee>> queue, DBconnectionObject)
            throws IOException, InterruptedException {
        BufferedReader reader = new BufferedReader(new FileReader(filePath));
        try {
            String dataLine;    

            while ((dataLine = reader.readLine()) != null) {

                if (dataLine != null && dataLine != "") {                   
                    List<String> eachLineList = Arrays.asList(eachLine.split(","));
                    queue.put(Optional.of(
                            new Employee(eachLineList.get(0), eachLineList.get(1), eachLineList.get(2))));
                }

            }           
        } finally {
            reader.close();
        }
    }


    class Employee {
        String firstName;
        String lastName;
        String age;

        public Employee(String firstName, String lastName, String lastName) {
            this.firstName = firstName;
            this.lastName = lastName;
            this.age = age;
        }
    }


    @SuppressWarnings("unchecked")
    <T> T processWithRetry(connectionObject connectionObject, int nRetries, Function<dbSession, T> function)
            throws IllegalStateException, IllegalArgumentException, ApplicationException, UnsupportedOperationException {
        if (nRetries < 1) {
            throw new IllegalArgumentException("Wrong number of attempts: " + nRetries);
        }

        T result = null;

        for (int i = 0; i < nRetries; i++) {
            try {
                result = function.apply(connectionObject);
                break;
            } catch (ApplicationException e) {
                if (i == nRetries - 1) {
                    throw e;
                } 
            } catch (Exception e) {             
                e.printStackTrace();

                Long failed = 0L;
                result = (T) failed;                
            }
        }
        return result;
    }

}

获取我在调试期间在 inpsect 中看到的参数之一。在下面的异常块中。

catch (ApplicationException e) {
                if (i == nRetries - 1) {
                    throw e;
                } 
            } catch (Exception e) {             
                e.printStackTrace();

                Long failed = 0L;
                result = (T) failed;                
            }

标签: java

解决方案


推荐阅读