How do I get the ProcessInstanceId in my Interceptor?

Hi,

I’m currently working on an interceptor to handle the suspended state, and I need to retrieve the processInstanceId within the interceptor. The goal is to send an HTTP request to an API where the processInstanceId is stored.

I’ve attempted to use reflection to obtain this ID, but doing so has resulted in an OptimisticLockingException.

Is there an alternative way to acquire the processInstanceId from the context?

Thank you in advance for your help!

private synchronized <T> String getProcessInstanceId(Command<T> command) {
        String processInstanceId = null;
        try {

            // Access the protected field processInstanceId via reflection
            Field field = command.getClass().getSuperclass().getDeclaredField("processInstanceId");
            field.setAccessible(true);
            processInstanceId = (String) field.get(command);
            field.setAccessible(false);
        } catch (NoSuchFieldException | IllegalAccessException e) {
            System.err.println("Error accessing processInstanceId: " + e.getMessage());
        }
        return processInstanceId;
    }