You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

The spooler logging facility is not able to work in different threads.

To use logging from different threads it is recommended to use the log4j framework.

An example for a Java class, which is using multiple threads and log4j:

import org.apache.log4j.BasicConfigurator;

import sos.scheduler.job.JobSchedulerJobAdapter;

public class MultiThreadLogging extends JobSchedulerJobAdapter \{
	private final String		conClassName	= this.getClass().getSimpleName();
	@SuppressWarnings("unused")
	private static final String	conSVNVersion	= "$Id$";

	@Override
	public boolean spooler_open() throws Exception \{
		super.spooler_open();
		spooler_log.info("starting " + conClassName);
		return true;
	\}

	@Override
	public boolean spooler_process() throws Exception \{
		super.spooler_process();
		/**
		 * Overwrite the log4j appender for the spooler_log, because all log-output from another thread
		 * than thread-0 is not logged actually.
		 */
		BasicConfigurator.configure();  // log4j output to stdout from this thread

		logger.info("in spooler_process");

		final Thread objThread = new Thread() \{

			@Override
			public void run() \{
				try \{
					System.out.println("test from Thread in " + conClassName);
					logger.info("test from Thread");
					sleep(2000);
				\}
				catch (Exception e) \{
					e.printStackTrace(System.err);
				\}
			\}
		\};

		final Thread objThread2 = new Thread() \{

			@Override
			public void run() \{
				try \{
					System.out.println("test from Thread2 in " + conClassName);
					logger.info("test from Thread2");
					sleep(2000);
				\}
				catch (Exception e) \{
					e.printStackTrace(System.err);
				\}
			\}
		\};

		objThread.start();
		objThread2.start();

		objThread.join();
		objThread2.join();
		logger.info("exit spooler_process");

		return signalSuccess();
	\}
\}
  • No labels