私は大気フレームワークを使用しており、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();
}
}
}
//..
}