RabbitMq侦听器的ServletFilter等效项是什么?

我有一个spring-boot对我实施的Web应用程序MDCFilter,增加了一个UUIDMDC伐木情况下,我可以在日志文件中找到。

Filter类看起来是这样的。

public class MDCFilter implements Filter {

@Override

public void init(FilterConfig filterConfig) {

}

@Override

public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)

throws IOException, ServletException {

String requestId = UUID.randomUUID().toString();

MDC.put(REQUEST_ID_KEY, requestId);

response.addHeader("trace", requestId);

try {

chain.doFilter(req, resp);

} finally {

MDC.remove("trace");

}

}

@Override

public void destroy() {

}

}

但是最近,我们转向通过队列来处理流量,我从文档中不知道要为消息侦听器复制此过滤器行为。

我的听众看起来像这样。

@RabbitListener(queues = "${queue1}")

public void receiveMessages(Message message) {

doTheBusinessLogic(message)

}

谁能指出我正确的方向?

回答:

使用容器的adviceChain。假设您正在使用Boot 2.0和简单的容器工厂,请覆盖boot的工厂以添加建议…

@SpringBootApplication

public class So49770881Application {

public static void main(String[] args) {

SpringApplication.run(So49770881Application.class, args);

}

@Bean(name = "rabbitListenerContainerFactory")

public SimpleRabbitListenerContainerFactory simpleRabbitListenerContainerFactory(

SimpleRabbitListenerContainerFactoryConfigurer configurer,

ConnectionFactory connectionFactory) {

SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();

configurer.configure(factory, connectionFactory);

factory.setAdviceChain(new MDCAdvice());

return factory;

}

public static class MDCAdvice implements MethodInterceptor {

@Override

public Object invoke(MethodInvocation invocation) throws Throwable {

// pre process

try {

return invocation.proceed();

}

finally {

// post process

}

}

}

}

以上是 RabbitMq侦听器的ServletFilter等效项是什么? 的全部内容, 来源链接: utcz.com/qa/409499.html

回到顶部