0 Replies Latest reply on Dec 25, 2010 12:43 PM by Charley Weed

    a4j:push and thread problem

    Charley Weed Newbie

      Hi,


      I'm trying to use a4j:push and am having problems with my listener thread.  I'm able to kick off the event thread and register the listener.  However on the 2nd or more iteration of my event thread, all my thread properties go NULL (agent, agentStateManager, listener).


      Am I missing something simple here?  Here's some code.  I'm at a standstill!




      BEAN CODE:
      
      @Name("agentStatePushAction")
      @Scope(ScopeType.SESSION)
      @Startup
      @AutoCreate
      public class AgentStatePushAction
              implements Runnable, Serializable {
      
          private final static Logger logger = Logger.getLogger(AgentStatePushAction.class);
      
      //    @Logger
      //    protected Log logger;
      
      //    @In
      //    private StatusMessages statusMessages;
      
          @In("#{agentStateManager}")
          private AgentStateManager agentStateManager;
      
          @In
          private UserAgent agent;
      
          @Out
          private AgentStateInfo agentState = new AgentStateInfo();
      
          private Thread thread;
          private boolean enabled = false;
      
          private PushEventListener listener;
      
          @Override
          public void run() {
      
              while (thread != null) {
                  try {
      
                      logger.info("Getting agent state for agentId # " + agent.getUserId());
                      agentState = agentStateManager.getAgentStateInfo(agent.getUserId());
      
                      if (agentState == null) {
      
                          createDefaultAgentContext();
                      }
      
                      listener.onEvent(new EventObject(this));
                      Thread.sleep(1000);
                  }
                  catch (Exception e) {
      
                      logger.error(e);
                      createDefaultAgentContext();
                      thread = null;
                  }
              }
          }
      
      
          public void addListener(EventListener listener) {
              if (this.listener != listener) {
                  this.listener = (PushEventListener) listener;
              }
          }
      
          public synchronized void start() {
              if (thread == null) {
                  thread = new Thread(this);
                  thread.setDaemon(true);
                  thread.start();
                  setEnabled(true);
                  try {
                      Thread.sleep(1000);
                  }
                  catch (Exception e) {
                      logger.error("Problem with thread sleep!", e);
                  }
              }
          }
      
          public synchronized void stop() {
              if (thread != null) {
                  setEnabled(false);
                  thread = null;
              }
          }
      
          public void createDefaultAgentContext() {
      
              agentState = new AgentStateInfo();
              agentState.setAgentState(AgentStateEnum.LOGGED_OUT);
          }
      
          public boolean isEnabled() {
              return enabled;
          }
      
          public void setEnabled(boolean enabled) {
              this.enabled = enabled;
          }
      }
      
      XHTML SNIPPET
      
                  <h:form>
                      <a4j:push interval="1000" eventProducer="#{agentStatePushAction.addListener}"
                                reRender="push, callActionDiv, agentStateDiv" enabled="#{agentStatePushAction.enabled}" id="push"/>
                  </h:form>
      
      </code>





      Todd