Java Code Examples for org.springframework.messaging.simp.stomp.StompHeaderAccessor#getDestination()

The following examples show how to use org.springframework.messaging.simp.stomp.StompHeaderAccessor#getDestination() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WebSocketTransport.java    From WeEvent with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
private void handleMessageFrame(StompHeaderAccessor stompHeaderAccessor, Message<byte[]> stompMsg) {
    String subscriptionId = stompHeaderAccessor.getFirstNativeHeader("subscription-id");

    // custom properties, eventId is in native header
    Map<String, String> extensions = new HashMap<>();
    for (Map.Entry<String, List<String>> entry : ((Map<String, List<String>>) stompMsg.getHeaders().get(NativeMessageHeaderAccessor.NATIVE_HEADERS)).entrySet()) {
        if (entry.getKey().startsWith("weevent-")) {
            extensions.put(entry.getKey(), entry.getValue().get(0));
        }
    }

    WeEvent event = new WeEvent(stompHeaderAccessor.getDestination(), stompMsg.getPayload(), extensions);
    event.setEventId(stompHeaderAccessor.getFirstNativeHeader("eventId"));
    log.info("received: {}", event);

    if (this.subscription2EventCache.containsKey(subscriptionId)) {
        IWeEventClient.EventListener listener = this.subscription2EventCache.get(subscriptionId).getSecond();
        listener.onEvent(event);
        // update the cache eventId
        this.subscription2EventCache.get(subscriptionId).getFirst().setOffset(event.getEventId());
    }
}
 
Example 2
Source File: BrokerStomp.java    From WeEvent with Apache License 2.0 6 votes vote down vote up
private void handleUnsubscribeMessage(StompHeaderAccessor stompHeaderAccessor, WebSocketSession session) {
    String simpDestination = stompHeaderAccessor.getDestination();
    String headerIdStr = stompHeaderAccessor.getFirstNativeHeader(StompHeaderAccessor.STOMP_ID_HEADER);

    try {
        boolean result = handleUnSubscribe(session, headerIdStr);

        // send response
        StompHeaderAccessor accessor;
        if (result) {
            accessor = StompHeaderAccessor.create(StompCommand.RECEIPT);
            accessor.setDestination(simpDestination);
        } else {
            accessor = StompHeaderAccessor.create(StompCommand.ERROR);
        }
        // a unique identifier for that message and a subscription header matching the identifier of the subscription that is receiving the message.
        accessor.setReceiptId(headerIdStr);
        sendSimpleMessage(session, accessor);
    } catch (BrokerException e) {
        handleErrorMessage(session, e, headerIdStr);
    }
}
 
Example 3
Source File: EventRouter.java    From haven-platform with Apache License 2.0 6 votes vote down vote up
private void sendHistoryToNewSubscriber(AbstractSubProtocolEvent ev) {
    Message<byte[]> msg = ev.getMessage();
    StompHeaderAccessor ha = StompHeaderAccessor.wrap(msg);
    String pattern = ha.getDestination();
    if(!pattern.startsWith(PREFIX)) {
        // we must send only to appropriate paths
        return;
    }
    MessageConverter messageConverter = this.simpMessagingTemplate.getMessageConverter();

    for(BusData data: buses.values()) {
        String dest = getDestination(data.getId());
        if(!this.pathMatcher.match(pattern, dest)) {
            continue;
        }
        for(Object obj: data.getEvents()) {
            StompHeaderAccessor mha = Stomp.createHeaders(ha.getSessionId(), ha.getSubscriptionId());
            mha.setDestination(dest);
            Message<?> message = messageConverter.toMessage(obj, mha.getMessageHeaders());
            clientChannel.send(message);
        }
    }
}
 
Example 4
Source File: BrokerStomp.java    From WeEvent with Apache License 2.0 5 votes vote down vote up
private void handleSendMessage(StompHeaderAccessor stompHeaderAccessor, Message<byte[]> msg, WebSocketSession session) {
    Map<String, String> extensions = this.getWeEventExtend(msg);
    // group id
    String groupId = stompHeaderAccessor.getFirstNativeHeader(WeEventConstants.EVENT_GROUP_ID);
    if (StringUtils.isBlank(groupId)) {
        groupId = "";
    }

    try {
        String destination = stompHeaderAccessor.getDestination();

        // publish event
        SendResult sendResult = this.iproducer.publish(new WeEvent(destination, msg.getPayload(), extensions), groupId, this.fiscoConfig.getWeb3sdkTimeout());

        // send response
        StompHeaderAccessor accessor;
        if (sendResult.getStatus().equals(SendResult.SendResultStatus.SUCCESS)) {
            accessor = StompHeaderAccessor.create(StompCommand.RECEIPT);
            accessor.setDestination(destination);
            accessor.setNativeHeader(WeEventConstants.EXTENSIONS_EVENT_ID, sendResult.getEventId());
        } else {
            accessor = StompHeaderAccessor.create(StompCommand.ERROR);
            accessor.setMessage(sendResult.toString());
        }
        accessor.setReceiptId(stompHeaderAccessor.getReceipt());

        sendSimpleMessage(session, accessor);
    } catch (BrokerException e) {
        handleErrorMessage(session, e, stompHeaderAccessor.getReceipt());
    }
}
 
Example 5
Source File: BrokerStomp.java    From WeEvent with Apache License 2.0 5 votes vote down vote up
private void handleDefaultMessage(StompHeaderAccessor stompHeaderAccessor, WebSocketSession session) {
    String simpDestination = stompHeaderAccessor.getDestination();
    String headerIdStr = stompHeaderAccessor.getFirstNativeHeader(StompHeaderAccessor.STOMP_ID_HEADER);

    // send response
    StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.ERROR);
    accessor.setDestination(simpDestination);
    accessor.setMessage("NOT SUPPORT COMMAND");
    accessor.setReceiptId(headerIdStr);
    // a unique identifier for that message and a subscription header matching the identifier of the subscription that is receiving the message.
    sendSimpleMessage(session, accessor);

    // follow protocol 1.2 to close connection
    this.closeSession(session);
}
 
Example 6
Source File: BrokerStomp.java    From WeEvent with Apache License 2.0 4 votes vote down vote up
private void handleSubscribeMessage(StompHeaderAccessor stompHeaderAccessor, WebSocketSession session) {
    // subscribe command id
    String headerIdStr = stompHeaderAccessor.getFirstNativeHeader(StompHeaderAccessor.STOMP_ID_HEADER);

    // group
    String groupId = stompHeaderAccessor.getFirstNativeHeader(WeEventConstants.EVENT_GROUP_ID);
    if (StringUtils.isBlank(groupId)) {
        groupId = "";
    }

    // check if there hasn't the event, need use the last id
    String subEventId = stompHeaderAccessor.getFirstNativeHeader(WeEventConstants.EXTENSIONS_EVENT_ID);
    if (StringUtils.isBlank(subEventId)) {
        subEventId = WeEvent.OFFSET_LAST;
    }

    // subscription id
    String continueSubscriptionIdStr = stompHeaderAccessor.getFirstNativeHeader(WeEvent.WeEvent_SubscriptionId);
    if (StringUtils.isBlank(continueSubscriptionIdStr)) {
        continueSubscriptionIdStr = "";
    }

    // tag
    String tag = stompHeaderAccessor.getFirstNativeHeader(WeEvent.WeEvent_TAG);
    if (StringUtils.isBlank(tag)) {
        tag = "";
    }

    // is ephemeral
    boolean ephemeral = stompHeaderAccessor.getFirstNativeHeader(WeEvent.WeEvent_EPHEMERAL) != null;

    try {
        String simpDestination = stompHeaderAccessor.getDestination();
        String subscriptionId = handleSubscribe(session,
                simpDestination,
                groupId,
                headerIdStr,
                subEventId,
                continueSubscriptionIdStr,
                tag,
                ephemeral);

        // send response
        StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.RECEIPT);
        accessor.setDestination(simpDestination);
        // a unique identifier for that message and a subscription header matching the identifier of the subscription that is receiving the message.
        accessor.setReceiptId(headerIdStr);
        accessor.setSubscriptionId(subscriptionId);
        accessor.setNativeHeader("subscription-id", subscriptionId);
        sendSimpleMessage(session, accessor);
    } catch (BrokerException e) {
        handleErrorMessage(session, e, headerIdStr);
    }
}