Java Code Examples for org.springframework.messaging.simp.SimpMessageHeaderAccessor#setLeaveMutable()

The following examples show how to use org.springframework.messaging.simp.SimpMessageHeaderAccessor#setLeaveMutable() . 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: UserDestinationMessageHandler.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
public void handleUnresolved(Message<?> message) {
	MessageHeaders headers = message.getHeaders();
	if (SimpMessageHeaderAccessor.getFirstNativeHeader(ORIGINAL_DESTINATION, headers) != null) {
		// Re-broadcast
		return;
	}
	SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.wrap(message);
	String destination = accessor.getDestination();
	accessor.setNativeHeader(ORIGINAL_DESTINATION, destination);
	accessor.setLeaveMutable(true);
	message = MessageBuilder.createMessage(message.getPayload(), accessor.getMessageHeaders());
	if (logger.isTraceEnabled()) {
		logger.trace("Translated " + destination + " -> " + getBroadcastDestination());
	}
	this.messagingTemplate.send(getBroadcastDestination(), message);
}
 
Example 2
Source File: SimpleBrokerMessageHandler.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
public void run() {
	long now = System.currentTimeMillis();
	for (SessionInfo info : sessions.values()) {
		if (info.getReadInterval() > 0 && (now - info.getLastReadTime()) > info.getReadInterval()) {
			handleDisconnect(info.getSessionId(), info.getUser(), null);
		}
		if (info.getWriteInterval() > 0 && (now - info.getLastWriteTime()) > info.getWriteInterval()) {
			SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create(SimpMessageType.HEARTBEAT);
			accessor.setSessionId(info.getSessionId());
			Principal user = info.getUser();
			if (user != null) {
				accessor.setUser(user);
			}
			initHeaders(accessor);
			accessor.setLeaveMutable(true);
			MessageHeaders headers = accessor.getMessageHeaders();
			info.getClientOutboundChannel().send(MessageBuilder.createMessage(EMPTY_PAYLOAD, headers));
		}
	}
}
 
Example 3
Source File: SimpleBrokerMessageHandler.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Override
public void run() {
	long now = System.currentTimeMillis();
	for (SessionInfo info : sessions.values()) {
		if (info.getReadInterval() > 0 && (now - info.getLastReadTime()) > info.getReadInterval()) {
			handleDisconnect(info.getSessionId(), info.getUser(), null);
		}
		if (info.getWriteInterval() > 0 && (now - info.getLastWriteTime()) > info.getWriteInterval()) {
			SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create(SimpMessageType.HEARTBEAT);
			accessor.setSessionId(info.getSessionId());
			Principal user = info.getUser();
			if (user != null) {
				accessor.setUser(user);
			}
			initHeaders(accessor);
			accessor.setLeaveMutable(true);
			MessageHeaders headers = accessor.getMessageHeaders();
			info.getClientOutboundChannel().send(MessageBuilder.createMessage(EMPTY_PAYLOAD, headers));
		}
	}
}
 
Example 4
Source File: UserDestinationMessageHandler.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public void handleMessage(Message<?> message) throws MessagingException {
	Message<?> messageToUse = message;
	if (this.broadcastHandler != null) {
		messageToUse = this.broadcastHandler.preHandle(message);
		if (messageToUse == null) {
			return;
		}
	}

	UserDestinationResult result = this.destinationResolver.resolveDestination(messageToUse);
	if (result == null) {
		return;
	}

	if (result.getTargetDestinations().isEmpty()) {
		if (logger.isTraceEnabled()) {
			logger.trace("No active sessions for user destination: " + result.getSourceDestination());
		}
		if (this.broadcastHandler != null) {
			this.broadcastHandler.handleUnresolved(messageToUse);
		}
		return;
	}

	SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.wrap(messageToUse);
	initHeaders(accessor);
	accessor.setNativeHeader(SimpMessageHeaderAccessor.ORIGINAL_DESTINATION, result.getSubscribeDestination());
	accessor.setLeaveMutable(true);

	messageToUse = MessageBuilder.createMessage(messageToUse.getPayload(), accessor.getMessageHeaders());
	if (logger.isTraceEnabled()) {
		logger.trace("Translated " + result.getSourceDestination() + " -> " + result.getTargetDestinations());
	}
	for (String target : result.getTargetDestinations()) {
		this.messagingTemplate.send(target, messageToUse);
	}
}
 
Example 5
Source File: UserDestinationMessageHandler.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public void handleMessage(Message<?> message) throws MessagingException {
	if (this.broadcastHandler != null) {
		message = this.broadcastHandler.preHandle(message);
		if (message == null) {
			return;
		}
	}
	UserDestinationResult result = this.destinationResolver.resolveDestination(message);
	if (result == null) {
		return;
	}
	if (result.getTargetDestinations().isEmpty()) {
		if (logger.isTraceEnabled()) {
			logger.trace("No active sessions for user destination: " + result.getSourceDestination());
		}
		if (this.broadcastHandler != null) {
			this.broadcastHandler.handleUnresolved(message);
		}
		return;
	}
	SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.wrap(message);
	initHeaders(accessor);
	accessor.setNativeHeader(ORIGINAL_DESTINATION, result.getSubscribeDestination());
	accessor.setLeaveMutable(true);
	message = MessageBuilder.createMessage(message.getPayload(), accessor.getMessageHeaders());
	if (logger.isTraceEnabled()) {
		logger.trace("Translated " + result.getSourceDestination() + " -> " + result.getTargetDestinations());
	}
	for (String target : result.getTargetDestinations()) {
		this.messagingTemplate.send(target, message);
	}
}
 
Example 6
Source File: SendToMethodReturnValueHandler.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private MessageHeaders createHeaders(String sessionId, MethodParameter returnType) {
	SimpMessageHeaderAccessor headerAccessor = SimpMessageHeaderAccessor.create(SimpMessageType.MESSAGE);
	if (getHeaderInitializer() != null) {
		getHeaderInitializer().initHeaders(headerAccessor);
	}
	if (sessionId != null) {
		headerAccessor.setSessionId(sessionId);
	}
	headerAccessor.setHeader(SimpMessagingTemplate.CONVERSION_HINT_HEADER, returnType);
	headerAccessor.setLeaveMutable(true);
	return headerAccessor.getMessageHeaders();
}
 
Example 7
Source File: SubscriptionMethodReturnValueHandler.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private MessageHeaders createHeaders(String sessionId, String subscriptionId, MethodParameter returnType) {
	SimpMessageHeaderAccessor headerAccessor = SimpMessageHeaderAccessor.create(SimpMessageType.MESSAGE);
	if (getHeaderInitializer() != null) {
		getHeaderInitializer().initHeaders(headerAccessor);
	}
	headerAccessor.setSessionId(sessionId);
	headerAccessor.setSubscriptionId(subscriptionId);
	headerAccessor.setHeader(SimpMessagingTemplate.CONVERSION_HINT_HEADER, returnType);
	headerAccessor.setLeaveMutable(true);
	return headerAccessor.getMessageHeaders();
}
 
Example 8
Source File: MessageConverterTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void toMessageWithMutableMessageHeaders() {
	SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create(SimpMessageType.MESSAGE);
	accessor.setHeader("foo", "bar");
	accessor.setNativeHeader("fooNative", "barNative");
	accessor.setLeaveMutable(true);

	MessageHeaders headers = accessor.getMessageHeaders();
	Message<?> message = this.converter.toMessage("ABC", headers);

	assertSame(headers, message.getHeaders());
	assertNull(message.getHeaders().getId());
	assertNull(message.getHeaders().getTimestamp());
	assertEquals(MimeTypeUtils.TEXT_PLAIN, message.getHeaders().get(MessageHeaders.CONTENT_TYPE));
}
 
Example 9
Source File: UserRegistryMessageHandler.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public void run() {
	try {
		SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create(SimpMessageType.MESSAGE);
		accessor.setHeader(SimpMessageHeaderAccessor.IGNORE_ERROR, true);
		accessor.setLeaveMutable(true);
		Object payload = userRegistry.getLocalRegistryDto();
		brokerTemplate.convertAndSend(getBroadcastDestination(), payload, accessor.getMessageHeaders());
	}
	finally {
		userRegistry.purgeExpiredRegistries();
	}
}
 
Example 10
Source File: MessageConverterTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void toMessageWithMutableMessageHeaders() {
	SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create(SimpMessageType.MESSAGE);
	accessor.setHeader("foo", "bar");
	accessor.setNativeHeader("fooNative", "barNative");
	accessor.setLeaveMutable(true);

	MessageHeaders headers = accessor.getMessageHeaders();
	Message<?> message = this.converter.toMessage("ABC", headers);

	assertSame(headers, message.getHeaders());
	assertNull(message.getHeaders().getId());
	assertNull(message.getHeaders().getTimestamp());
	assertEquals(MimeTypeUtils.TEXT_PLAIN, message.getHeaders().get(MessageHeaders.CONTENT_TYPE));
}
 
Example 11
Source File: SubscriptionMethodReturnValueHandler.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private MessageHeaders createHeaders(@Nullable String sessionId, String subscriptionId, MethodParameter returnType) {
	SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create(SimpMessageType.MESSAGE);
	if (getHeaderInitializer() != null) {
		getHeaderInitializer().initHeaders(accessor);
	}
	if (sessionId != null) {
		accessor.setSessionId(sessionId);
	}
	accessor.setSubscriptionId(subscriptionId);
	accessor.setHeader(SimpMessagingTemplate.CONVERSION_HINT_HEADER, returnType);
	accessor.setLeaveMutable(true);
	return accessor.getMessageHeaders();
}
 
Example 12
Source File: SendToMethodReturnValueHandler.java    From java-technology-stack with MIT License 5 votes vote down vote up
private MessageHeaders createHeaders(@Nullable String sessionId, MethodParameter returnType) {
	SimpMessageHeaderAccessor headerAccessor = SimpMessageHeaderAccessor.create(SimpMessageType.MESSAGE);
	if (getHeaderInitializer() != null) {
		getHeaderInitializer().initHeaders(headerAccessor);
	}
	if (sessionId != null) {
		headerAccessor.setSessionId(sessionId);
	}
	headerAccessor.setHeader(SimpMessagingTemplate.CONVERSION_HINT_HEADER, returnType);
	headerAccessor.setLeaveMutable(true);
	return headerAccessor.getMessageHeaders();
}
 
Example 13
Source File: SubscriptionMethodReturnValueHandler.java    From java-technology-stack with MIT License 5 votes vote down vote up
private MessageHeaders createHeaders(@Nullable String sessionId, String subscriptionId, MethodParameter returnType) {
	SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create(SimpMessageType.MESSAGE);
	if (getHeaderInitializer() != null) {
		getHeaderInitializer().initHeaders(accessor);
	}
	if (sessionId != null) {
		accessor.setSessionId(sessionId);
	}
	accessor.setSubscriptionId(subscriptionId);
	accessor.setHeader(SimpMessagingTemplate.CONVERSION_HINT_HEADER, returnType);
	accessor.setLeaveMutable(true);
	return accessor.getMessageHeaders();
}
 
Example 14
Source File: MessageConverterTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void toMessageWithMutableMessageHeaders() {
	SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create(SimpMessageType.MESSAGE);
	accessor.setHeader("foo", "bar");
	accessor.setNativeHeader("fooNative", "barNative");
	accessor.setLeaveMutable(true);

	MessageHeaders headers = accessor.getMessageHeaders();
	Message<?> message = this.converter.toMessage("ABC", headers);

	assertSame(headers, message.getHeaders());
	assertNull(message.getHeaders().getId());
	assertNull(message.getHeaders().getTimestamp());
	assertEquals(MimeTypeUtils.TEXT_PLAIN, message.getHeaders().get(MessageHeaders.CONTENT_TYPE));
}
 
Example 15
Source File: UserRegistryMessageHandler.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public void run() {
	try {
		SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create(SimpMessageType.MESSAGE);
		accessor.setHeader(SimpMessageHeaderAccessor.IGNORE_ERROR, true);
		accessor.setLeaveMutable(true);
		Object payload = userRegistry.getLocalRegistryDto();
		brokerTemplate.convertAndSend(getBroadcastDestination(), payload, accessor.getMessageHeaders());
	}
	finally {
		userRegistry.purgeExpiredRegistries();
	}
}
 
Example 16
Source File: UserRegistryMessageHandler.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public void run() {
	try {
		SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create(SimpMessageType.MESSAGE);
		accessor.setHeader(SimpMessageHeaderAccessor.IGNORE_ERROR, true);
		accessor.setLeaveMutable(true);
		Object payload = userRegistry.getLocalRegistryDto();
		brokerTemplate.convertAndSend(getBroadcastDestination(), payload, accessor.getMessageHeaders());
	}
	finally {
		userRegistry.purgeExpiredRegistries();
	}
}
 
Example 17
Source File: UserDestinationMessageHandler.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public void handleMessage(Message<?> message) throws MessagingException {
	Message<?> messageToUse = message;
	if (this.broadcastHandler != null) {
		messageToUse = this.broadcastHandler.preHandle(message);
		if (messageToUse == null) {
			return;
		}
	}

	UserDestinationResult result = this.destinationResolver.resolveDestination(messageToUse);
	if (result == null) {
		return;
	}

	if (result.getTargetDestinations().isEmpty()) {
		if (logger.isTraceEnabled()) {
			logger.trace("No active sessions for user destination: " + result.getSourceDestination());
		}
		if (this.broadcastHandler != null) {
			this.broadcastHandler.handleUnresolved(messageToUse);
		}
		return;
	}

	SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.wrap(messageToUse);
	initHeaders(accessor);
	accessor.setNativeHeader(SimpMessageHeaderAccessor.ORIGINAL_DESTINATION, result.getSubscribeDestination());
	accessor.setLeaveMutable(true);

	messageToUse = MessageBuilder.createMessage(messageToUse.getPayload(), accessor.getMessageHeaders());
	if (logger.isTraceEnabled()) {
		logger.trace("Translated " + result.getSourceDestination() + " -> " + result.getTargetDestinations());
	}
	for (String target : result.getTargetDestinations()) {
		this.messagingTemplate.send(target, messageToUse);
	}
}
 
Example 18
Source File: SendToMethodReturnValueHandler.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private MessageHeaders createHeaders(@Nullable String sessionId, MethodParameter returnType) {
	SimpMessageHeaderAccessor headerAccessor = SimpMessageHeaderAccessor.create(SimpMessageType.MESSAGE);
	if (getHeaderInitializer() != null) {
		getHeaderInitializer().initHeaders(headerAccessor);
	}
	if (sessionId != null) {
		headerAccessor.setSessionId(sessionId);
	}
	headerAccessor.setHeader(SimpMessagingTemplate.CONVERSION_HINT_HEADER, returnType);
	headerAccessor.setLeaveMutable(true);
	return headerAccessor.getMessageHeaders();
}
 
Example 19
Source File: OrderedMessageSenderTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Test
public void test() throws InterruptedException {

	int start = 1;
	int end = 1000;

	AtomicInteger index = new AtomicInteger(start);
	AtomicReference<Object> result = new AtomicReference<>();
	CountDownLatch latch = new CountDownLatch(1);

	this.channel.subscribe(message -> {
		int expected = index.getAndIncrement();
		Integer actual = (Integer) message.getHeaders().getOrDefault("seq", -1);
		if (actual != expected) {
			result.set("Expected: " + expected + ", but was: " + actual);
			latch.countDown();
			return;
		}
		if (actual == 100 || actual == 200) {
			try {
				Thread.sleep(200);
			}
			catch (InterruptedException ex) {
				result.set(ex.toString());
				latch.countDown();
			}
		}
		if (actual == end) {
			result.set("Done");
			latch.countDown();
		}
	});

	for (int i = start; i <= end; i++) {
		SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create(SimpMessageType.MESSAGE);
		accessor.setHeader("seq", i);
		accessor.setLeaveMutable(true);
		this.sender.send(MessageBuilder.createMessage("payload", accessor.getMessageHeaders()));
	}

	latch.await(10, TimeUnit.SECONDS);
	assertEquals("Done", result.get());
}
 
Example 20
Source File: OrderedMessageSenderTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Test
public void test() throws InterruptedException {

	int start = 1;
	int end = 1000;

	AtomicInteger index = new AtomicInteger(start);
	AtomicReference<Object> result = new AtomicReference<>();
	CountDownLatch latch = new CountDownLatch(1);

	this.channel.subscribe(message -> {
		int expected = index.getAndIncrement();
		Integer actual = (Integer) message.getHeaders().getOrDefault("seq", -1);
		if (actual != expected) {
			result.set("Expected: " + expected + ", but was: " + actual);
			latch.countDown();
			return;
		}
		if (actual == 100 || actual == 200) {
			try {
				Thread.sleep(200);
			}
			catch (InterruptedException ex) {
				result.set(ex.toString());
				latch.countDown();
			}
		}
		if (actual == end) {
			result.set("Done");
			latch.countDown();
		}
	});

	for (int i = start; i <= end; i++) {
		SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create(SimpMessageType.MESSAGE);
		accessor.setHeader("seq", i);
		accessor.setLeaveMutable(true);
		this.sender.send(MessageBuilder.createMessage("payload", accessor.getMessageHeaders()));
	}

	latch.await(10, TimeUnit.SECONDS);
	assertEquals("Done", result.get());
}