1

One of our Java based Swing applications has a class which creates an java.util.concurrent.ExecutorService instance used to run SwingWorkers. Before the executor service is returned, a custom PropertyChangeListener is added to the instance of sun.awt.AppContext to shutdown the executor service when the AppContext is 'disposed'.

I am investigating all hurdles that may arise when we migrate the code base from Java 8 to 15. The class AppContext is no longer available from Java 9 on.

Is there a suitable replacement mechanism in current Java JDK to recreate this?

The current code looks something like this:

ExecutorService service = new ThreadPoolExecutor(...); 
final ExecutorService es = service;

AppContext.getInstance().addPropertyChangeListener(
    AppContext.DISPOSED_PROPERTY_NAME,
    new PropertyChangeListener()
    {
        @Override
        public void propertyChange(PropertyChangeEvent pce)
        {
            // here some code, where we extract executor service from property change event
            ExecutorService executorService = ...;
            AccessController.doPrivileged(new PrivilegedAction<Void>()
            {
                executorService.shutdown();
            });
        }
    }
);
Andreas N
  • 804
  • 7
  • 18
  • Does this answer your question? [What is the Java 9+ equivalent for removing a class from AppContext; need to reload PrintServiceLookup class](https://stackoverflow.com/questions/60763560/what-is-the-java-9-equivalent-for-removing-a-class-from-appcontext-need-to-rel) – Mike 'Pomax' Kamermans Mar 24 '21 at 16:51
  • Alternatively, have you looked at https://stackoverflow.com/questions/56388086/package-sun-awt-does-not-exist to enable cross-compilation? – Mike 'Pomax' Kamermans Mar 24 '21 at 16:54
  • @Mike'Pomax'Kamermans: I have seen the second one, but both are not an option. (Although the --release option is enabled.) While I'm at it, I want to remove any class usages from sun.* packages, as they are not supported, public interface. https://www.oracle.com/java/technologies/faq-sun-packages.html – Andreas N Mar 24 '21 at 17:43
  • then you'll have to either rephrase your question to something like "what do I need to do to migration from sun.* in java 8 to java 9" or post a separate question, because that's a different matter entirely (and possible even off-topic because a _lot_ of people have had to migrate, so someone _must_ have documented that process somewhere already. If not in a blog post, then certainly in java user groups) – Mike 'Pomax' Kamermans Mar 24 '21 at 17:48
  • 2
    Let’s ask this way: under which circumstances was an `AppContext` disposed in an ordinary Swing application? Which feature do you lose when you just drop this code without any replacement? – Holger Mar 25 '21 at 07:52

0 Answers0