0

I made an jsf application.This application has a menu containing start,stop buttons.When start is pressed , application starts to get data from web sites, and updates its database.The application also has progress bar for update process.However,this process takes a long time to finish.I want that when i close my browser , it should go on updating database.Besides, when i open it again, i should get previous state.However, this isn't happening.When i close browser the application closes too.How do i do that?

Thanks.

Viplime
  • 141
  • 4
  • 14
  • Could we see your backing bean code? – Javi Pedrera Nov 12 '12 at 12:20
  • Codes are more than 1200 lines, is it necessary ? Algorithm is : 1.Download files from net. 2.Open files and update database until no files remain. 3.Close files , and done ! Are you asking for java codes or xhmtl codes ? – Viplime Nov 12 '12 at 12:28
  • I would like to know what is the scope of your Bean which manages the process – Javi Pedrera Nov 12 '12 at 12:38
  • 1
    Note: If your method has more than 200 lines, you have a design problem. It would be better to review this method and apply [loose coupling](http://en.wikipedia.org/wiki/Loose_coupling) and [high cohesion](http://en.wikipedia.org/wiki/Cohesion_(computer_science)). Related: [What is high cohesion and how to use it / make it?](http://stackoverflow.com/q/10830135/1065197) – Luiggi Mendoza Nov 12 '12 at 14:17
  • You're right but i mean total line in my class.I don't have a method with more than 1200 lines.Thanks for reply :) . – Viplime Nov 12 '12 at 23:52
  • Still, looks like lot of code for a single class. – Luiggi Mendoza Nov 13 '12 at 01:07

1 Answers1

1

In my case, I would not extend the session life. Instead, create a task and add the object that performs the task into a Queue in an @ApplicationScoped bean and save in database (or any other place) the user that started the job and the status of the job.

When the user logs off (manually logging off or closing the web browser), the task will still be executed because is managed by the whole application (not by a request nor user session). When the user logs in again, he/she could ask to this application queue about the status of the task.

You will need (at least):

  • An @ApplicationScoped managed bean that will contain and handle the tasks.
  • A way to handle and execute one or more tasks at the same time. This could be achieved with a ExecutorService or similar technologies. Note: don't dare to manually start new threads by your own, this will only lead to kill your web application server.
  • An structure to map the user with the tasks he/she has started. It could be mapped with a Map<String, List<Task>> in order that a single user could have more than 1 task at the moment. It would be better to save this in a database (or similar) in order to have a log for these tasks that don't reside in memory. You can even design this in order that if you undeploy the web application or the server suddenly shut downs, you could restart the tasks at hand from a savestate point.
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • Good suggestion.Thanks for reply.My application will be used only by admin.Therefore,only one instance of my app should be run and all admins should get the same progress data.I guess i don't need queue for this.Besides, when i make my app @ApplicationScoped and close browser will the app keep on running? – Viplime Nov 13 '12 at 00:03
  • yes, the `@ApplicationScoped` managed bean lives until the application is undeployed or the web application server (Tomcat, JBoss, GlassFish, etc.) shuts down. – Luiggi Mendoza Nov 13 '12 at 01:04
  • I tried @ApplicationScoped and it works as i want.Thank you.I solved the problem thanks to you :) . – Viplime Nov 13 '12 at 22:35