本文整理汇总了Java中org.springframework.messaging.support.MessageBuilder.createMessage方法的典型用法代码示例。如果您正苦于以下问题:Java MessageBuilder.createMessage方法的具体用法?Java MessageBuilder.createMessage怎么用?Java MessageBuilder.createMessage使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.springframework.messaging.support.MessageBuilder
的用法示例。
在下文中一共展示了MessageBuilder.createMessage方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: connectReceiveAndCloseWithStompFrame
import org.springframework.messaging.support.MessageBuilder; //导入方法依赖的package包/类
@Test
public void connectReceiveAndCloseWithStompFrame() throws Exception {
StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.SEND);
accessor.setDestination("/destination");
MessageHeaders headers = accessor.getMessageHeaders();
Message<byte[]> message = MessageBuilder.createMessage("body".getBytes(Charset.forName("UTF-8")), headers);
byte[] bytes = new StompEncoder().encode(message);
TextMessage textMessage = new TextMessage(bytes);
SockJsFrame frame = SockJsFrame.messageFrame(new Jackson2SockJsMessageCodec(), textMessage.getPayload());
String body = "o\n" + frame.getContent() + "\n" + "c[3000,\"Go away!\"]";
ClientHttpResponse response = response(HttpStatus.OK, body);
connect(response);
verify(this.webSocketHandler).afterConnectionEstablished(any());
verify(this.webSocketHandler).handleMessage(any(), eq(textMessage));
verify(this.webSocketHandler).afterConnectionClosed(any(), eq(new CloseStatus(3000, "Go away!")));
verifyNoMoreInteractions(this.webSocketHandler);
}
示例2: brokerChannelUsedByAnnotatedMethod
import org.springframework.messaging.support.MessageBuilder; //导入方法依赖的package包/类
@Test
public void brokerChannelUsedByAnnotatedMethod() {
TestChannel channel = this.simpleBrokerContext.getBean("brokerChannel", TestChannel.class);
SimpAnnotationMethodMessageHandler messageHandler =
this.simpleBrokerContext.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()));
}
示例3: createMessage
import org.springframework.messaging.support.MessageBuilder; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
private Message<byte[]> createMessage(StompHeaderAccessor accessor, 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;
}
示例4: handleMessage
import org.springframework.messaging.support.MessageBuilder; //导入方法依赖的package包/类
@Override
public void handleMessage(Message<?> message) throws MessagingException {
String destination = getDestination(message);
if (destination == null) {
return;
}
String lookupDestination = getLookupDestination(destination);
if (lookupDestination == null) {
return;
}
MessageHeaderAccessor headerAccessor = MessageHeaderAccessor.getMutableAccessor(message);
headerAccessor.setHeader(DestinationPatternsMessageCondition.LOOKUP_DESTINATION_HEADER, lookupDestination);
headerAccessor.setLeaveMutable(true);
message = MessageBuilder.createMessage(message.getPayload(), headerAccessor.getMessageHeaders());
if (logger.isDebugEnabled()) {
logger.debug("Searching methods to handle " + headerAccessor.getShortLogMessage(message.getPayload()));
}
handleMessageInternal(message, lookupDestination);
headerAccessor.setImmutable();
}
示例5: sendToWithDestinationPlaceholders
import org.springframework.messaging.support.MessageBuilder; //导入方法依赖的package包/类
@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());
}
示例6: setAttributesFromMessage
import org.springframework.messaging.support.MessageBuilder; //导入方法依赖的package包/类
@Test
public void setAttributesFromMessage() {
String sessionId = "session1";
ConcurrentHashMap<String, Object> map = new ConcurrentHashMap<>();
SimpMessageHeaderAccessor headerAccessor = SimpMessageHeaderAccessor.create();
headerAccessor.setSessionId(sessionId);
headerAccessor.setSessionAttributes(map);
Message<?> message = MessageBuilder.createMessage("", headerAccessor.getMessageHeaders());
SimpAttributesContextHolder.setAttributesFromMessage(message);
SimpAttributes attrs = SimpAttributesContextHolder.getAttributes();
assertThat(attrs, notNullValue());
assertThat(attrs.getSessionId(), is(sessionId));
attrs.setAttribute("name1", "value1");
assertThat(map.get("name1"), is("value1"));
}
示例7: sendErrorMessage
import org.springframework.messaging.support.MessageBuilder; //导入方法依赖的package包/类
private void sendErrorMessage(String message, String sessionId) {
StompHeaderAccessor headerAccessor = StompHeaderAccessor.create(StompCommand.ERROR);
headerAccessor.setMessage(message);
headerAccessor.setSessionId(sessionId);
Message<byte[]> error = MessageBuilder.createMessage(new byte[0], headerAccessor.getMessageHeaders());
System.out.println("Error: " + message);
this.clientOutboundChannel.send(error);
}
示例8: sendStompErrorFrameToClient
import org.springframework.messaging.support.MessageBuilder; //导入方法依赖的package包/类
private void sendStompErrorFrameToClient(String errorText) {
if (this.isRemoteClientSession) {
StompHeaderAccessor headerAccessor = StompHeaderAccessor.create(StompCommand.ERROR);
if (getHeaderInitializer() != null) {
getHeaderInitializer().initHeaders(headerAccessor);
}
headerAccessor.setSessionId(this.sessionId);
headerAccessor.setUser(this.connectHeaders.getUser());
headerAccessor.setMessage(errorText);
Message<?> errorMessage = MessageBuilder.createMessage(EMPTY_PAYLOAD, headerAccessor.getMessageHeaders());
handleInboundMessage(errorMessage);
}
}
示例9: doSendWithStompHeaders
import org.springframework.messaging.support.MessageBuilder; //导入方法依赖的package包/类
@Test
public void doSendWithStompHeaders() {
StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.SUBSCRIBE);
accessor.setDestination("/user/queue/foo");
Message<?> message = MessageBuilder.createMessage(new byte[0], accessor.getMessageHeaders());
this.messagingTemplate.doSend("/queue/foo-user123", message);
List<Message<byte[]>> messages = this.messageChannel.getMessages();
Message<byte[]> sentMessage = messages.get(0);
MessageHeaderAccessor sentAccessor = MessageHeaderAccessor.getAccessor(sentMessage, MessageHeaderAccessor.class);
assertEquals(StompHeaderAccessor.class, sentAccessor.getClass());
assertEquals("/queue/foo-user123", ((StompHeaderAccessor) sentAccessor).getDestination());
}
示例10: doSend
import org.springframework.messaging.support.MessageBuilder; //导入方法依赖的package包/类
@Override
protected void doSend(String destination, Message<?> message) {
Assert.notNull(destination, "Destination must not be null");
SimpMessageHeaderAccessor simpAccessor =
MessageHeaderAccessor.getAccessor(message, SimpMessageHeaderAccessor.class);
if (simpAccessor != null) {
if (simpAccessor.isMutable()) {
simpAccessor.setDestination(destination);
simpAccessor.setMessageTypeIfNotSet(SimpMessageType.MESSAGE);
simpAccessor.setImmutable();
sendInternal(message);
return;
}
else {
// Try and keep the original accessor type
simpAccessor = (SimpMessageHeaderAccessor) MessageHeaderAccessor.getMutableAccessor(message);
initHeaders(simpAccessor);
}
}
else {
simpAccessor = SimpMessageHeaderAccessor.wrap(message);
initHeaders(simpAccessor);
}
simpAccessor.setDestination(destination);
simpAccessor.setMessageTypeIfNotSet(SimpMessageType.MESSAGE);
message = MessageBuilder.createMessage(message.getPayload(), simpAccessor.getMessageHeaders());
sendInternal(message);
}
示例11: message
import org.springframework.messaging.support.MessageBuilder; //导入方法依赖的package包/类
private Message<byte[]> message(StompCommand command, String sessionId, String user, String destination) {
StompHeaderAccessor accessor = StompHeaderAccessor.create(command);
if (sessionId != null) {
accessor.setSessionId(sessionId);
}
if (user != null) {
accessor.setUser(new TestPrincipal(user));
}
if (destination != null) {
accessor.setDestination(destination);
}
accessor.setLeaveMutable(true);
return MessageBuilder.createMessage(new byte[0], accessor.getMessageHeaders());
}
示例12: handleMessageToClientWithDestinationUserNameProvider
import org.springframework.messaging.support.MessageBuilder; //导入方法依赖的package包/类
@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());
}
示例13: clientOutboundChannelUsedBySimpleBroker
import org.springframework.messaging.support.MessageBuilder; //导入方法依赖的package包/类
@Test
public void clientOutboundChannelUsedBySimpleBroker() {
TestChannel channel = this.simpleBrokerContext.getBean("clientOutboundChannel", TestChannel.class);
SimpleBrokerMessageHandler broker = this.simpleBrokerContext.getBean(SimpleBrokerMessageHandler.class);
StompHeaderAccessor headers = StompHeaderAccessor.create(StompCommand.SUBSCRIBE);
headers.setSessionId("sess1");
headers.setSubscriptionId("subs1");
headers.setDestination("/foo");
Message<?> message = MessageBuilder.createMessage(new byte[0], headers.getMessageHeaders());
// subscribe
broker.handleMessage(message);
headers = StompHeaderAccessor.create(StompCommand.SEND);
headers.setSessionId("sess1");
headers.setDestination("/foo");
message = MessageBuilder.createMessage("bar".getBytes(), headers.getMessageHeaders());
// message
broker.handleMessage(message);
message = channel.messages.get(0);
headers = StompHeaderAccessor.wrap(message);
assertEquals(SimpMessageType.MESSAGE, headers.getMessageType());
assertEquals("/foo", headers.getDestination());
assertEquals("bar", new String((byte[]) message.getPayload()));
}
示例14: handleClientMessageProcessingErrorWithReceipt
import org.springframework.messaging.support.MessageBuilder; //导入方法依赖的package包/类
@Test
public void handleClientMessageProcessingErrorWithReceipt() throws Exception {
String receiptId = "123";
StompHeaderAccessor clientHeaderAccessor = StompHeaderAccessor.create(StompCommand.SEND);
clientHeaderAccessor.setReceipt(receiptId);
MessageHeaders clientHeaders = clientHeaderAccessor.getMessageHeaders();
Message<byte[]> clientMessage = MessageBuilder.createMessage(new byte[0], clientHeaders);
Message<byte[]> actual = this.handler.handleClientMessageProcessingError(clientMessage, new Exception());
StompHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(actual, StompHeaderAccessor.class);
assertNotNull(accessor);
assertEquals(receiptId, accessor.getReceiptId());
}
示例15: should_set_random_traceid_if_header_value_is_invalid
import org.springframework.messaging.support.MessageBuilder; //导入方法依赖的package包/类
@Test
public void should_set_random_traceid_if_header_value_is_invalid() {
Message message = MessageBuilder.createMessage("",
headers("invalid", randomId()));
try {
this.extractor.joinTrace(message);
fail("should throw an exception");
} catch (IllegalArgumentException e) {
then(e).hasMessageContaining("Malformed id");
}
}