Skip to content

Implementation of a Promise object using Java RMI.

Notifications You must be signed in to change notification settings

taciosd/rmi-promise

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 

Repository files navigation

rmi-promise

Implementation of a Promise object using Java RMI.

Example

Client code

class Client {
    
    public void onBtnClick() {
        RmiPromise<Double> promise = service.executeHeavyWork();
            
        promise.registerCallback(new DefaultCallback() {
            @Override
            public void onPhaseChanged(Phase phase) throws RemoteException {
                Platform.runLater(() -> {
                    label.setText("Phase changed! " + phase));
                    if (phase.equals(Phase.SUCCESS)) {
                        resultLabel.setText("Success!!!");
                    }
                });
            }
            
            @Override
            public void onProgressChanged(int newProgress) throws RemoteException {
                Platform.runLater(() -> progressIndicator.setProgress(newProgress));
            }
        });
    }
}

Server code

class Server {
    
    public RmiPromise<Double> executeHeavyWork() throws RemoteException {
        
        RmiPromiseImpl<Double> promise = new RmiPromiseImpl<>();
            
        Thread thread = new Thread(() -> {
            try {
                double result = 0.0;
                            
                for (int i = 0; i < 100; i++) {              
                    if (promise.hasCancelOrder()) {
                        // Rollback, update promise and return.
                        promise.finish(Phase.CANCELLED);
                        return;
                    }
                    
                    result = Util.calculateDifficultNumber(result);
                    
                    promise.setProgress(i+1);
                }
                
                promise.finish(result);
            }
            catch (Exception e) {
                e.printStackTrace();
                promise.finish(e);
            }
        });
        
        thread.start();
        
        return promise;
    }    
}

About

Implementation of a Promise object using Java RMI.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages