本文整理汇总了Java中org.springframework.messaging.MessageHeaders类的典型用法代码示例。如果您正苦于以下问题:Java MessageHeaders类的具体用法?Java MessageHeaders怎么用?Java MessageHeaders使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
MessageHeaders类属于org.springframework.messaging包,在下文中一共展示了MessageHeaders类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: addComment
import org.springframework.messaging.MessageHeaders; //导入依赖的package包/类
@PostMapping("/comments")
public Mono<String> addComment(Mono<Comment> newComment) {
if (commentSink != null) {
return newComment
.map(comment -> {
commentSink.next(MessageBuilder
.withPayload(comment)
.setHeader(MessageHeaders.CONTENT_TYPE,
MediaType.APPLICATION_JSON_VALUE)
.build());
return comment;
})
.flatMap(comment -> {
meterRegistry
.counter("comments.produced", "imageId", comment.getImageId())
.increment();
return Mono.just("redirect:/");
});
} else {
return Mono.just("redirect:/");
}
}
开发者ID:PacktPublishing,项目名称:Learning-Spring-Boot-2.0-Second-Edition,代码行数:23,代码来源:CommentController.java
示例2: addComment
import org.springframework.messaging.MessageHeaders; //导入依赖的package包/类
@PreAuthorize("hasRole('USER')")
@PostMapping("/comments")
public Mono<ResponseEntity<?>> addComment(Mono<Comment> newComment) {
if (commentSink != null) {
return newComment
.map(comment -> {
System.out.println("Got " + comment);
commentSink.next(MessageBuilder
.withPayload(comment)
.setHeader(MessageHeaders.CONTENT_TYPE,
MediaType.APPLICATION_JSON_VALUE)
.build());
return comment;
})
.flatMap(comment -> {
meterRegistry
.counter("comments.produced", "imageId", comment.getImageId())
.increment();
return Mono.just(ResponseEntity.noContent().build());
});
} else {
return Mono.just(ResponseEntity.noContent().build());
}
}
开发者ID:PacktPublishing,项目名称:Learning-Spring-Boot-2.0-Second-Edition,代码行数:25,代码来源:CommentController.java
示例3: addComment
import org.springframework.messaging.MessageHeaders; //导入依赖的package包/类
@PostMapping("/comments")
public Mono<ResponseEntity<?>> addComment(Mono<Comment> newComment) {
if (commentSink != null) {
return newComment
.map(comment -> {
commentSink.next(MessageBuilder
.withPayload(comment)
.setHeader(MessageHeaders.CONTENT_TYPE,
MediaType.APPLICATION_JSON_VALUE)
.build());
return comment;
})
.flatMap(comment -> {
meterRegistry
.counter("comments.produced", "imageId", comment.getImageId())
.increment();
return Mono.just(ResponseEntity.noContent().build());
});
} else {
return Mono.just(ResponseEntity.noContent().build());
}
}
开发者ID:PacktPublishing,项目名称:Learning-Spring-Boot-2.0-Second-Edition,代码行数:23,代码来源:CommentController.java
示例4: testSendMessageToEntity
import org.springframework.messaging.MessageHeaders; //导入依赖的package包/类
@Test
public void testSendMessageToEntity() throws Exception {
entityService.sendMessageToEntity(entity, output);
verify(simpMessagingTemplate).convertAndSendToUser(
eq("stompUsername"),
eq("/queue/output"),
eq(output),
messageHeadersArgumentCaptor.capture());
MessageHeaders headers = messageHeadersArgumentCaptor.getValue();
SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.getAccessor(headers, SimpMessageHeaderAccessor.class);
assertEquals("stompSessionId", accessor.getSessionId());
assertTrue(accessor.isMutable());
}
示例5: verifyContents
import org.springframework.messaging.MessageHeaders; //导入依赖的package包/类
private void verifyContents() {
for (int i = 0; i < contents.size(); i++) {
verify(simpMessagingTemplate).convertAndSendToUser(
eq("stompUsername" + i),
eq("/queue/output"),
eq(output),
messageHeadersArgumentCaptor.capture()
);
MessageHeaders headers = messageHeadersArgumentCaptor.getValue();
SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.getAccessor(headers, SimpMessageHeaderAccessor.class);
assertEquals("stompSessionId" + i, accessor.getSessionId());
assertTrue(accessor.isMutable());
}
}
示例6: authenticateUser
import org.springframework.messaging.MessageHeaders; //导入依赖的package包/类
/**
* Authenticate user before he is allowed to connect to
* the socket.
*
* @param message Message<?>
* @return Principal
*/
private Principal authenticateUser(Message<?> message) {
MessageHeaders headers = message.getHeaders();
String token = this.parseToken(headers);
UserService service = new UserService();
Traveler user = service.getUserByToken(token);
if (user.getUid() == null) {
System.out.println("invalid token");
return null;
}
System.out.println("valid token");
System.out.println(user.getUid());
return user;
}
示例7: receiveMessage
import org.springframework.messaging.MessageHeaders; //导入依赖的package包/类
private void receiveMessage(PubsubMessage message, AckReplyConsumer consumer) {
Map<String, Object> messageHeaders = new HashMap<>();
message.getAttributesMap().forEach(messageHeaders::put);
if (this.ackMode == AckMode.MANUAL) {
// Send the consumer downstream so user decides on when to ack/nack.
messageHeaders.put(GcpHeaders.ACKNOWLEDGEMENT, consumer);
}
try {
sendMessage(this.messageConverter.toMessage(
message.getData().toStringUtf8(),
new MessageHeaders(messageHeaders)));
}
catch (RuntimeException re) {
if (this.ackMode == AckMode.AUTO) {
consumer.nack();
}
throw re;
}
if (this.ackMode == AckMode.AUTO) {
consumer.ack();
}
}
示例8: testWorkDelegatesMethodCall
import org.springframework.messaging.MessageHeaders; //导入依赖的package包/类
@Test
public void testWorkDelegatesMethodCall() throws Exception {
// given
AtomicInteger counter = new AtomicInteger(0);
MessageWorker<String, Integer> uut = new MessageWorker<String, Integer>() {
@Override
public Integer work(String object) {
return counter.incrementAndGet();
}
};
MessageHeaders messageHeaders = mock(MessageHeaders.class);
// when
uut.work("dummy value", messageHeaders);
// then
assertEquals(1, counter.intValue());
verifyZeroInteractions(messageHeaders);
}
示例9: canConvertTo
import org.springframework.messaging.MessageHeaders; //导入依赖的package包/类
@Override
protected boolean canConvertTo(Object payload, MessageHeaders headers) {
if (!jackson23Available || !logger.isWarnEnabled()) {
return (this.objectMapper.canSerialize(payload.getClass()) && supportsMimeType(headers));
}
AtomicReference<Throwable> causeRef = new AtomicReference<Throwable>();
if (this.objectMapper.canSerialize(payload.getClass(), causeRef) && supportsMimeType(headers)) {
return true;
}
Throwable cause = causeRef.get();
if (cause != null) {
String msg = "Failed to evaluate serialization for type [" + payload.getClass() + "]";
if (logger.isDebugEnabled()) {
logger.warn(msg, cause);
}
else {
logger.warn(msg + ": " + cause);
}
}
return false;
}
示例10: resolve
import org.springframework.messaging.MessageHeaders; //导入依赖的package包/类
@Override
public MimeType resolve(MessageHeaders headers) {
if (headers == null || headers.get(MessageHeaders.CONTENT_TYPE) == null) {
return this.defaultMimeType;
}
Object value = headers.get(MessageHeaders.CONTENT_TYPE);
if (value instanceof MimeType) {
return (MimeType) value;
}
else if (value instanceof String) {
return MimeType.valueOf((String) value);
}
else {
throw new IllegalArgumentException(
"Unknown type for contentType header value: " + value.getClass());
}
}
示例11: setup
import org.springframework.messaging.MessageHeaders; //导入依赖的package包/类
@Before
public void setup() throws Exception {
this.resolver = new HeadersMethodArgumentResolver();
Method method = getClass().getDeclaredMethod("handleMessage", Map.class, String.class,
MessageHeaders.class, MessageHeaderAccessor.class, TestMessageHeaderAccessor.class);
this.paramAnnotated = new MethodParameter(method, 0);
this.paramAnnotatedNotMap = new MethodParameter(method, 1);
this.paramMessageHeaders = new MethodParameter(method, 2);
this.paramMessageHeaderAccessor = new MethodParameter(method, 3);
this.paramMessageHeaderAccessorSubclass = new MethodParameter(method, 4);
Map<String, Object> headers = new HashMap<String, Object>();
headers.put("foo", "bar");
this.message = MessageBuilder.withPayload(new byte[0]).copyHeaders(headers).build();
}
示例12: existingHeadersModification
import org.springframework.messaging.MessageHeaders; //导入依赖的package包/类
@Test
public void existingHeadersModification() throws InterruptedException {
Map<String, Object> map = new HashMap<>();
map.put("foo", "bar");
map.put("bar", "baz");
GenericMessage<String> message = new GenericMessage<>("payload", map);
Thread.sleep(50);
MessageHeaderAccessor accessor = new MessageHeaderAccessor(message);
accessor.setHeader("foo", "BAR");
MessageHeaders actual = accessor.getMessageHeaders();
assertEquals(3, actual.size());
assertNotEquals(message.getHeaders().getId(), actual.getId());
assertEquals("BAR", actual.get("foo"));
assertEquals("baz", actual.get("bar"));
}
示例13: sendSystemSubscriptions
import org.springframework.messaging.MessageHeaders; //导入依赖的package包/类
private void sendSystemSubscriptions() {
int i = 0;
for (String destination : getSystemSubscriptions().keySet()) {
StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.SUBSCRIBE);
accessor.setSubscriptionId(String.valueOf(i++));
accessor.setDestination(destination);
if (logger.isDebugEnabled()) {
logger.debug("Subscribing to " + destination + " on \"system\" connection.");
}
TcpConnection<byte[]> conn = getTcpConnection();
if (conn != null) {
MessageHeaders headers = accessor.getMessageHeaders();
conn.send(MessageBuilder.createMessage(EMPTY_PAYLOAD, headers)).addCallback(
new ListenableFutureCallback<Void>() {
public void onSuccess(Void result) {
}
public void onFailure(Throwable ex) {
String error = "Failed to subscribe in \"system\" session.";
handleTcpConnectionFailure(error, ex);
}
});
}
}
}
示例14: testHeadersToSend
import org.springframework.messaging.MessageHeaders; //导入依赖的package包/类
@Test
public void testHeadersToSend() throws Exception {
Message<?> inputMessage = createInputMessage("sess1", "sub1", "/app", "/dest", null);
SimpMessageSendingOperations messagingTemplate = Mockito.mock(SimpMessageSendingOperations.class);
SendToMethodReturnValueHandler handler = new SendToMethodReturnValueHandler(messagingTemplate, false);
handler.handleReturnValue(PAYLOAD, this.noAnnotationsReturnType, inputMessage);
ArgumentCaptor<MessageHeaders> captor = ArgumentCaptor.forClass(MessageHeaders.class);
verify(messagingTemplate).convertAndSend(eq("/topic/dest"), eq(PAYLOAD), captor.capture());
MessageHeaders messageHeaders = captor.getValue();
SimpMessageHeaderAccessor accessor = getAccessor(messageHeaders, SimpMessageHeaderAccessor.class);
assertNotNull(accessor);
assertTrue(accessor.isMutable());
assertEquals("sess1", accessor.getSessionId());
assertNull("Subscription id should not be copied", accessor.getSubscriptionId());
assertEquals(this.noAnnotationsReturnType, accessor.getHeader(SimpMessagingTemplate.CONVERSION_HINT_HEADER));
}
示例15: unregisterSubscription
import org.springframework.messaging.MessageHeaders; //导入依赖的package包/类
@Override
public final void unregisterSubscription(Message<?> message) {
MessageHeaders headers = message.getHeaders();
SimpMessageType messageType = SimpMessageHeaderAccessor.getMessageType(headers);
if (!SimpMessageType.UNSUBSCRIBE.equals(messageType)) {
throw new IllegalArgumentException("Expected UNSUBSCRIBE: " + message);
}
String sessionId = SimpMessageHeaderAccessor.getSessionId(headers);
if (sessionId == null) {
logger.error("No sessionId in " + message);
return;
}
String subscriptionId = SimpMessageHeaderAccessor.getSubscriptionId(headers);
if (subscriptionId == null) {
logger.error("No subscriptionId " + message);
return;
}
removeSubscriptionInternal(sessionId, subscriptionId, message);
}