Archive for the 'Programming' Category

03
Jul

Premature end of script headers in perl script

‘Premature end of scrip headers’ error message refers to the script being stopped for whatever reason before it actually return any output to the browser.

The first time to check is to ensure the below codes are output first

print "Content-type: text/html\n\n";

And then to debug the error, simply add the below line(preferably after the above code).

use CGI::Carp qw(fatalsToBrowser);
VN:F [1.9.3_1094]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.3_1094]
Rating: 0 (from 0 votes)
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)
23
Jan

Using single SQL query to retrieve counts of two subqueries

Recently received a question from someone asking for a single SQL statement to retrieve the counts for two subqueries (or something like that).

I had altered the question and and my answer below.

A company has several customers and courses. Each customer may be signing up on several courses.
Write an SQL query to display the number of women and the number of men signing on each course.

And my simple database design

CREATE TABLE `CUSTOMER` (
`CUSTOMER_ID` INT NULL AUTO_INCREMENT PRIMARY KEY,
`NAME` VARCHAR( 100 ) NULL ,
`GENDER` CHAR( 1 ) NULL ,
`EMAIL` VARCHAR( 150 ) NULL
) ENGINE = MYISAM ;

CREATE TABLE `COURSE` (
`COURSE_ID` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
`NAME` VARCHAR( 50 ) NOT NULL ,
`DESCRIPTION` VARCHAR( 255 ) NOT NULL ,
`START_DATE` DATE NOT NULL ,
`END_DATE` DATE NOT NULL
PRIMARY KEY ( `COURSE_ID` )
) ENGINE = MYISAM ;

CREATE TABLE `CUSTOMER_COURSE` (
`CUSTOMER_ID` INT NULL ,
`COURSE_ID` INT NULL ,
`STATUS` VARCHAR( 5 ) NULL
) ENGINE = MYISAM ;

And the SQL to “display the number of women and the number of men signing on each course”

select
    sum(total_males) as total_males,
    sum(total_females) as total_females,
    COURSE_id
from
    (select COUNT(e.GENDER) as total_males , 0 as total_females, ep.COURSE_ID
        from CUSTOMER e, CUSTOMER_COURSE ep
        where ep.CUSTOMER_ID = e.CUSTOMER_ID and e.GENDER = 'M'
        group by ep.COURSE_ID
    union all
    select 0 as total_males, COUNT(e.GENDER) as total_females ,ep.COURSE_ID
        from CUSTOMER e, CUSTOMER_COURSE ep
        where ep.CUSTOMER_ID = e.CUSTOMER_ID and e.GENDER = 'F'
    group by ep.COURSE_ID
	) t1
group by COURSE_id

Explanation:

  • the two subqueries generate 3 columns: total males, total females, course ID
  • one of the columns in the two subqueries is 0
  • the two subqueries would then UNION ALL to populate all results for the parent SQL
  • the parent SQL would then do a SUM/COUNT for the subqueries by grouping the course ID.
  • the ‘t1′ is just a empty table name thrown back to the parent for reference
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