10

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

@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包含自定义对象的对象,为简单起见,我将其称为“通知程序”。通知程序侦听服务器端发生的某些事件。然后它调用“通知”方法,该方法应该将有关事件的详细信息发送给所有客户端。但是,它不起作用。

@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 + "!"));
    }
}

我没有例外。SimpMessagingTemplate已经被Spring成功注入,所以它不为空。我已经能够进入 Spring 代码并发SimpleBrokerMessageHandler现在subscriptionRegistry使用SimpMessagingTemplate. 因此,它必须是与控制器正在使用的实例不同的实例。我怎样才能获得subscriptionRegistry控制器使用的相同内容?

4

2 回答 2

4

解决方案是使用 Spring 的ApplicationListener类而不是ServletContextListener,并专门监听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();
        }
    }
}
于 2015-03-04T18:37:23.780 回答
1

这个解决方案效果很好。

对于任何想要快速解决方案的人 - 去掉所有“通知程序”的东西,然后将 MessagingApplicationListener 注入你的班级。然后使用字符串调用“通知”以发送消息。显然,正如@battmanz 所说,您需要某种方式将事件推送到侦听器,但是这个类可以满足您的需要。

于 2020-01-07T11:59:51.307 回答