我在项目中创建了一个正在工作的通知系统。我实际的代码是:ActiveMQ Spring Stomp:我如何更改现有代码以创建持久订阅
我的客户(JavaScript的):
let connectWebSocket =() => {
socket = new SockJS(context.backend + '/myWebSocketEndPoint');
stompClient = Stomp.over(socket);
stompClient.connect({},function (frame) {
stompClient.subscribe('/topic/notification', function(response){
alert(response);
});
});
}
connectWebSocket();
服务器(Java与Spring)
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer{
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/topic");
}
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/myWebSocketEndPoint")
.setAllowedOrigins("*")
.withSockJS();
}
}
这是工作。现在我想在用户离线时发送给用户通知:当他们进行登录时,我会发送(自动)他们的通知。我必须用activeMQ来做到这一点。我见过一些例子,但不太了解它们..有人可以指示我如何准确地编辑我的代码并实现持久订阅?非常感谢
编辑:我已经更新了我的客户端代码:
let connectWebSocket =() => {
let clientId =user.profile.id;
socket = new SockJS(context.backend + '/myWebSocketEndPoint');
stompClient = Stomp.over(socket);
stompClient.connect({"client-id": clientId},{},function (frame) {
stompClient.subscribe('/topic/notification', function(response){
alert(response);
},{"activemq.subscriptionName": clientId});
});
}
但是当用户离线时,如果通知到达,当他返回在线,不发送通知him..I想我不得不改变我的服务器端
的pom.xml
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.14.2</version>
</dependency>
EDIT2:: 在pom.xml中有正确的依赖关系,我现在有一个错误。我有这样的配置:
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableStompBrokerRelay("/topic/");
}
但是当我运行我的代码,我看到这个错误:
2017/01/24 17:17:15.751 ERROR [org.springframework.boot.SpringApplication:839] Application startup failed
org.springframework.context.ApplicationContextException: Failed to start bean 'stompBrokerRelayMessageHandler'; nested exception is java.lang.NoClassDefFoundError: reactor/io/codec/Codec
EDIT3:这是我如何发送通知给客户:
@Component
public class MenuItemNotificationSender {
@Autowired
private SimpMessagingTemplate messagingTemplate;
@Autowired
public MenuItemNotificationSender(SimpMessagingTemplate messagingTemplate){
this.messagingTemplate = messagingTemplate;
}
public void sendNotification(MenuItemDto menuItem) {
messagingTemplate.convertAndSend("/topic/notification", menuItem);
}
}