Passing data (transactionId) to CXF interceptors

北城以北 提交于 2019-12-13 21:22:38

问题


I trying to build an UI to view the SOAP transactions that were sent out from my application.

A typical scenario is that each user transaction include multiple web-service request to multiple systems and I am generating a transactionId for that user transaction and Logging all the logs with that transactionId in log file, so that the log file can be searched using the transactionId and the corrosponding log statements can be displayed on UI.

So, I am able to append the generated transactionId to all the log statements and able to pull them from log files.

But the issue is that for the SOAP request, I am unable to figure out a way to to add the same transactionId in the log file in SOAP request.

Can someone provide me some pointers how can I pass the generated transactionId to CXF interceptors ( or custom CXF interceptors) so that when the CXF logs the SOAP request and repsonse it will append the passed transactionId.

Like this

INFO: Outbound Message
---------------------------
transactionId=1234ABCXXX
ID: 1
Address: http://localhost:8080/Zservice/get?wsdl
Encoding: UTF-8
Http-Method: POST
Content-Type: text/xml
Headers: {Accept=[*/*], SOAPAction=[""]}
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:get xmlns:ns2="http://service.zservice.com/"><arg0/></ns2:get></soap:Body></soap:Envelope>

回答1:


I am finally able to active this using MDC

In your web-service adapter where you are invoking the web-service client set the transaction Id in MDC like this.

      MDC.put("transaction", transId);        
      Account acct= new Account();
       //Set the requrest 

      //invoke WS client
      client.get(acct);

In your custom interceptors, retrieve the transaction Id

public void handleMessage(SoapMessage message) {

    String transaction = MDC.get("transaction");

    logger.info("Transaction ID: {} ", transaction);
    try {

         LogInUtil.logging(logger, message, transaction);
         }
        catch (Exception ex) {
           logger.warn("Unable to save SOAP Response due to {}",ex.getMessage());
        }
}



回答2:


If you are trying to use a unique transaction-id for each request in order to associate it with the request, you need to get hold of the org.apache.cxf.message.Message instance. It is passed as parameter to all interceptors in the handleMessage() method. You can also use org.apache.cxf.phase.PhaseInterceptorChain.getCurrentMessage() in static context to get hold of it anywhere in your application.

You can then create the transaction-id in InInterceptor as in

Message message = PhaseInterceptorChain.getCurrentMessage();
message.put("transaction-id",UUID.randomUUID().toString());

In your code where you are logging the SOAP request/response you can retrieve and log it

Message message = PhaseInterceptorChain.getCurrentMessage();
String transactionId = message.get("transaction-id");
// happy logging


来源:https://stackoverflow.com/questions/16347989/passing-data-transactionid-to-cxf-interceptors

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!