JavaFX network background task with javafx.concurrent


Question

I'm implementing an JavaFX application which is communicating with a mobile phone via wifi (android).

Therefore I have a server thread on the JavaFX running in a background process:

public class NetworkService implements Runnable { 

    private final ServerSocket serverSocket;
    private final ExecutorService pool;

    private RoutePlannerJFX application;
    private UserData userData;


    public NetworkService(ExecutorService pool,
                          ServerSocket serverSocket,
                          RoutePlannerJFX app,
                          UserData data) {
        this.serverSocket = serverSocket;
        this.pool = pool;
        application = app;
        userData = data;
    }


    public void run() {
        try {
            while ( true ) {
                Socket cs = serverSocket.accept(); 
                pool.execute(new Handler(serverSocket, cs, application, userData));
            }
        } catch (IOException ex) {
            System.out.println("--- Interrupt NetworkService-run");
        }
        finally {
            System.out.println("--- Ende NetworkService(pool.shutdown)");
            pool.shutdown();  //keine Annahme von neuen Anforderungen
            try {
                pool.awaitTermination(4L, TimeUnit.SECONDS);
                if ( !serverSocket.isClosed() ) {
                    System.out.println("--- Ende NetworkService:ServerSocket close");
                    serverSocket.close();
                }
            } catch ( IOException e ) { }
            catch ( InterruptedException ei ) { }
        }
    }
}

which has a handler:

public class Handler implements Runnable {

    private final Socket client;
    private final ServerSocket serverSocket;

    private RoutePlannerJFX application;
    private UserData userData;


    Handler(ServerSocket serverSocket,Socket client, RoutePlannerJFX app, UserData data) {
        this.client = client;
        this.serverSocket = serverSocket;
        application = app;
        userData = data;
    }


    public void run() {
        StringBuffer sb = new StringBuffer();
        PrintWriter out = null;
        try {
            System.out.println( "running service, " + Thread.currentThread() );
            out = new PrintWriter( client.getOutputStream(), true );
            BufferedReader bufferedReader = 
                new BufferedReader(
                    new InputStreamReader(
                        client.getInputStream()));
            char[] buffer = new char[100];
            int anzahlZeichen = bufferedReader.read(buffer, 0, 100); 
            String nachricht = new String(buffer, 0, anzahlZeichen);
            String[] werte = nachricht.split("\\s");


            System.out.println(nachricht+"\n");

            POI poi = new POI(nachricht);
            userData.addItemToPoiList(poi);

            application.setScene("INSTRUCT");

        } catch (IOException e) {System.out.println("IOException, Handler-run");}
        finally { 
            System.out.println(sb);  //Rückgabe Ergebnis an den Client
            if ( !client.isClosed() ) {
            System.out.println("****** Handler:Client close");
            try {
                client.close();
                } catch ( IOException e ) { }
            } 
        }
    }
}

The application has a public method to change the scene (setScene()).

That's the way I wish I could do it, but now I know, that I cannot switch the scene in my backgroundprocess.

Has anyone an idea how to implement this problem? I need to fire an action, when my backgroundprocess receives a message from the the client, but I don't know what's the best way to do that… I already found javafx.concurrent, but which do I have to use and how?

Thank's in advance!

1
2
9/2/2012 9:00:23 PM

Accepted Answer

For the client side you can use either a Task or a Service to run in a separate thread & update the GUI as described in the documentation

You can check this post too JavaFX Async Task

2
9/3/2012 10:34:57 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon