Java Code Examples for org.springframework.messaging.support.MessageBuilder#createMessage()
The following examples show how to use
org.springframework.messaging.support.MessageBuilder#createMessage() .
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: MessageBrokerConfigurationTests.java From java-technology-stack with MIT License | 6 votes |
@Test public void brokerChannelUsedByAnnotatedMethod() { ApplicationContext context = loadConfig(SimpleBrokerConfig.class); TestChannel channel = context.getBean("brokerChannel", TestChannel.class); SimpAnnotationMethodMessageHandler messageHandler = context.getBean(SimpAnnotationMethodMessageHandler.class); StompHeaderAccessor headers = StompHeaderAccessor.create(StompCommand.SEND); headers.setSessionId("sess1"); headers.setSessionAttributes(new ConcurrentHashMap<>()); headers.setDestination("/foo"); Message<?> message = MessageBuilder.createMessage(new byte[0], headers.getMessageHeaders()); messageHandler.handleMessage(message); message = channel.messages.get(0); headers = StompHeaderAccessor.wrap(message); assertEquals(SimpMessageType.MESSAGE, headers.getMessageType()); assertEquals("/bar", headers.getDestination()); assertEquals("bar", new String((byte[]) message.getPayload())); }
Example 2
Source File: SendToMethodReturnValueHandlerTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
@Test // SPR-12170 public void sendToWithDestinationPlaceholders() throws Exception { given(this.messageChannel.send(any(Message.class))).willReturn(true); Map<String, String> vars = new LinkedHashMap<>(1); vars.put("roomName", "roomA"); String sessionId = "sess1"; SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create(); accessor.setSessionId(sessionId); accessor.setSubscriptionId("sub1"); accessor.setHeader(DESTINATION_TEMPLATE_VARIABLES_HEADER, vars); Message<?> message = MessageBuilder.createMessage(PAYLOAD, accessor.getMessageHeaders()); this.handler.handleReturnValue(PAYLOAD, this.sendToWithPlaceholdersReturnType, message); verify(this.messageChannel, times(1)).send(this.messageCaptor.capture()); SimpMessageHeaderAccessor actual = getCapturedAccessor(0); assertEquals(sessionId, actual.getSessionId()); assertEquals("/topic/chat.message.filtered.roomA", actual.getDestination()); }
Example 3
Source File: MessageBrokerConfigurationTests.java From spring-analysis-note with MIT License | 6 votes |
@Test public void brokerChannelUsedByAnnotatedMethod() { ApplicationContext context = loadConfig(SimpleBrokerConfig.class); TestChannel channel = context.getBean("brokerChannel", TestChannel.class); SimpAnnotationMethodMessageHandler messageHandler = context.getBean(SimpAnnotationMethodMessageHandler.class); StompHeaderAccessor headers = StompHeaderAccessor.create(StompCommand.SEND); headers.setSessionId("sess1"); headers.setSessionAttributes(new ConcurrentHashMap<>()); headers.setDestination("/foo"); Message<?> message = MessageBuilder.createMessage(new byte[0], headers.getMessageHeaders()); messageHandler.handleMessage(message); message = channel.messages.get(0); headers = StompHeaderAccessor.wrap(message); assertEquals(SimpMessageType.MESSAGE, headers.getMessageType()); assertEquals("/bar", headers.getDestination()); assertEquals("bar", new String((byte[]) message.getPayload())); }
Example 4
Source File: StompSubProtocolHandlerTests.java From java-technology-stack with MIT License | 6 votes |
@Test public void handleMessageToClientWithSimpConnectAck() { StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.CONNECT); accessor.setHeartbeat(10000, 10000); accessor.setAcceptVersion("1.0,1.1,1.2"); Message<?> connectMessage = MessageBuilder.createMessage(EMPTY_PAYLOAD, accessor.getMessageHeaders()); SimpMessageHeaderAccessor ackAccessor = SimpMessageHeaderAccessor.create(SimpMessageType.CONNECT_ACK); ackAccessor.setHeader(SimpMessageHeaderAccessor.CONNECT_MESSAGE_HEADER, connectMessage); ackAccessor.setHeader(SimpMessageHeaderAccessor.HEART_BEAT_HEADER, new long[] {15000, 15000}); Message<byte[]> ackMessage = MessageBuilder.createMessage(EMPTY_PAYLOAD, ackAccessor.getMessageHeaders()); this.protocolHandler.handleMessageToClient(this.session, ackMessage); assertEquals(1, this.session.getSentMessages().size()); TextMessage actual = (TextMessage) this.session.getSentMessages().get(0); assertEquals("CONNECTED\n" + "version:1.2\n" + "heart-beat:15000,15000\n" + "user-name:joe\n" + "\n" + "\u0000", actual.getPayload()); }
Example 5
Source File: MessageMethodArgumentResolver.java From spring-analysis-note with MIT License | 6 votes |
@Override public Object resolveArgument(MethodParameter parameter, Message<?> message) throws Exception { Class<?> targetMessageType = parameter.getParameterType(); Class<?> targetPayloadType = getPayloadType(parameter, message); if (!targetMessageType.isAssignableFrom(message.getClass())) { throw new MethodArgumentTypeMismatchException(message, parameter, "Actual message type '" + ClassUtils.getDescriptiveType(message) + "' does not match expected type '" + ClassUtils.getQualifiedName(targetMessageType) + "'"); } Object payload = message.getPayload(); if (targetPayloadType.isInstance(payload)) { return message; } if (isEmptyPayload(payload)) { throw new MessageConversionException(message, "Cannot convert from actual payload type '" + ClassUtils.getDescriptiveType(payload) + "' to expected payload type '" + ClassUtils.getQualifiedName(targetPayloadType) + "' when payload is empty"); } payload = convertPayload(message, parameter, targetPayloadType); return MessageBuilder.createMessage(payload, message.getHeaders()); }
Example 6
Source File: StompSubProtocolHandlerTests.java From java-technology-stack with MIT License | 6 votes |
@Test public void handleMessageToClientWithSimpConnectAckDefaultHeartBeat() { StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.CONNECT); accessor.setHeartbeat(10000, 10000); accessor.setAcceptVersion("1.0"); Message<?> connectMessage = MessageBuilder.createMessage(EMPTY_PAYLOAD, accessor.getMessageHeaders()); SimpMessageHeaderAccessor ackAccessor = SimpMessageHeaderAccessor.create(SimpMessageType.CONNECT_ACK); ackAccessor.setHeader(SimpMessageHeaderAccessor.CONNECT_MESSAGE_HEADER, connectMessage); Message<byte[]> ackMessage = MessageBuilder.createMessage(EMPTY_PAYLOAD, ackAccessor.getMessageHeaders()); this.protocolHandler.handleMessageToClient(this.session, ackMessage); assertEquals(1, this.session.getSentMessages().size()); TextMessage actual = (TextMessage) this.session.getSentMessages().get(0); assertEquals("CONNECTED\n" + "version:1.0\n" + "heart-beat:0,0\n" + "user-name:joe\n" + "\n" + "\u0000", actual.getPayload()); }
Example 7
Source File: StompSubProtocolHandlerTests.java From java-technology-stack with MIT License | 6 votes |
@Test public void handleMessageToClientWithSimpDisconnectAck() { StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.DISCONNECT); Message<?> connectMessage = MessageBuilder.createMessage(EMPTY_PAYLOAD, accessor.getMessageHeaders()); SimpMessageHeaderAccessor ackAccessor = SimpMessageHeaderAccessor.create(SimpMessageType.DISCONNECT_ACK); ackAccessor.setHeader(SimpMessageHeaderAccessor.DISCONNECT_MESSAGE_HEADER, connectMessage); Message<byte[]> ackMessage = MessageBuilder.createMessage(EMPTY_PAYLOAD, ackAccessor.getMessageHeaders()); this.protocolHandler.handleMessageToClient(this.session, ackMessage); assertEquals(1, this.session.getSentMessages().size()); TextMessage actual = (TextMessage) this.session.getSentMessages().get(0); assertEquals("ERROR\n" + "message:Session closed.\n" + "content-length:0\n" + "\n\u0000", actual.getPayload()); }
Example 8
Source File: DefaultStompSession.java From java-technology-stack with MIT License | 6 votes |
@SuppressWarnings("unchecked") private Message<byte[]> createMessage(StompHeaderAccessor accessor, @Nullable Object payload) { accessor.updateSimpMessageHeadersFromStompHeaders(); Message<byte[]> message; if (payload == null) { message = MessageBuilder.createMessage(EMPTY_PAYLOAD, accessor.getMessageHeaders()); } else if (payload instanceof byte[]) { message = MessageBuilder.createMessage((byte[]) payload, accessor.getMessageHeaders()); } else { message = (Message<byte[]>) getMessageConverter().toMessage(payload, accessor.getMessageHeaders()); accessor.updateStompHeadersFromSimpMessageHeaders(); if (message == null) { throw new MessageConversionException("Unable to convert payload with type='" + payload.getClass().getName() + "', contentType='" + accessor.getContentType() + "', converter=[" + getMessageConverter() + "]"); } } return message; }
Example 9
Source File: OrderProducer.java From SpringBoot-Course with MIT License | 5 votes |
public void send(Object message, Map<String, Object> properties) throws Exception { MessageHeaders messageHeaders = new MessageHeaders(properties); Message msg = MessageBuilder.createMessage(message, messageHeaders); String exchange = "order_direct_exchange"; String routingKey = "order_routingKey"; rabbitTemplate.setConfirmCallback(confirmCallback); rabbitTemplate.setReturnCallback(returnCallback); rabbitTemplate.convertAndSend(exchange, routingKey, msg); System.out.println(msg); System.err.println("Producer 消息发送成功!"); }
Example 10
Source File: StompSubProtocolHandlerTests.java From spring-analysis-note with MIT License | 5 votes |
@Test public void handleMessageToClientWithDestinationUserNameProvider() { this.session.setPrincipal(new UniqueUser("joe")); StompHeaderAccessor headers = StompHeaderAccessor.create(StompCommand.CONNECTED); Message<byte[]> message = MessageBuilder.createMessage(EMPTY_PAYLOAD, headers.getMessageHeaders()); this.protocolHandler.handleMessageToClient(this.session, message); assertEquals(1, this.session.getSentMessages().size()); WebSocketMessage<?> textMessage = this.session.getSentMessages().get(0); assertEquals("CONNECTED\n" + "user-name:joe\n" + "\n" + "\u0000", textMessage.getPayload()); }
Example 11
Source File: SimpleBrokerMessageHandlerTests.java From spring-analysis-note with MIT License | 5 votes |
private Message<String> createSubscriptionMessage(String sessionId, String subscriptionId, String destination) { SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.create(SimpMessageType.SUBSCRIBE); headers.setSubscriptionId(subscriptionId); headers.setDestination(destination); headers.setSessionId(sessionId); return MessageBuilder.createMessage("", headers.getMessageHeaders()); }
Example 12
Source File: AbstractMessageConverter.java From java-technology-stack with MIT License | 5 votes |
@Override @Nullable public final Message<?> toMessage(Object payload, @Nullable MessageHeaders headers, @Nullable Object conversionHint) { if (!canConvertTo(payload, headers)) { return null; } Object payloadToUse = convertToInternal(payload, headers, conversionHint); if (payloadToUse == null) { return null; } MimeType mimeType = getDefaultContentType(payloadToUse); if (headers != null) { MessageHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(headers, MessageHeaderAccessor.class); if (accessor != null && accessor.isMutable()) { if (mimeType != null) { accessor.setHeaderIfAbsent(MessageHeaders.CONTENT_TYPE, mimeType); } return MessageBuilder.createMessage(payloadToUse, accessor.getMessageHeaders()); } } MessageBuilder<?> builder = MessageBuilder.withPayload(payloadToUse); if (headers != null) { builder.copyHeaders(headers); } if (mimeType != null) { builder.setHeaderIfAbsent(MessageHeaders.CONTENT_TYPE, mimeType); } return builder.build(); }
Example 13
Source File: DefaultStompSessionTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Test public void heartbeatTasks() throws Exception { this.session.afterConnected(this.connection); verify(this.connection).send(any()); this.connectHeaders.setHeartbeat(new long[] {10000, 10000}); StompHeaderAccessor connected = StompHeaderAccessor.create(StompCommand.CONNECTED); connected.setVersion("1.2"); connected.setHeartbeat(10000, 10000); connected.setLeaveMutable(true); this.session.handleMessage(MessageBuilder.createMessage(new byte[0], connected.getMessageHeaders())); ArgumentCaptor<Runnable> writeTaskCaptor = ArgumentCaptor.forClass(Runnable.class); ArgumentCaptor<Runnable> readTaskCaptor = ArgumentCaptor.forClass(Runnable.class); verify(this.connection).onWriteInactivity(writeTaskCaptor.capture(), any(Long.class)); verify(this.connection).onReadInactivity(readTaskCaptor.capture(), any(Long.class)); Runnable writeTask = writeTaskCaptor.getValue(); Runnable readTask = readTaskCaptor.getValue(); assertNotNull(writeTask); assertNotNull(readTask); writeTask.run(); StompHeaderAccessor accessor = StompHeaderAccessor.createForHeartbeat(); Message<byte[]> message = MessageBuilder.createMessage(new byte[] {'\n'}, accessor.getMessageHeaders()); verify(this.connection).send(eq(message)); verifyNoMoreInteractions(this.connection); reset(this.sessionHandler); readTask.run(); verify(this.sessionHandler).handleTransportError(same(this.session), any(IllegalStateException.class)); verifyNoMoreInteractions(this.sessionHandler); }
Example 14
Source File: BrokerStomp.java From WeEvent with Apache License 2.0 | 5 votes |
private void sendSimpleMessage(WebSocketSession session, StompHeaderAccessor accessor) { MessageHeaders headers = accessor.getMessageHeaders(); Message<byte[]> message1 = MessageBuilder.createMessage("".getBytes(StandardCharsets.UTF_8), headers); byte[] bytes = new StompEncoder().encode(message1); TextMessage textMessage = new TextMessage(bytes); send2Remote(session, textMessage); }
Example 15
Source File: BrokerStomp.java From WeEvent with Apache License 2.0 | 5 votes |
private void handleOnEvent(String headerIdStr, String subscriptionId, WeEvent event, WebSocketSession session) { StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.MESSAGE); // package the return frame accessor.setSubscriptionId(headerIdStr); accessor.setNativeHeader("subscription-id", subscriptionId); accessor.setMessageId(headerIdStr); accessor.setDestination(event.getTopic()); accessor.setContentType(new MimeType("text", "plain", StandardCharsets.UTF_8)); // set custom properties in header for (Map.Entry<String, String> custom : event.getExtensions().entrySet()) { accessor.setNativeHeader(custom.getKey(), custom.getValue()); } // set eventId in header accessor.setNativeHeader(WeEventConstants.EXTENSIONS_EVENT_ID, event.getEventId()); // payload == content MessageHeaders headers = accessor.getMessageHeaders(); Message<byte[]> message = MessageBuilder.createMessage(event.getContent(), headers); byte[] bytes = new StompEncoder().encode(message); // send to remote send2Remote(session, new TextMessage(bytes)); }
Example 16
Source File: SimpleBrokerMessageHandler.java From java-technology-stack with MIT License | 5 votes |
private void handleDisconnect(String sessionId, @Nullable Principal user, @Nullable Message<?> origMessage) { this.sessions.remove(sessionId); this.subscriptionRegistry.unregisterAllSubscriptions(sessionId); SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create(SimpMessageType.DISCONNECT_ACK); accessor.setSessionId(sessionId); if (user != null) { accessor.setUser(user); } if (origMessage != null) { accessor.setHeader(SimpMessageHeaderAccessor.DISCONNECT_MESSAGE_HEADER, origMessage); } initHeaders(accessor); Message<byte[]> message = MessageBuilder.createMessage(EMPTY_PAYLOAD, accessor.getMessageHeaders()); getClientOutboundChannel().send(message); }
Example 17
Source File: StompBrokerRelayMessageHandlerIntegrationTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
public static MessageExchangeBuilder connect(String sessionId) { StompHeaderAccessor headers = StompHeaderAccessor.create(StompCommand.CONNECT); headers.setSessionId(sessionId); headers.setAcceptVersion("1.1,1.2"); headers.setHeartbeat(0, 0); Message<?> message = MessageBuilder.createMessage(new byte[0], headers.getMessageHeaders()); MessageExchangeBuilder builder = new MessageExchangeBuilder(message); builder.expected.add(new StompConnectedFrameMessageMatcher(sessionId)); return builder; }
Example 18
Source File: SenderService.java From spring-cloud-alibaba with Apache License 2.0 | 4 votes |
public <T> void sendWithTags(T msg, String tag) throws Exception { Message message = MessageBuilder.createMessage(msg, new MessageHeaders(Stream.of(tag).collect(Collectors .toMap(str -> MessageConst.PROPERTY_TAGS, String::toString)))); source.output1().send(message); }
Example 19
Source File: DefaultSubscriptionRegistryTests.java From spring4-understanding with Apache License 2.0 | 4 votes |
private Message<?> createMessage(String destination) { SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create(); accessor.setDestination(destination); return MessageBuilder.createMessage("", accessor.getMessageHeaders()); }
Example 20
Source File: SimpleBrokerMessageHandler.java From spring4-understanding with Apache License 2.0 | 4 votes |
@Override protected void handleMessageInternal(Message<?> message) { MessageHeaders headers = message.getHeaders(); SimpMessageType messageType = SimpMessageHeaderAccessor.getMessageType(headers); String destination = SimpMessageHeaderAccessor.getDestination(headers); String sessionId = SimpMessageHeaderAccessor.getSessionId(headers); Principal user = SimpMessageHeaderAccessor.getUser(headers); updateSessionReadTime(sessionId); if (!checkDestinationPrefix(destination)) { return; } SimpMessageHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message, SimpMessageHeaderAccessor.class); if (accessor == null) { throw new IllegalStateException( "No header accessor (not using the SimpMessagingTemplate?): " + message); } if (SimpMessageType.MESSAGE.equals(messageType)) { logMessage(message); sendMessageToSubscribers(destination, message); } else if (SimpMessageType.CONNECT.equals(messageType)) { logMessage(message); long[] clientHeartbeat = SimpMessageHeaderAccessor.getHeartbeat(headers); long[] serverHeartbeat = getHeartbeatValue(); this.sessions.put(sessionId, new SessionInfo(sessionId, user, clientHeartbeat, serverHeartbeat)); SimpMessageHeaderAccessor connectAck = SimpMessageHeaderAccessor.create(SimpMessageType.CONNECT_ACK); initHeaders(connectAck); connectAck.setSessionId(sessionId); connectAck.setUser(SimpMessageHeaderAccessor.getUser(headers)); connectAck.setHeader(SimpMessageHeaderAccessor.CONNECT_MESSAGE_HEADER, message); connectAck.setHeader(SimpMessageHeaderAccessor.HEART_BEAT_HEADER, serverHeartbeat); Message<byte[]> messageOut = MessageBuilder.createMessage(EMPTY_PAYLOAD, connectAck.getMessageHeaders()); getClientOutboundChannel().send(messageOut); } else if (SimpMessageType.DISCONNECT.equals(messageType)) { logMessage(message); handleDisconnect(sessionId, user); } else if (SimpMessageType.SUBSCRIBE.equals(messageType)) { logMessage(message); this.subscriptionRegistry.registerSubscription(message); } else if (SimpMessageType.UNSUBSCRIBE.equals(messageType)) { logMessage(message); this.subscriptionRegistry.unregisterSubscription(message); } }