Wierd problem in linux 2.4.18-bf2.4, Java Blackdown-1.4.2-rc1

Achilleus Mantzios achill at matrix.gatewaynet.com
Fri Jun 24 12:04:46 GMT 2005


Ok its linux a problem, but i think it would be interesting to you.

1st off, this problem does not occur in FreeBSD 5.4-STABLE,
with java 1.4.2p7. (Kudos to FreeBSD java team!!!)

The situation is as follows:

>From a jsp i call 

	java.lang.Process proc = 
		Runtime.getRuntime().exec("/usr/local/sbin/uucico_command");
	proc.waitFor();

/usr/local/sbin/uucico_command has as follows:
------------------------------------------------------------
#!/bin/sh
/bin/rm -f /var/lock/LCK..ttyS*
/usr/local/var/lib/pgsql/dbmirror/AsyncMirrorWithSpecialTables.pl 
/usr/local/var/lib/pgsql/dbmirror/OFFICE.conf
/usr/local/sbin/gpsavg
epochsecs=`date +'%s'`
pushd /usr/local/var/XML_TO_OFFICE/
tar cfj As_AND_Ds_$epochsecs.tar.bz2 A*.xml D*.xml
\rm -f A*.xml D*.xml
popd
/usr/bin/bzip2 /usr/local/var/XML_TO_OFFICE/*
export PGUSER=postgres
export PGPASSWORD=""
vessel_name=`/usr/local/pgsql/bin/psql -A -q -t -h localhost dynacom -c 
"select replace(lower(name),' ','_') from vessels limit 1"`
VESSEL_NAME=`/usr/local/pgsql/bin/psql -A -q -t -h localhost dynacom -c 
"select replace(name,' ','_') from vessels limit 1"`
uucp -r /usr/local/var/XML_TO_OFFICE/*.bz2 
uucphub\!/usr/local/var/XML_FROM_VESSELS/$VESSEL_NAME/.
/bin/rm -fr /usr/local/var/XML_TO_OFFICE/*
/bin/rm -fr /var/spool/uucppublic/outbound > /dev/null 2>&1
/bin/rm -fr /var/spool/uucppublic/inbound > /dev/null 2>&1
outbound=`/usr/bin/wc -c /var/spool/uucp/uucphub/D./* 2>/dev/null | tail 
-1 | awk '{print $1}'`
outbound=${outbound:=0}
echo $outbound > /var/spool/uucppublic/outbound 2>/dev/null
uucp -r -g 0 /var/spool/uucppublic/outbound 
uucphub\!/var/spool/uucppublic/$vessel_name.inbound
\rm -fr /var/spool/uucp/.Status/uucphub
uucp_acct_id=`/usr/local/sbin/dyna_psql $outbound 2>/dev/null`
/usr/local/sbin/dyna_log_uucp_acct_update $uucp_acct_id
uucico -x 4 -f -s uucphub
uucpsc=`/bin/cat /var/spool/uucp/.Status/uucphub | /usr/bin/awk '{print 
$1}'`
uucpsc=${uucpsc:=99}
#logger -p uucp.info "NOTICE!!! acctid="$uucp_acct_id "uucpsc="$uucpsc
/usr/local/j2sdk1.4.2/bin/java -cp 
"/usr/local/jboss-3.0.3/server/default/SMA_UUCP.jar:/usr/local/pgsql/share/java/postgresql.jar:/usr/local/jboss-3.0.3/client/log4j.jar" 
com.gatewaynet.uucp.LogUUCPAcct $uucp_acct_id $uucpsc
/usr/local/j2sdk1.4.2/bin/java -cp 
"/usr/local/jboss-3.0.3/server/default/SMA_UUCP.jar:/usr/local/pgsql/share/java/postgresql.jar:/usr/local/jboss-3.0.3/client/log4j.jar:/usr/local/jboss-3.0.3/server/default/lib/activation.jar:/usr/local/jboss-3.0.3/server/default/lib/mail.jar" 
com.gatewaynet.uucp.SendCollected
pushd /usr/local/var/XML_FROM_OFFICE/
for ASDS in As_AND_Ds*.tar.bz2; do tar xfj $ASDS; rm -f $ASDS; done
popd
/usr/bin/bunzip2 /usr/local/var/XML_FROM_OFFICE/*.bz2
############################################
### The command below is the problematic one
############################################
/usr/local/jboss-3.0.3/server/default/readoff
/etc/cron.daily/compstatus &
------------------------------------------------------------

now the problematic /usr/local/jboss-3.0.3/server/default/readoff has as 
follows
------------------------------------------------------------
#!/bin/sh
export 
CLASSPATH="/usr/local/jboss-3.0.3/server/default/SMA_PER.jar:/usr/local/jboss-3.0.3/server/default/xerces.jar:/usr/local/jboss-3.0.3/client/jbossall-client.jar:/usr/local/jboss-3.0.3/client/log4j.jar:/usr/local/pgsql/share/java/postgresql.jar"

/usr/local/j2sdk1.4.2/bin/java 
-Djavax.xml.parsers.DocumentBuilderFactory=org.apache.xerces.jaxp.DocumentBuilderFactoryImpl 
com.gatewaynet.periodic.ReadXmlFromOffice
------------------------------------------------------------

whereas ReadXmlFromOffice.java has as follows:
------------------------------------------------------------
/*
 * Created on 19 Μαρ 2004, 11:55:47 πμ
 *
 */
package com.gatewaynet.periodic;

import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.sql.PreparedStatement;
import java.util.Arrays;
import java.util.Comparator;

import org.w3c.dom.Node;


/**
 * @author achill4
 *
 * To change the template for this generated type comment go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */

public class ReadXmlFromOffice {

	private static String XMLFROMDIR = 
"/usr/local/var/XML_FROM_OFFICE";
	private static String XMLTODIR = "/usr/local/var/XML_TO_OFFICE";
	
	private static String FILEDIR = 
"/usr/local/var/FILES_FROM_OFFICE";
	
	static private org.apache.log4j.Logger logger = 
org.apache.log4j.Logger.getLogger("dynacom.RXML");

	/**
	 * 
	 */
	public ReadXmlFromOffice() {
		super();
		// TODO Auto-generated constructor stub
	}

	public static void main(String[] args) {
		java.sql.Connection con=null;
		try {
			javax.naming.InitialContext ic = new 
javax.naming.InitialContext();
			Class.forName("org.postgresql.Driver");
			con = 
java.sql.DriverManager.getConnection("jdbc:postgresql://localhost/dynacom","postgres","");
			con.setAutoCommit(false);

			PreparedStatement vslidst = 
con.prepareStatement("select id from vessels limit 1");
			java.sql.ResultSet vslidrs = 
vslidst.executeQuery();
			if (!vslidrs.next()) throw new Exception("No 
vessel in database. Contact IT DEPT.");
			int thisvesselvslid = vslidrs.getInt(1);
			vslidrs.close();
			vslidst.close();
			con.commit();
			
			javax.xml.parsers.DocumentBuilderFactory 
builderFactory =javax.xml.parsers.DocumentBuilderFactory.newInstance();
			builderFactory.setValidating(false);
			
			javax.xml.parsers.DocumentBuilder builder 
=builderFactory.newDocumentBuilder();
			builder.setErrorHandler(new 
com.gatewaynet.web.util.xml.MyErrorHandler());

			File xmldir = new File(XMLFROMDIR);
			File[] xmlfiles = xmldir.listFiles();
			Arrays.sort(xmlfiles,new DirComparator());
			String[] xerrors = new String[xmlfiles.length];
			String lastdone=null;
			int numxerrors=0;
			int xmli;
			for (xmli = 0; xmli < xmlfiles.length; xmli++) {
				File xmlfile = xmlfiles[xmli];
				try {
					String xmlfilename 
=xmlfile.getName(); 
					if (!xmlfilename.endsWith(".xml") 
&& !xmlfilename.endsWith(".XML")) continue;
					org.w3c.dom.Document document = 
builder.parse(xmlfile);
					org.w3c.dom.Element rootel = 
document.getDocumentElement();
					org.w3c.dom.NamedNodeMap attrs = 
rootel.getAttributes();
					String xidstr=null;
					String vslidstr=null;
					org.w3c.dom.Attr attr;
					attr = (org.w3c.dom.Attr) 
attrs.getNamedItem("xid");
					xidstr = attr.getValue();
					attr = (org.w3c.dom.Attr) 
attrs.getNamedItem("vslid");
					vslidstr = attr.getValue();					

					org.w3c.dom.Node curnd;
	
					org.w3c.dom.NodeList nodes = 
rootel.getChildNodes();
	
					String appname=null;
					String apptbl_tmp=null;
					String gao=null;
					String idstr=null;
					String SQL=null;
					String comment=null;
					
					String filename=null;
					String file_base64=null;
					
					String unixcommand=null;

					for (int i = 0; i < 
nodes.getLength(); i++) {
						curnd = nodes.item(i);
						if (curnd.getNodeType() == 
org.w3c.dom.Node.ELEMENT_NODE) {
							if 
(((org.w3c.dom.Element) curnd).getTagName().equals("appname"))
								appname =  
((org.w3c.dom.Text) curnd.getFirstChild()).getData();
								
							if 
(((org.w3c.dom.Element) curnd).getTagName().equals("apptbl_tmp"))
								apptbl_tmp 
=  ((org.w3c.dom.Text) curnd.getFirstChild()).getData();
							
							if 
(((org.w3c.dom.Element) curnd).getTagName().equals("id"))
								idstr =  
((org.w3c.dom.Text) curnd.getFirstChild()).getData();
															
							if 
(((org.w3c.dom.Element) curnd).getTagName().equals("gao"))
								gao =  
((org.w3c.dom.Text) curnd.getFirstChild()).getData();
	
							if 
(((org.w3c.dom.Element) curnd).getTagName().equals("sql")) {
								Node 
sqlnode = curnd.getFirstChild();
								if 
(sqlnode != null) SQL = ((org.w3c.dom.CDATASection) 
curnd.getFirstChild()).getData();
							}
								
							if 
(((org.w3c.dom.Element) curnd).getTagName().equals("comment")) {
								Node 
commentnode = curnd.getFirstChild();
								if 
(commentnode != null) comment =  ((org.w3c.dom.Text) 
curnd.getFirstChild()).getData();
							}
							/**
							 *  file transfer 
kai unix commands
							 */
							if 
(((org.w3c.dom.Element) curnd).getTagName().equals("filename")) {
								Node 
filenamenode = curnd.getFirstChild();
								if 
(filenamenode != null) filename =  ((org.w3c.dom.Text) 
curnd.getFirstChild()).getData();
							}
							
							if 
(((org.w3c.dom.Element) curnd).getTagName().equals("filecontent")) {
								Node 
filecontentnode = curnd.getFirstChild();
								if 
(filecontentnode != null) file_base64 = ((org.w3c.dom.CDATASection) 
curnd.getFirstChild()).getData();
							}
							
							if 
(((org.w3c.dom.Element) curnd).getTagName().equals("unixcommand")) {
								Node 
unixcommandnode = curnd.getFirstChild();
								if 
(unixcommandnode != null) unixcommand = ((org.w3c.dom.CDATASection) 
curnd.getFirstChild()).getData();
							}

							
						}	
					}
										
					if (xidstr == null) throw new 
Exception("xid is null");
					int xid = 
Integer.parseInt(xidstr);
					if (vslidstr == null) throw new 
Exception("vslid is null");
					int vslid = 
Integer.parseInt(vslidstr);
					
					if (gao == null) throw new 
Exception("gao is null");
					if (vslid != thisvesselvslid && 
!gao.equals("FLTRAN") && !gao.equals("UNXCOM")) throw new Exception("This 
Transaction is for Another Vessel. Contact IT DEPT.");
					
					logger.info("Processing file 
"+xmlfilename);
					
					if (gao.equals("ACKNOW")) {
						PreparedStatement st = 
con.prepareStatement("update xadmin set state='ARC' where xid=?");
						st.setInt(1,xid);
						int rc = 
st.executeUpdate();
						if (rc != 1) throw new 
Exception("Error updating xadmin to ARC.");
						st.close();
					}
					else if (gao.equals("CANCEL")) {
						PreparedStatement st = 
con.prepareStatement("update xadmin set state='CAN',comment=? where 
xid=?");
						st.setString(1,comment);
						st.setInt(2,xid);
						int rc = 
st.executeUpdate();
						if (rc != 1) throw new 
Exception("Error updating xadmin to ARC.");
						st.close();
					}
					else if (gao.equals("REPLIC")) {
						logger.info("IN REPLIC for 
file "+xmlfilename);
						// EDW MAS ENDIAFEREI MONO 
TO SQL.
						java.sql.Statement st2 = 
con.createStatement();
						logger.info("IN REPLIC 
after createStatement for file "+xmlfilename);
						if (SQL != null) 
st2.execute(SQL);
						logger.info("IN REPLIC 
after execute for file "+xmlfilename);
						st2.close();
					}
					else if (gao.equals("FLTRAN")) {
						if (filename == null) 
throw new Exception("File name in file transfer cannot be null. Contact IT 
DEPT.");
						if (file_base64 == null) 
throw new Exception("File content in file transfer cannot be null. Contact 
IT DEPT.");
						byte[] filecontent = 
Base64.decode(file_base64.toCharArray());
						File outfile = new 
File(FILEDIR+"/"+filename);
						FileOutputStream fout = 
new FileOutputStream(outfile);
						fout.write(filecontent);
						fout.close();	
					}
					else if (gao.equals("UNXCOM")) {
						if (unixcommand == null) 
throw new Exception("Command in unix command cannot be null. Contact IT 
DEPT.");
						// PROSOXH SE 
size(unixcommands)/1024 > (getconf ARG_MAX)
						Process proc = 
Runtime.getRuntime().exec(new String[] {"sh","-c",unixcommand});
						
						FileOutputStream fout = 
new FileOutputStream(new 
File(XMLTODIR+"/"+xmlfile.getName().replaceAll(".xml","")+".out"));
						
						PrintStream out = new 
PrintStream(fout);
						
						java.io.InputStream 
procout = proc.getInputStream();
						
						
out.println("==============");
						out.println("Output (if 
any)");
						
out.println("==============");
						
						int c;
						while ((c=procout.read()) 
>=0) {
							out.write(c);
							
//System.out.write(c);
							
//System.out.flush();
						}
						
						java.io.InputStream 
procerr = proc.getErrorStream();
						
						
out.println("==============");
						out.println("Error (if 
any)");
						
out.println("===============");
						
						while ((c=procerr.read()) 
>=0) {
							out.write(c);
							
//System.out.write(c);
							
//System.out.flush();
						}
						
					}
					else throw new Exception("GAO 
"+gao+" not supported. CONTACT IT DEPT.");
					
					con.commit();
					boolean isdeled = 
xmlfile.delete();
					if (!isdeled) throw new 
Exception("Error deleting file. Xaction Aborted.");
					System.out.println("OK with file 
"+xmlfile.getName());
					
					lastdone=xmlfile.getName();
					 
				}
				catch (Exception xmle) {
					System.out.println("Error : 
"+xmle.getMessage()+": with file "+xmlfile.getName());
					logger.error("Error : 
"+xmle.getMessage()+": with file "+xmlfile.getName());
					//xmle.printStackTrace();
					con.rollback();
					
					
xerrors[numxerrors++]="--------------------------"+"Error with 
"+xmlfile.getName()+"--------------------------\n"+
					
xmle.getMessage()+"\n"+"---------------------------------------------------------------------";
					xmlfile.delete();
				}
			} // end for
			if (lastdone != null) {
				FileOutputStream fout = new 
FileOutputStream(new File(XMLTODIR+"/lastdone.mon"));
				PrintStream out = new PrintStream(fout);						
				out.println("Last XML was "+lastdone);
			}
			if (numxerrors!=0) {
				FileOutputStream fout = new 
FileOutputStream(new File(XMLTODIR+"/xerror.mon"));
				PrintStream out = new PrintStream(fout);						
				int run;
				for (run=0;run<numxerrors;run++) {
					out.println(xerrors[run]);
				}
				
			}
		}
		catch (Exception pce) {
			System.out.println("A General XML Error Occured: 
"+pce.getMessage());
			logger.error("A General XML Error Occured: 
"+pce.getMessage());
		}
		finally {
			if (con != null) try {con.close();} 
catch(Exception fe){}
		}
	}
	
}

class DirComparator implements Comparator {
	public DirComparator () {
	}
	
	public int compare(Object obj, Object obj1) {
        File f = (File) obj;
        File f1 = (File) obj1;
        String str = f.getName();
        String str1 = f1.getName();
        int i,i1;
        //System.out.println("str="+str);
        //System.out.println("str1="+str1);
        if (str.charAt(0) < str1.charAt(0)) return -1;
        else if (str.charAt(0) > str1.charAt(0)) return 1;
        try {
        	i = Integer.parseInt(str.substring(1,str.indexOf('.')));
        	//System.out.println("i="+i);
        }
        catch (Exception e) {
        	return 1;
        }
        try {
        	i1 = 
Integer.parseInt(str1.substring(1,str1.indexOf('.')));
        	//System.out.println("i1="+i1);
        }
        catch (Exception e) {
        	return -1;
        }
        if (i < i1 ) return -1;
        else if (i==i1) return 0;
        else return 1;
    }
    
    public boolean equals(Object obj) {
        return false;
    }
}
 
------------------------------------------------------------


Now the really wierd problem.

When the /usr/local/jboss-3.0.3/server/default/readoff is run from
the shell it has never stalled, behaved strange or failed.

Also when the /usr/local/sbin/uucico_command is run from the shell
it has never failed, or *caused* 
/usr/local/jboss-3.0.3/server/default/readoff to stall.

BUT, when i run /usr/local/sbin/uucico_command from the JSP,
then i experience a stall of the 
/usr/local/jboss-3.0.3/server/default/readoff script,
and only if there are many (where many varies from 
run to run, but generally more than lets say 10) xml files
to be processed in /usr/local/var/XML_FROM_OFFICE/.

The java program in readoff processes *some* XML files, and then stalls
for no apparent reason.

The solution was to replace 
the last 2 lines

/usr/local/jboss-3.0.3/server/default/readoff
/etc/cron.daily/compstatus &

with

echo /usr/local/bin/afteruucico | at now

where /usr/local/bin/afteruucico is just

#!/bin/sh
/usr/local/jboss-3.0.3/server/default/readoff
/etc/cron.daily/compstatus

In other words,
i have a JSP calling a script, which calls another script which
executes a java program.

The java programm does some processing and then sleeps for
no reason without finishing, without spitting out any Exceptions.

When i magically "jump out" of the initial java environment (jboss/JSP),
with the *at* command, then the readoff script and its ReadXmlFromOffice
java programm all work fine.

Any thoughts would be great.

I checked ulimit,etc, with no apparent clue.

This problem kinda haunted me last night, and i just
wanna kill it, instead of pretending i found a workaround.

The only "excuse" i can think of, is that it runs in Linux,
which i am not extremely familiar with.

If you need more feedback, or if any ambitious volunteer
wants to hunt this down, i am happy to help.

P.S.

The jboss dir in FreeBSD, is a morningly tbz from the linux
machine, so no tricks there.

-- 
-Achilleus



More information about the freebsd-java mailing list