0

私は大気フレームワークを使用しており、ServletContextListener で開始されたスレッドを実行しています。

このスレッドがコンテキストに入れるデータをブロードキャストしたいのですが、ServletContextAttributeListener を実装できる場所でどのクラスを拡張する必要があるか混乱しています。

私は現在 OnMessage クラスを拡張していますが、それでは十分ではないようです。

public class SerialComInit implements ServletContextListener {
        //..
        private static final String SHUTDOWN_REQ = "SHUTDOWN";
        public void attributeAdded(ServletContextAttributeEvent event) {

            queue = (BlockingQueue<String>) event.getServletContext().getAttribute("serialPortData");

            //TODO Use jackson to generate JSON
            //TODO Implement SHUTDOWN command on server side
            //we always get a null here on first try that's why I added null check
            if (queue == null){
                System.out.println("Queue is empty");
            } else {
                String item;
                try {
                    //blocks while queue is empty
                    while ((item = queue.take()) != SHUTDOWN_REQ) {
                        System.out.println("*******WEB*******"+item+"*******");
                        //TODO Broadcast message to connected clients
                    }
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    System.out.println("queue error");
                    //e.printStackTrace();
                }
            }       
        }
   //..
}
4

1 に答える 1

0

AbstractReflectorAtmosphereHandler を拡張することで問題を解決しました。

私の ServletContextAttributeListener -> attributeAdded 呼び出しで使用

MetaBroadcaster.getDefault().broadcastTo("/", "message");
于 2013-03-06T17:23:31.030 に答える