Title: MessageListener

Hi,

I implemented a Bean as a MessageListener to a Topic queue. The Bean gets new data whenever something arrives on the topic (omnessage method).

Now i have an autoupdatetable which gets the data from the bean. All works perfect!

The problem is whenever the Session Bean timeouts, the i would like to unsubscribe from the topic but it is impossible to get the FacesContext when running

In the onMessage method. What i wanted to do is whenever the onmessage method is called, then check if the session is still alive, and if not to unsubscribe

From the topic as done in the stop action method but FacesContext.getCurrentInstance() returns null in the onMessage method.

Many thanks for your suggestions.

Kind regards

Dominique

P.S.

Pls take a look to the good:


public class ListenerBean implements MessageListener, Serializable {
       
        private List testList;
       
        private List<String> watchList;
       
    /**
     * Session Fascade Class. The supplier holds all methods which can be used
     * against the active Session Bean.
     */
    private IPmonHistorySupplier supplier;

    /**
     * Reference to the subcribed Topic.
     */
    private TopicSubscriber subscriber;
   
    /**
     * path where the dtd file is located.
     */
    private String dtdDirectory;
   
    public ListenerBean() {
       
        TopicConnectionFactory tqcf;
        Topic                  topic;
       
        testList = new ArrayList();
        watchList = new ArrayList<String>();
       
        try {
            supplier = new IPmonHistorySupplier( "tpfssw.eds.ch", 1099 );
        }
        catch ( ServiceNotAvailableException ex) {
                ex.printStackTrace();
        }
    }
   
    /**
     * stop
     *
     * @param event ActionEvent
    */
    public void stop(ActionEvent event) {
        try {
                        System.out.println("STOP");
                        FacesContext context = FacesContext.getCurrentInstance();
                       
                        supplier.unsuscribeFromResponseTopic(subscriber);
                        System.out.println("ListenerBean: unscribed");
                } catch (Exception ex) {
                        ex.printStackTrace();
                        // TODO: handle exception
                }
    }
   
    public void start(ActionEvent event) {
        try {
                        System.out.println("START");
                        FacesContext context = FacesContext.getCurrentInstance();
                       
                        supplier.subscribeToResponseTopic( this );
                        System.out.println("ListenerBean: subscribed");
                } catch (Exception ex) {
                        ex.printStackTrace();
                        // TODO: handle exception
                }
    }

    public void onMessage(Message message) {
               
        System.out.println("onMessage");
        FacesContext context = FacesContext.getCurrentInstance();
        System.out.println("context: " + context);

                try {
                       
                    FacesContext context = FacesContext.getCurrentInstance();
                    ExternalContext econtext = context.getExternalContext();
                    HttpSession session = (HttpSession) econtext.getSession(false);

                        testList.clear();

                        // cheat 
                        for (int i = 0; i < 10; i++) {
                                testList.add(new Integer((int) (Math.random() * 100)));
                        }
                                    // cheat

                        String ipmonMessage = ((TextMessage) message).getText();

                        SAXBuilder builder = new SAXBuilder();
                        builder.setValidation(false);

                        InputSource inputSource = new InputSource(new StringReader(
                                        ipmonMessage));

                        dtdDirectory = System.getProperty("user.dir");
                        int index = dtdDirectory.indexOf("bin");
                        dtdDirectory = dtdDirectory.substring(0, index);
                        dtdDirectory = dtdDirectory + "mydtd"
                                        + System.getProperty("file.separator") + "IPMonitor.dtd";

                        inputSource.setSystemId(new File(dtdDirectory).toURL().toString());

                        Document document = builder.build(inputSource);

                        Element root = document.getRootElement();

                        Logger log;
                        log = Logger.getLogger("ch.eds.ipmonitor");
                        log.info(ipmonMessage);

                } catch (JMSException ex) {
                        // TODO Auto-generated catch block
                        ex.printStackTrace();
                } catch (JDOMException ex) {
                        // TODO Auto-generated catch block
                        ex.printStackTrace();
                } catch (IOException ex) {
                        // TODO Auto-generated catch block
                        ex.printStackTrace();
                } catch (SessionTimeoutException ex) {
                        try {
                                supplier.unsuscribeFromResponseTopic(subscriber);
                                System.out.println("ListenerBean: Session timeout");
                        } catch (ServiceNotAvailableException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        }
                */
                } catch (NullPointerException ex) {
                        try {
                                supplier.unsuscribeFromResponseTopic(subscriber);
                                System.out.println("ListenerBean: Session timeout");
                        } catch (ServiceNotAvailableException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        }
                }
    }
       
    public List getTestList()
    {
        return testList;
    }

}

Reply via email to