实现ApplicationListener的java Spring控制器
我正在Spring4.0.5中实现WebSocket。客户端(浏览器)保持与服务器的连接。当我的服务层更新后端的一些记录时,我想通知订阅特定主题的客户端
我希望我的控制器侦听我的ApplicationEvent并将其发布到WebSocket客户端。这可能吗
我在服务层中实现了另一个侦听器,它可以捕获事件。 控制器不能侦听ApplicationEvent是真的吗
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Controller;
import com.geneoz.service.event.MessageEvent;
@Controller
public class ApplicationEventObservorController implements ApplicationListener<MessageEvent> {
private static Logger logger = Logger.getLogger(ApplicationEventObservorController.class);
private SimpMessagingTemplate template;
@Autowired
public ApplicationEventObservorController(SimpMessagingTemplate template) {
logger.debug("Initialising ApplicationEventObservorController");
this.template = template;
}
@Override
public void onApplicationEvent(MessageEvent event) {
logger.debug("Captured event. " + event);
this.template.convertAndSend("/topic/update", event.toString());
}
}
共 (0) 个答案