Note that there are some explanatory texts on larger screens.

plurals
  1. POSetting bean properties from a thread
    text
    copied!<p>I have a <code>@ViewScoped</code> bean that passes itself to a Thread object.</p> <pre><code>class SomeThread implements Runnable { private SomeBeanClass someBean; public SomeThread(SomeBeanClass someBean) { this.someBean = someBean; } public void run() { // do some calculations and set bean properties someBean.setProperty("blabla"); someBean.setLocked(false); } </code></pre> <p>This seems to work most of the times, but not very stable. In my view, I use PrimeFaces' <code>p:poll</code> to refresh a <code>h:inputTextarea</code> which displays the property I set in the thread. It works... sometimes. Sometimes I see no text at all, sometimes it only displays new text for the first poll.</p> <p>The boolean property I set in the thread doesn't seem to be up to date if I call an action method from my form while the thread is running (the thread set it to <code>false</code>, after that I invoke the action method from the form and <code>isLocked()</code> still returns <code>true</code>).</p> <p>Is there any more clever way to do this? Passing a bean to a thread seems wrong to me. But how else could I achieve this?</p> <p><strong>UPDATE</strong></p> <p>My goal is as follows: I want the user to be able to see changes made to some files that lie on a remote server (without refreshing). Basically, I want the user to see a <code>tail -f</code> of a remote file in an <code>inputTextarea</code>. (The connection to the remote server is done by the thread using SSH [Ganymed library]).</p> <p>So I put the <code>inputTextarea</code> in the form and refresh it using <code>p:poll</code>. I update the String that is bound to the <code>inputTextarea</code> in the thread (the user invokes the action that will start the thread using a <code>commandButton</code>. Also, I want the user to stop the <code>tail -f</code> via a <code>selectBooleanCheckbox</code>. So I put a condition in the <code>run()</code> method of the thread:</p> <pre><code>if(someBean.isFinished()) { // logout and break; } </code></pre> <p>The changes made to the <code>selectBooleanCheckbox</code> are reflected in the run method straight away because I used <code>&lt;f:ajax /&gt;</code> - but bean properties set from the run method are sometimes not "updated" right away.</p> <p>I'm using Tomcat 7.0.12, JSF 2 (MyFaces) and PrimeFaces 2.2.1</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload