Best practice in synchronized methods in jsf and java

I have a JSF application in which users create some files. The problem is that they have to download them and also download confirmation messages, and the upload / download process is exclusive, only one user at a time, because authentication requires a technical user / password. My question is: how can I make the wait process transparent to the user, a kind of protocol, for example:

  • Waiting for connection
  • Authentication
  • upload file
  • download confirmation file
  • done
+5
source share
1 answer

Use one stream performer.

@ManagedBean
@ApplicationScoped
public class FileManager {

    private ExecutorService executor;

    @PostConstruct
    public void init() {
        executor = Executors.newSingleThreadExecutor();
    }

    public Result process(Task task) throws InterruptedException, ExecutionException {
        return executor.submit(task).get();
    }

    @PreDestroy
    public void destroy() {
        executor.shutdownNow();
    }

} 

Result - javabean, , Task :

public class Task implements Callable<Result> {

    private Data data;

    public Task(Data data) {
        this.data = data;
    }

    @Override
    public Result call() throws Exception {
        Result result = process(data); // Do your upload/download/auth job here.
        return result;
    }

}

Data - javabean, ( ?). , bean :

@ManagedBean
@RequestScoped
public class Bean {

    @ManagedProperty("#{fileManager}")
    private FileManager fileManager;

    public void submit() {
        try {
            Data data = prepareItSomehow();
            Result result = fileManager.process(new Task(data));
            // Now do your job with result.
        }
        catch (Exception e) {
            // Handle
        }
    }

    // ...
}

, .

EJB, .

+3

All Articles