@Override @Nullable public String getSubscriptionId() { return this.headers.getId(); }
@Override public void unsubscribe(@Nullable StompHeaders headers) { String id = this.headers.getId(); if (id != null) { DefaultStompSession.this.subscriptions.remove(id); DefaultStompSession.this.unsubscribe(id, headers); } }
public DefaultSubscription(StompHeaders headers, StompFrameHandler handler) { super(headers.getReceipt()); Assert.notNull(headers.getDestination(), "Destination must not be null"); Assert.notNull(handler, "StompFrameHandler must not be null"); this.headers = headers; this.handler = handler; DefaultStompSession.this.subscriptions.put(headers.getId(), this); }
@Override public Subscription subscribe(StompHeaders headers, StompFrameHandler handler) { Assert.hasText(headers.getDestination(), "Destination header is required"); Assert.notNull(handler, "StompFrameHandler must not be null"); String subscriptionId = headers.getId(); if (!StringUtils.hasText(subscriptionId)) { subscriptionId = String.valueOf(DefaultStompSession.this.subscriptionIndex.getAndIncrement()); headers.setId(subscriptionId); } checkOrAddReceipt(headers); Subscription subscription = new DefaultSubscription(headers, handler); StompHeaderAccessor accessor = createHeaderAccessor(StompCommand.SUBSCRIBE); accessor.addNativeHeaders(headers); Message<byte[]> message = createMessage(accessor, EMPTY_PAYLOAD); execute(message); return subscription; }
@Test public void unsubscribe() { this.session.afterConnected(this.connection); assertTrue(this.session.isConnected()); String destination = "/topic/foo"; StompFrameHandler frameHandler = mock(StompFrameHandler.class); Subscription subscription = this.session.subscribe(destination, frameHandler); subscription.unsubscribe(); Message<byte[]> message = this.messageCaptor.getValue(); StompHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message, StompHeaderAccessor.class); assertEquals(StompCommand.UNSUBSCRIBE, accessor.getCommand()); StompHeaders stompHeaders = StompHeaders.readOnlyStompHeaders(accessor.getNativeHeaders()); assertEquals(stompHeaders.toString(), 1, stompHeaders.size()); assertEquals(subscription.getSubscriptionId(), stompHeaders.getId()); }
@Test public void subscribe() { this.session.afterConnected(this.connection); assertTrue(this.session.isConnected()); String destination = "/topic/foo"; StompFrameHandler frameHandler = mock(StompFrameHandler.class); Subscription subscription = this.session.subscribe(destination, frameHandler); Message<byte[]> message = this.messageCaptor.getValue(); StompHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message, StompHeaderAccessor.class); assertEquals(StompCommand.SUBSCRIBE, accessor.getCommand()); StompHeaders stompHeaders = StompHeaders.readOnlyStompHeaders(accessor.getNativeHeaders()); assertEquals(stompHeaders.toString(), 2, stompHeaders.size()); assertEquals(destination, stompHeaders.getDestination()); assertEquals(subscription.getSubscriptionId(), stompHeaders.getId()); }
@Test public void ack() { this.session.afterConnected(this.connection); assertTrue(this.session.isConnected()); String messageId = "123"; this.session.acknowledge(messageId, true); Message<byte[]> message = this.messageCaptor.getValue(); StompHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message, StompHeaderAccessor.class); assertEquals(StompCommand.ACK, accessor.getCommand()); StompHeaders stompHeaders = StompHeaders.readOnlyStompHeaders(accessor.getNativeHeaders()); assertEquals(stompHeaders.toString(), 1, stompHeaders.size()); assertEquals(messageId, stompHeaders.getId()); }
@Test public void nack() { this.session.afterConnected(this.connection); assertTrue(this.session.isConnected()); String messageId = "123"; this.session.acknowledge(messageId, false); Message<byte[]> message = this.messageCaptor.getValue(); StompHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message, StompHeaderAccessor.class); assertEquals(StompCommand.NACK, accessor.getCommand()); StompHeaders stompHeaders = StompHeaders.readOnlyStompHeaders(accessor.getNativeHeaders()); assertEquals(stompHeaders.toString(), 1, stompHeaders.size()); assertEquals(messageId, stompHeaders.getId()); }
@Override @Nullable public String getSubscriptionId() { return this.headers.getId(); }
@Test public void subscribeWithHeaders() { this.session.afterConnected(this.connection); assertTrue(this.session.isConnected()); String subscriptionId = "123"; String destination = "/topic/foo"; StompHeaders stompHeaders = new StompHeaders(); stompHeaders.setId(subscriptionId); stompHeaders.setDestination(destination); StompFrameHandler frameHandler = mock(StompFrameHandler.class); Subscription subscription = this.session.subscribe(stompHeaders, frameHandler); assertEquals(subscriptionId, subscription.getSubscriptionId()); Message<byte[]> message = this.messageCaptor.getValue(); StompHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message, StompHeaderAccessor.class); assertEquals(StompCommand.SUBSCRIBE, accessor.getCommand()); stompHeaders = StompHeaders.readOnlyStompHeaders(accessor.getNativeHeaders()); assertEquals(stompHeaders.toString(), 2, stompHeaders.size()); assertEquals(destination, stompHeaders.getDestination()); assertEquals(subscriptionId, stompHeaders.getId()); }
@Test // SPR-15131 public void unsubscribeWithCustomHeader() { this.session.afterConnected(this.connection); assertTrue(this.session.isConnected()); String headerName = "durable-subscription-name"; String headerValue = "123"; StompHeaders subscribeHeaders = new StompHeaders(); subscribeHeaders.setDestination("/topic/foo"); subscribeHeaders.set(headerName, headerValue); StompFrameHandler frameHandler = mock(StompFrameHandler.class); Subscription subscription = this.session.subscribe(subscribeHeaders, frameHandler); StompHeaders unsubscribeHeaders = new StompHeaders(); unsubscribeHeaders.set(headerName, subscription.getSubscriptionHeaders().getFirst(headerName)); subscription.unsubscribe(unsubscribeHeaders); Message<byte[]> message = this.messageCaptor.getValue(); StompHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message, StompHeaderAccessor.class); assertEquals(StompCommand.UNSUBSCRIBE, accessor.getCommand()); StompHeaders stompHeaders = StompHeaders.readOnlyStompHeaders(accessor.getNativeHeaders()); assertEquals(stompHeaders.toString(), 2, stompHeaders.size()); assertEquals(subscription.getSubscriptionId(), stompHeaders.getId()); assertEquals(headerValue, stompHeaders.getFirst(headerName)); }
@Override public void unsubscribe(@Nullable StompHeaders headers) { String id = this.headers.getId(); if (id != null) { DefaultStompSession.this.subscriptions.remove(id); DefaultStompSession.this.unsubscribe(id, headers); } }
public DefaultSubscription(StompHeaders headers, StompFrameHandler handler) { super(headers.getReceipt()); Assert.notNull(headers.getDestination(), "Destination must not be null"); Assert.notNull(handler, "StompFrameHandler must not be null"); this.headers = headers; this.handler = handler; DefaultStompSession.this.subscriptions.put(headers.getId(), this); }
@Override public Subscription subscribe(StompHeaders headers, StompFrameHandler handler) { Assert.hasText(headers.getDestination(), "Destination header is required"); Assert.notNull(handler, "StompFrameHandler must not be null"); String subscriptionId = headers.getId(); if (!StringUtils.hasText(subscriptionId)) { subscriptionId = String.valueOf(DefaultStompSession.this.subscriptionIndex.getAndIncrement()); headers.setId(subscriptionId); } checkOrAddReceipt(headers); Subscription subscription = new DefaultSubscription(headers, handler); StompHeaderAccessor accessor = createHeaderAccessor(StompCommand.SUBSCRIBE); accessor.addNativeHeaders(headers); Message<byte[]> message = createMessage(accessor, EMPTY_PAYLOAD); execute(message); return subscription; }
@Override @Nullable public String getSubscriptionId() { return this.headers.getId(); }
@Override @Nullable public String getSubscriptionId() { return this.headers.getId(); }
@Override public void unsubscribe(@Nullable StompHeaders headers) { String id = this.headers.getId(); if (id != null) { DefaultStompSession.this.subscriptions.remove(id); DefaultStompSession.this.unsubscribe(id, headers); } }
@Override public void unsubscribe(@Nullable StompHeaders headers) { String id = this.headers.getId(); if (id != null) { DefaultStompSession.this.subscriptions.remove(id); DefaultStompSession.this.unsubscribe(id, headers); } }
public DefaultSubscription(StompHeaders headers, StompFrameHandler handler) { super(headers.getReceipt()); Assert.notNull(headers.getDestination(), "Destination must not be null"); Assert.notNull(handler, "StompFrameHandler must not be null"); this.headers = headers; this.handler = handler; DefaultStompSession.this.subscriptions.put(headers.getId(), this); }
public DefaultSubscription(StompHeaders headers, StompFrameHandler handler) { super(headers.getReceipt()); Assert.notNull(headers.getDestination(), "Destination must not be null"); Assert.notNull(handler, "StompFrameHandler must not be null"); this.headers = headers; this.handler = handler; DefaultStompSession.this.subscriptions.put(headers.getId(), this); }