0

I have a middleware java spring application that publishes services on the top layer as well as has Axis 2 clients for service consumption on the bottom layer. When requests are made on the service layer a session ID is added to the thread context using MDC (log4j 1.x).

All of our logging from the service layer (top) and integration layer (bottom) use the MDC to output the session id so these logs can be correlated.

The difficulty is I also had a need to output the request and response xml for the axis2 clients. I created the following handler to accomplish this:

public class AxisXmlLoggerHandler extends AbstractHandler {
  static Logger log = LoggerFactory.getLogger("xmlLogger");

  @Override
  public InvocationResponse invoke(MessageContext mc) throws AxisFault {
    log.info(mc.getEnvelope().toString());

    return InvocationResponse.CONTINUE;
  }
}

Also added this to axis2.xml in InFlow, InFaultFlow, OutFlow, OutFaultFlow

<handler name="XMLLoggerDispatcher" class="com.xxx.xxx.logging.AxisXmlLoggerHandler"/>

This worked perfectly for logging the xml, however the session ID was not in the MDC, I'm assuming because the MDC gets copied to child threads and the handlers are executed from a different pool.

So the Question I'm facing is, how do I get the MDC onto the handler thread?

Although what I really need is the session ID, so maybe there are other options. For instance, we add the session ID on the outgoing header, so I thought if I could access the header from the handler, and find a way to correlate the request and response messages via some other mechanism (custom lifecycle), that would also solve my problem. However I have not been able to find any headers in the soap envelope from the handler. I tried in the following two ways from the handler class:

Iterator<SOAPHeader> hi = mc.getEnvelope().getHeader().examineAllHeaderBlocks();
if (!hi.hasNext()) log.info("no headers using examineAllHeaderBlocks");
while (hi.hasNext()) {
  SOAPHeader header = hi.next();
  log.info("header: "+header.getLocalName()+"   "+header.getText()+"   string: "+header.toString());
}

Iterator<OMElement> i = (Iterator<OMElement>) mc.getEnvelope().getHeader().getChildElements();
if (!i.hasNext()) log.info("no headers using getChildElements");
while (i.hasNext()) {
  OMElement test = i.next();
  log.info("header child: "+test.getText());
}

In both cases the logs show no headers. I've also tried moving to different phases within the flows with no affect. Any help would be greatly appriciated

1 Answers1

0

To get request headers use MessageContext.getCurrentMessageContext, skeleton can get the session ID and set it as header and access the same header in Handler and add Session ID to your response or in ur Logger MDC (similar to Correleation ID)

In handler parameter for invoke method is response message context

   invoke(MessageContext msgContext) - This is response message context

    MessageContext reqMsgCtx = MessageContext.getCurrentMessageContext(); - This gives    request message context
    //Get header with following code
    SOAPEnvelope env = reqMsgCtx.getEnvelope();
    SOAPHeader aSoapHeader = env.getHeader();
   //Your code to add the headerblocks to response message header 
Mano
  • 1
  • 2
  • This seems reasonable, unfortunately I saw this after leaving the company and can't validate in our code anymore. but hope this helps someone else – Joshua Page Mar 24 '17 at 19:33