Tag Archive for 'java'

02
Mar

Create new log file with log4j FileAppender when using Quartz Scheduler

Recently came across a problem whereby the quartz scheduler could not recreate a new log4j FileAppender log when the job is call again.

Googled a bit and found out a couple of solutions such as using a custom log4j classes, which create a seperate thread when being called in quartz scheduler.

I find that it is too complex and had try several ways to recreate the log file inside Quartz.

And finally came up with this one line of code.

Before I begin, let’s take a look at my log4j config.
This log4j config will create a simple console system out logger and also a FileAppender logger.
For my case, I want to re-create a new log file whenever this job is run.

log4j.rootLogger=INFO, stdout
log4j.additivity.stdout=false
log4j.additivity.joblog=false
log4j.appender.ROOT.layout.ConversionPattern=[%d] %t %c %-5p - %m%n

#console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p - %m%n

#per job execution log
log4j.logger.com.yourcompany=INFO, joblog
log4j.appender.joblog=org.apache.log4j.FileAppender
log4j.appender.joblog.layout=org.apache.log4j.PatternLayout
log4j.appender.joblog.File=../jobtemp.log
log4j.appender.joblog.Append=false
log4j.appender.joblog.layout.ConversionPattern=%d %p - %m%n

and here’s the schedule job

public class ScheduleJob implements StatefulJob {

	private static Logger logger = Logger.getLogger(ScheduleJob.class);

	public void startJob() throws SchedulerException{
		Scheduler scheduler =
			StdSchedulerFactory.getDefaultScheduler();
		scheduler.start();
	}

	public void execute(JobExecutionContext context)
		throws JobExecutionException {

		//reset log4j config for FileAppender
		PropertyConfigurator.configure("../conf/log4j.properties");

		RunJobImmediately newJob  = new RunJobImmediately();
		newJob.performJob();

	}

	public static void main(String args[]){
    	try{
    		ScheduleJob scheduleJob = new ScheduleJob();
    		scheduleJob.startJob();

		}catch(Exception e){
			logger.error(e);
		}
	}
}

and finally the one line of code to recreate the log file.
Apparently, this line will somehow ‘reload’ the log4j  utitlies, which thus recreate the FileAppender logger to re-create the log file.

PropertyConfigurator.configure("../conf/log4j.properties");

Feel free to let me know if there is any performance issues or it is not working at your side.

VN:F [1.9.3_1094]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.3_1094]
Rating: 0 (from 0 votes)
20
Feb

Printing out active threads in java

Here’s the code snippets to display the number of running threads in your java program.

It’s good to use it to track your resources.

int activeCount = Thread.activeCount();
System.out.println("total active = " + activeCount);
Thread[] threads = new Thread[activeCount];
Thread.enumerate(threads);

System.out.println("before");
for (int j=0; j<threads.length; j++) {
 System.out.println(threads[j].toString());
}

//run your classes
Benny benny = new Benny();

activeCount = Thread.activeCount();
System.out.println("total active = " + activeCount);

threads = new Thread[activeCount];
System.out.println("after");
Thread.enumerate(threads);
for (int i=0; i<threads.length; i++) {
 System.out.println(threads[i].toString());
}
VN:F [1.9.3_1094]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.3_1094]
Rating: 0 (from 0 votes)
17
Jan

Making a Struts Radio selected

By default, struts’s html radio tag do not allow you to make a radio to be selected as they have no “selected” property or something

<html:radio property="gender" value="F" />Male
<html:radio property="gender" value="M"/>Female

To make a radio button to be selected on load, set the value that you want to be selected in your action form reset method


public void reset(ActionMapping mapping, HttpServletRequest request) {
            super.reset(mapping, request);
            gender= "F";
      }
VN:F [1.9.3_1094]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.3_1094]
Rating: 0 (from 0 votes)



Search Website

Sponsored Ads

Sponsored Links

Sponsors

Sponsors

About Me

Me

Hello, Im Benny Chong. This blog is a proof that I have too much spare time to burn. 

I will be writing topics mainly on web design, application development, photography and other rubbish. Cheers