• Post Reply Bookmark Topic Watch Topic
  • New Topic

Listeners with ExecutorService - how to inform the UI with MVP  RSS feed

 
olze oli
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

i am currently learning designs in java. I am working on an application which is like a FTP Client. It has a UI with 3 Panels, one Panel is for start/stop downloading (buttons), one panel represents a JTable which holds 0-n files/directories (with meta information), and one panel holds just a status bar.
When the user adds some files and clicks on "download", the download should start - what it does.
The Metainformation describes how many threads are allowed to download this file/directory.

Now i am going to move to MVP (resp. presenter first) and i am wondering how i should implement the ExecutorService to inform my UI about the current state.
At the moment i have a FtpWorkerPool which encapsulates a ExecutorService, it has 3 methods to add a FtpFile - addFtpFile(FtpFile, TransferListener) - addFtpFile(FtpFile, TransferListener, DetailTransferListener) and addFtpFile(FtpFile)
That does not look good to me, i would prefer to use something like FtpWorkerPool.addListenerForFtpFile(FtpFile, Listener) and now i have the problem:
How should i get to these informations?
I created a and thought i could access it with

But the queue is always empty? Must i extend the executor service and override the methods which are responsible for adding the jobs, so i can save them in a map (e.g. Map jobs<FtpFile, Listeners>) so i can add listeners later to the jobs?
 
Henry Wong
author
Sheriff
Posts: 23280
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
olze oli wrote:
I created a and thought i could access it with

But the queue is always empty? 


Well, you created an executor that can grow to a ridiculous number of threads, hence, it is likely taking runnable tasks off of the queue (so that it can work on it), as fast as you are placing tasks onto the queue. If you really want to see stuff in the queue, you may have to add tasks to the queue at a faster rate.

Henry
 
olze oli
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry, makes sense. But how can i access the currently running Thread and add a listener to it? Do i have to hold a reference to it (and have redundancy because i have a reference and the queue has one) ? 
 
Henry Wong
author
Sheriff
Posts: 23280
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
olze oli wrote:Thanks Henry, makes sense. But how can i access the currently running Thread and add a listener to it? 


Access the thread to listen to what? You have to be specific. 

As for the tasks themselves, well, you wrote them. You can make them Observers, which allows listeners to be registered.

Henry
 
olze oli
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use case: i have a FTP Library which should handle all the download related stuff. This library should be used with vaadin, swing and other ui frameworks. The user should be able to download a directory as job. The default limit for download threads is at the moment 3. That means: the ui must be able to do something like:



the transferListener is a general one, which means i show only information about the overal state of the download (e.g. 500mb received). But the detail listener should be informed about each 4k byte block which is transfered for each of the 3 workers (which is dynamic, so i also need this information how many are currently in working state).
How can i get e.g. getCountOfWorkerThreadsFor for a FileToDownload? I need a map or something like this to see which FileToDownload is currently in the queue, or is currently being executed.
 
Henry Wong
author
Sheriff
Posts: 23280
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
olze oli wrote:... But the detail listener should be informed about each 4k byte block which is transfered for each of the 3 workers (which is dynamic, so i also need this information how many are currently in working state).
How can i get e.g. getCountOfWorkerThreadsFor for a FileToDownload? I need a map or something like this to see which FileToDownload is currently in the queue, or is currently being executed.


The executor runs your code. Your code decides what to download. Your code decides what parts to download. etc. etc. etc. Why can't your code log what it is currently doing somewhere, so that other threads, presumably also your code, can look to see what is happening.

Henry
 
olze oli
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I extended ThreadPoolExecutor, overriding the beforeExecute and afterExecute - worked. Then i had to access the job again, so i used http://www.javaspecialists.eu/archive/Issue228.html - its ugly but it works.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!