通过ServletContextListener中的SimpMessagingTemplate向所有客户端发送消息 [英] Send Message to all clients via SimpMessagingTemplate in ServletContextListener

查看:409
本文介绍了通过ServletContextListener中的SimpMessagingTemplate向所有客户端发送消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Spring框架,我有一个工作的websocket控制器,如下所示:

I'm using the Spring framework and I have a working websocket controller that looks like this:

@Controller
public class GreetingController {

    @MessageMapping("/hello")
    @SendTo("/topic/greetings")
    public Greeting greeting(HelloMessage message) throws InterruptedException {
        return new Greeting("Hello, " + message.getName() + "!");
    }
}

我也有这样的配置:

@Configuration
@EnableWebSocketMessageBroker
public class HelloWebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {

    @Override
    public void configureMessageBroker(MessageBrokerRegistry config) {
        config.enableSimpleBroker("/topic");
        config.setApplicationDestinationPrefixes("/app");
    }

    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/hello").withSockJS();
    }
}

这部分效果很好!我可以使用Stomp.js在两个或多个浏览器之间成功发送和接收消息。这是不起作用的部分。我已经实现了一个 ServletContextListener ,其中包含一个自定义对象,为简单起见,我称之为通知程序。通知程序侦听在服务器端发生的某些事件。然后它调用'notify'方法,该方法应该将有关事件的详细信息发送给所有客户端。但它不起作用。

That part works great! I can successfully send and receive messages between two or more browsers using Stomp.js. Here's the part that doesn't work. I have implemented a ServletContextListener that contains a custom object that, for simplicity sake, I have called 'notifier'. The notifier listens for certain events to happen on the server-side. It then calls the 'notify' method, which is supposed to send details about the event to all clients. It doesn't work, though.

@WebListener
public class MessageListener implements ServletContextListener, Notifiable {

    private Notifier notifier;

    @Autowired
    private SimpMessagingTemplate messageSender;


    public MessageListener() {
        notifier = new Notifier(this);
    }

    public void contextInitialized(ServletContextEvent contextEvent) {
        WebApplicationContextUtils
        .getRequiredWebApplicationContext(contextEvent.getServletContext())
        .getAutowireCapableBeanFactory()
        .autowireBean(this);

        notifier.start();
    }

    public void contextDestroyed(ServletContextEvent contextEvent) {
        notifier.stop();
    }

    public void notify(NotifyEvent event) {
        messageSender.convertAndSend("/topic/greetings", new Greeting("Hello, " + event.subject + "!"));
    }
}

我没有例外。 Spring已经成功注入了 SimpMessagingTemplate ,所以它不是null。我已经能够进入Spring代码,并且在使用时已经发现 SimpleBrokerMessageHandler subscriptionRegistry 为空 SimpMessagingTemplate 。因此它必须是与控制器正在使用的实例不同的实例。如何获得控制器使用的 subscriptionRegistry

I do not get an exception. The SimpMessagingTemplate has been successfully injected by Spring, so it's not null. I have been able to step into the Spring code and have figured out that the SimpleBrokerMessageHandler's subscriptionRegistry is empty when using the SimpMessagingTemplate. So it must be a separate instance from the one that the controllers are using. How can I get the same subscriptionRegistry that is used by the controllers?

推荐答案

解决方案是使用Spring的 ApplicationListener 类而不是 ServletContextListener ,并专门监听 ContextRefreshedEvent

The solution was to use Spring's ApplicationListener class instead of a ServletContextListener, and to specifically listen for the ContextRefreshedEvent.

这是我的工作示例:

@Component
public class MessagingApplicationListener implements ApplicationListener<ContextRefreshedEvent>, Notifiable {
    private final NotifierFactor notifierFactory;
    private final MessageSendingOperations<String> messagingTemplate;
    private Notifier notifier;

    @Autowired
    public MessagingApplicationListener(NotifierFactor notifierFactory, MessageSendingOperations<String> messagingTemplate) {
        this.notifierFactory = notifierFactory;
        this.messagingTemplate = messagingTemplate;
    }

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        if (notifier == null) {
            notifier = notifierFactory.create(this);
            notifier.start();
        }
    }

    public void notify(NotifyEvent event) {
        messagingTemplate.convertAndSend("/topic/greetings", new Greeting("Hello, " + event.subject + "!"));
    }

    @PreDestroy
    private void stopNotifier() {
        if (notifier != null) {
            notifier.stop();
        }
    }
}

这篇关于通过ServletContextListener中的SimpMessagingTemplate向所有客户端发送消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆