...
Code Block |
---|
|
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();
\}
\}
|
The content of the log file after execution of the Java class is shown below:
Code Block |
---|
2013-08-14 13:58:27.419 [info] SCHEDULER-918 state=starting (at=2013-08-14 13:58:27.393)
2013-08-14 13:58:28.000 [info] starting MultiThreadLogging
2013-08-14 13:58:28.075 [info] INFO [main] (SOSMsg.java:140) - LOG_I_0020: JobSchedulerLog4JAppender ist jetzt als log4j-appender konfiguriert
2013-08-14 13:58:28.080 [info] INFO [main] (JobSchedulerJobAdapter.java:116) - 1.3.12.3142 (2013-05-22T16:17:36.781+0200) Copyright 2003-2013 SOS GmbH Berlin
2013-08-14 13:58:28.109 [info] INFO [main] (MultiThreadLogging.java:34) - in spooler_process
2013-08-14 13:58:28.999 [info] 36 [main] INFO root - in spooler_process
2013-08-14 13:58:28.999 [info] test from Thread in MultiThreadLogging
2013-08-14 13:58:28.999 [info] test from Thread2 in MultiThreadLogging
2013-08-14 13:58:28.999 [info] 42 [Thread-0] INFO root - test from Thread
2013-08-14 13:58:28.999 [info] 42 [Thread-1] INFO root - test from Thread2
2013-08-14 13:58:30.115 [info] INFO [main] (MultiThreadLogging.java:71) - exit spooler_process
2013-08-14 13:58:30.147 [info] Job MultiThreadedLogging terminated.
2013-08-14 13:58:30.189 [info] 2044 [main] INFO root - exit spooler_process
|