Archive

Archive for April, 2013

Does Netty call System.exit?

Yesterday I struggled with a very frustrating issue. I was writing a NIO server using the excellent Netty framework and controlled it from jconsole using a custom MBean. To terminate the application I would call the MBean’s stop method and that would call the server’s stop method, which would shut everything down. No problems so far, but I needed to release several resources in a certain order and for some mysterious reason the JVM would terminate in the middle of the stop method, skipping several important steps!

I tracked the problem to Bootstrap.releaseExternalResources. A short time after that method was called the application would die. I moved the call around in the method and added sleep statements before and after and plenty of logging. Sure enough, whenever releaseExternalResources had been called the time was up.

Next I tried to find a call to System.exit in the Netty code. There was none. What could be causing this?

In hindsight it is obvious. The MBean server uses a daemon thread. When releaseExternalResources shuts down the Netty thread pools, the last non-daemon thread stops. At that point the JVM terminates.

I solved the problem by starting a new non-daemon thread from the MBean specifically for running the stop operation.

Advertisements
Categories: Java

Unused namespaces begone!

We recently ran into a tricky problem with SOA Suite 11g: unused namespaces in XML files. Consider a simple input file like this one:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<a:A xmlns="http://www.example.org/A" xmlns:a="http://www.example.org/A"
     xmlns:b="http://www.example.org/B">
   <b:B xmlns="http://www.example.org/B">
      <Name>Name 1</Name>
   </b:B>
</a:A>

Now, we want to transform it into an output file that consists of the element B. The transformation is simple:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<xsl:stylesheet version="1.0" xmlns:a="http://www.example.org/A"
                xmlns:b="http://www.example.org/B"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                exclude-result-prefixes="a b">
  <xsl:template match="b:B">
    <xsl:copy-of select="."/>
  </xsl:template>
</xsl:stylesheet>

Unfortunately the result is not so good:

<?xml version = '1.0' encoding = 'UTF-8'?>
<b:B xmlns="http://www.example.org/B" xmlns:b="http://www.example.org/B"
     xmlns:a="http://www.example.org/A">
      <Name>Name1</Name>
</b:B>

The resulting file refers to namespace A. This is allowed by the XSLT specification, but some XML engines fail to read the resulting file as they choke on the unknown namespace. What to do?

Fortunately SOA Suite 11g supports XSLT 2, at least partly. Change version to 2.0 and add copy-namespaces to the copy-of statement:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<xsl:stylesheet version="2.0"
                xmlns:a="http://www.example.org/A"
                xmlns:b="http://www.example.org/B"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                exclude-result-prefixes="a b">
  <xsl:template match="b:B">
    <xsl:copy-of select="." copy-namespaces="no"/>
  </xsl:template>
</xsl:stylesheet>

This produces the desired result. XSLT 2 has many other useful features, so it is well worth investigating even though it is not fully supported in SOA Suite yet.

Categories: SOA Suite