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

时间:2019-05-03 04:27:46

标签: java

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

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

当我在运行时调试和检查功能参数具有mulipleArgyments args1,arg2,arg3时。参数之一指向Employee对象。如何在下面的异常块中获取函数参数。

当我说有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;                
            }

0 个答案:

没有答案