org.springframework.web.socket.messaging.WebSocketAnnotationMethodMessageHandler Java Examples

The following examples show how to use org.springframework.web.socket.messaging.WebSocketAnnotationMethodMessageHandler. 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: TracingChannelInterceptorTest.java    From java-spring-cloud with Apache License 2.0 6 votes vote down vote up
@Test
public void testAfterMessageHandled() {
  Span span = mock(Span.class);
  Scope scope = mock(Scope.class);
  MessageHandler messageHandler = mock(WebSocketAnnotationMethodMessageHandler.class);
  MessageBuilder<String> messageBuilder = MessageBuilder.withPayload("Hi")
      .setHeader(TracingChannelInterceptor.SIMP_MESSAGE_TYPE, SimpMessageType.MESSAGE)
      .setHeader(TracingChannelInterceptor.SIMP_DESTINATION, TEST_DESTINATION)
      .setHeader(TracingChannelInterceptor.OPENTRACING_SCOPE, scope)
      .setHeader(TracingChannelInterceptor.OPENTRACING_SPAN, span);

  TracingChannelInterceptor interceptor = new TracingChannelInterceptor(mockTracer,
      Tags.SPAN_KIND_CLIENT);
  interceptor.afterMessageHandled(messageBuilder.build(), null, messageHandler, null);

  // Verify span is finished and scope is closed
  verify(span).finish();
  verify(scope).close();
}
 
Example #2
Source File: MessageBrokerBeanDefinitionParser.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private void registerAnnotationMethodMessageHandler(Element messageBrokerElement,
		RuntimeBeanReference inChannel, RuntimeBeanReference outChannel,
		RuntimeBeanReference converter, RuntimeBeanReference messagingTemplate,
		ParserContext context, @Nullable Object source) {

	ConstructorArgumentValues cargs = new ConstructorArgumentValues();
	cargs.addIndexedArgumentValue(0, inChannel);
	cargs.addIndexedArgumentValue(1, outChannel);
	cargs.addIndexedArgumentValue(2, messagingTemplate);

	MutablePropertyValues values = new MutablePropertyValues();
	String prefixAttribute = messageBrokerElement.getAttribute("application-destination-prefix");
	values.add("destinationPrefixes", Arrays.asList(StringUtils.tokenizeToStringArray(prefixAttribute, ",")));
	values.add("messageConverter", converter);

	RootBeanDefinition beanDef = new RootBeanDefinition(WebSocketAnnotationMethodMessageHandler.class, cargs, values);
	if (messageBrokerElement.hasAttribute("path-matcher")) {
		String pathMatcherRef = messageBrokerElement.getAttribute("path-matcher");
		beanDef.getPropertyValues().add("pathMatcher", new RuntimeBeanReference(pathMatcherRef));
	}

	RuntimeBeanReference validatorRef = getValidator(messageBrokerElement, source, context);
	if (validatorRef != null) {
		beanDef.getPropertyValues().add("validator", validatorRef);
	}

	Element resolversElement = DomUtils.getChildElementByTagName(messageBrokerElement, "argument-resolvers");
	if (resolversElement != null) {
		values.add("customArgumentResolvers", extractBeanSubElements(resolversElement, context));
	}

	Element handlersElement = DomUtils.getChildElementByTagName(messageBrokerElement, "return-value-handlers");
	if (handlersElement != null) {
		values.add("customReturnValueHandlers", extractBeanSubElements(handlersElement, context));
	}

	registerBeanDef(beanDef, context, source);
}
 
Example #3
Source File: MessageBrokerBeanDefinitionParser.java    From java-technology-stack with MIT License 5 votes vote down vote up
private void registerAnnotationMethodMessageHandler(Element messageBrokerElement,
		RuntimeBeanReference inChannel, RuntimeBeanReference outChannel,
		RuntimeBeanReference converter, RuntimeBeanReference messagingTemplate,
		ParserContext context, @Nullable Object source) {

	ConstructorArgumentValues cargs = new ConstructorArgumentValues();
	cargs.addIndexedArgumentValue(0, inChannel);
	cargs.addIndexedArgumentValue(1, outChannel);
	cargs.addIndexedArgumentValue(2, messagingTemplate);

	MutablePropertyValues values = new MutablePropertyValues();
	String prefixAttribute = messageBrokerElement.getAttribute("application-destination-prefix");
	values.add("destinationPrefixes", Arrays.asList(StringUtils.tokenizeToStringArray(prefixAttribute, ",")));
	values.add("messageConverter", converter);

	RootBeanDefinition beanDef = new RootBeanDefinition(WebSocketAnnotationMethodMessageHandler.class, cargs, values);
	if (messageBrokerElement.hasAttribute("path-matcher")) {
		String pathMatcherRef = messageBrokerElement.getAttribute("path-matcher");
		beanDef.getPropertyValues().add("pathMatcher", new RuntimeBeanReference(pathMatcherRef));
	}

	RuntimeBeanReference validatorRef = getValidator(messageBrokerElement, source, context);
	if (validatorRef != null) {
		beanDef.getPropertyValues().add("validator", validatorRef);
	}

	Element resolversElement = DomUtils.getChildElementByTagName(messageBrokerElement, "argument-resolvers");
	if (resolversElement != null) {
		values.add("customArgumentResolvers", extractBeanSubElements(resolversElement, context));
	}

	Element handlersElement = DomUtils.getChildElementByTagName(messageBrokerElement, "return-value-handlers");
	if (handlersElement != null) {
		values.add("customReturnValueHandlers", extractBeanSubElements(handlersElement, context));
	}

	registerBeanDef(beanDef, context, source);
}
 
Example #4
Source File: TracingChannelInterceptor.java    From java-specialagent with Apache License 2.0 5 votes vote down vote up
@Override
public void afterMessageHandled(final Message<?> message, final MessageChannel channel, final MessageHandler handler, final Exception arg3) {
  if ((handler instanceof WebSocketAnnotationMethodMessageHandler || handler instanceof SubProtocolWebSocketHandler) && SimpMessageType.MESSAGE.equals(message.getHeaders().get(SIMP_MESSAGE_TYPE))) {
    message.getHeaders().get(OPENTRACING_SCOPE, Scope.class).close();
    message.getHeaders().get(OPENTRACING_SPAN, Span.class).finish();
    // MessageHeaders are immutable
    // message.getHeaders().remove(OPENTRACING_SCOPE);
  }
}
 
Example #5
Source File: TracingChannelInterceptor.java    From java-specialagent with Apache License 2.0 5 votes vote down vote up
@Override
public Message<?> beforeHandle(final Message<?> message, final MessageChannel channel, final MessageHandler handler) {
  if ((!(handler instanceof WebSocketAnnotationMethodMessageHandler) && !(handler instanceof SubProtocolWebSocketHandler)) || !SimpMessageType.MESSAGE.equals(message.getHeaders().get(SIMP_MESSAGE_TYPE)))
    return message;

  final Span span = message.getHeaders().get(OPENTRACING_SPAN, Span.class);
  final Scope scope = tracer.scopeManager().activate(span);
  return MessageBuilder.fromMessage(message).setHeader(OPENTRACING_SCOPE, scope).build();
}
 
Example #6
Source File: TracingChannelInterceptor.java    From java-spring-cloud with Apache License 2.0 5 votes vote down vote up
@Override
public void afterMessageHandled(Message<?> message, MessageChannel channel,
    MessageHandler handler, Exception arg3) {
  if ((handler instanceof WebSocketAnnotationMethodMessageHandler ||
      handler instanceof SubProtocolWebSocketHandler) &&
      SimpMessageType.MESSAGE.equals(message.getHeaders().get(SIMP_MESSAGE_TYPE))) {
    message.getHeaders().get(OPENTRACING_SCOPE, Scope.class).close();
    message.getHeaders().get(OPENTRACING_SPAN, Span.class).finish();
  }
}
 
Example #7
Source File: TracingChannelInterceptor.java    From java-spring-cloud with Apache License 2.0 5 votes vote down vote up
@Override
public Message<?> beforeHandle(Message<?> message, MessageChannel channel,
    MessageHandler handler) {
  if ((handler instanceof WebSocketAnnotationMethodMessageHandler ||
      handler instanceof SubProtocolWebSocketHandler) &&
      SimpMessageType.MESSAGE.equals(message.getHeaders().get(SIMP_MESSAGE_TYPE))) {
    Span span = message.getHeaders().get(OPENTRACING_SPAN, Span.class);
    Scope scope = tracer.scopeManager().activate(span);
    message = MessageBuilder.fromMessage(message)
        .setHeader(OPENTRACING_SCOPE, scope)
        .build();
  }
  return message;
}
 
Example #8
Source File: MessageBrokerBeanDefinitionParser.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private void registerAnnotationMethodMessageHandler(Element messageBrokerElement,
		RuntimeBeanReference inChannel, RuntimeBeanReference outChannel,
		RuntimeBeanReference converter, RuntimeBeanReference messagingTemplate,
		ParserContext context, Object source) {

	ConstructorArgumentValues cavs = new ConstructorArgumentValues();
	cavs.addIndexedArgumentValue(0, inChannel);
	cavs.addIndexedArgumentValue(1, outChannel);
	cavs.addIndexedArgumentValue(2, messagingTemplate);

	MutablePropertyValues values = new MutablePropertyValues();
	String prefixAttribute = messageBrokerElement.getAttribute("application-destination-prefix");
	values.add("destinationPrefixes", Arrays.asList(StringUtils.tokenizeToStringArray(prefixAttribute, ",")));
	values.add("messageConverter", converter);

	RootBeanDefinition beanDef = new RootBeanDefinition(WebSocketAnnotationMethodMessageHandler.class, cavs, values);
	if (messageBrokerElement.hasAttribute("path-matcher")) {
		String pathMatcherRef = messageBrokerElement.getAttribute("path-matcher");
		beanDef.getPropertyValues().add("pathMatcher", new RuntimeBeanReference(pathMatcherRef));
	}

	Element resolversElement = DomUtils.getChildElementByTagName(messageBrokerElement, "argument-resolvers");
	if (resolversElement != null) {
		values.add("customArgumentResolvers", extractBeanSubElements(resolversElement, context));
	}

	Element handlersElement = DomUtils.getChildElementByTagName(messageBrokerElement, "return-value-handlers");
	if (handlersElement != null) {
		values.add("customReturnValueHandlers", extractBeanSubElements(handlersElement, context));
	}

	registerBeanDef(beanDef, context, source);
}
 
Example #9
Source File: WebSocketMessageBrokerConfigurationSupport.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
protected SimpAnnotationMethodMessageHandler createAnnotationMethodMessageHandler() {
	return new WebSocketAnnotationMethodMessageHandler(
			clientInboundChannel(), clientOutboundChannel(), brokerMessagingTemplate());
}
 
Example #10
Source File: WebSocketMessageBrokerConfigurationSupport.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
protected SimpAnnotationMethodMessageHandler createAnnotationMethodMessageHandler() {
	return new WebSocketAnnotationMethodMessageHandler(
			clientInboundChannel(), clientOutboundChannel(), brokerMessagingTemplate());
}
 
Example #11
Source File: WebSocketMessageBrokerConfigurationSupport.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
protected SimpAnnotationMethodMessageHandler createAnnotationMethodMessageHandler() {
	return new WebSocketAnnotationMethodMessageHandler(clientInboundChannel(),
			clientOutboundChannel(), brokerMessagingTemplate());
}