NullPointerException
NullPointerException

Reputation: 3814

Passing data (transactionId) to CXF interceptors

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>

Upvotes: 0

Views: 3667

Answers (2)

NullPointerException
NullPointerException

Reputation: 3814

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());
        }
}

Upvotes: 3

nadirsaghar
nadirsaghar

Reputation: 577

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

Upvotes: 0

Related Questions