Visitor :

Jun 9, 2009

해마 사진

 
Posted by Picasa

해마 사진

 
Posted by Picasa

May 4, 2009

Welcom The close() method of ClassLoader in JDK7

The close() method of ClassLoader in JDK7 is revolutionary stuff in JDK history, because ClassLoader have no function of close, module programming is very difficult to manage java object.
Therefore, there is no need to kill application to close classloader (Known for App kill).
I am very expecting JDK7 release.

URL url = new URL("file:foo.jar");
URLClassLoader loader = new URLClassLoader (new URL[] {url});
Class cl = Class.forName ("Foo", true, loader);
Runnable foo = (Runnable) cl.newInstance();
foo.run();
loader.close ();
// foo.jar gets updated somehow
loader = new URLClassLoader (new URL[] {url});
cl = Class.forName ("Foo", true, loader);
foo = (Runnable) cl.newInstance();
// run the new implementation of Foo
foo.run();

java sources for ojdbc (odjbc14-10.0.2.zip, ojdbc14-9.2.zip) decompied by jad

Those sources are decompiled by jad decompiler. I can't source for ojdbc14.jar anywhere, so that I put sources to my blog.

Prior to see the source, you have to see the readme of ojdbc14.jar in oracle web site to understand jdbc specification and bug fixes.

odjbc14-10.0.2.zip
http://knight76.tistory.com/attachment/cfile4.uf@14432C1D49E8206A909F92.zip

ojdbc14-9.2.zip
http://knight76.tistory.com/attachment/cfile22.uf@1144E51D49E82072C532AE.zip

Notepad with class java file (Jad Usage Tip)

0. Donwload jad
Download and install jad app, and put intalled jad in the path on your computer to execute on any commnad window.

1. Create jad.bat file
Crate jad.bat file and put your jad.bat into path on your computer.
@echo off
jad -o -d. %*
notepad %~n1.jad
del %~n1.jad

2. Link class extension file to jad.bat that you made.

3. Unzip jar
Unzip a certain jar to your computer. and run "cmd" on execute window menu.
And, go to the unzipped jar directory and execute below command to decompile your class files.

4. Click class file
After clicking your class file linked jad.bat, you may see this window.



Strength of this bat file
- You may not decompile every class file.
- You may not know every option of jad.
- Autiomatically remove the jad file when you close the decompiled java source note-pad-window.

addShutdownHook() of Runtime class



The addShutdownHook method of Runtime class is vervy useful to invoke something before a application exit. In addition coding in JUnit, very useful.
If you create and use some Threads, you can not invoke directly, this method usage is very helpful.


public class Monitor {
private static Log log = LogFactory.getLog(HTMLPageFetcher.class);

private static NotificationCollector notiCollector;
static {
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
close();
}
});
}
public static void close() {
log.info("close application.!!");
notiCollector.stop();
}


...

JavaFx Programming Reference

To programe java fx, refer below sites.



http://jfx.wikia.com/wiki/Code_Examples

http://java.sun.com/javafx/1/tutorials/ui/events/index.html

http://java.sun.com/javafx/1/tutorials/core/modifiers/index.html

http://javaora.tistory.com/tag/Learning%20the%20JavaFX%20Script%20Programming%20Language%20-%20Lesson%2011:%20Access%20Modifiers


http://java.sun.com/javafx/script/reference/scenegraph_demo/


http://www.javafx.com/samples/SimpleVideoPlayer/index.html


http://www.vogella.de/articles/JavaFX/article.html

Setting property in svn repository

Setting property is possible in svn server like below.

${HOME}/.subversion/config

[auto-props]

*.c = svn:eol-style=native

*.rng = svn:eol-style=native
..

Besides, in source, there is a way to set property (using svn client).


Right-click on a certain sh file, see menu below.
Team>>Show Properties>>Add Property

namevalue
svn:eol-stylenative
svn:executable*

JavaOne 2008

To study or listen the sessions of java one 2008, visit "http://developers.sun.com/learning/javaoneonline/j1online.jsp?track=coolstuff&yr=2008" web site.

You may be requested to login by sun.com. If you are sdn's member, you can access and enjoy it.

Also, you can visit this site. "http://java.sun.com/javaone/sf/sessions/general/index.jsp"

No SqlHandler log4j:ERROR problem

If you meet this log, you shoud take carefully a look at your log4j sqlhandler option, modify it.

log4j:ERROR JDBCAppender::configure(), No SqlHandler or sql, table, procedure option given !
log4j:ERROR JDBCAppender::append(), Not ready to append !

Thinking about saving error logs in web application based on web-work(or struts2)




You may  meet java errors every day if you are a java developer. Especially if a lot of or critical error logs in web application get to meet, you will be panic. So, there is need to develop error log DB and error-collectiong log module.


I will introduce my idea about developing error log DB.


According to your company policies, methods are applied for error logging system.






Architecture of error logging system consists of


1) Error DB can be stored


2) Admin web application can show errors on error db


3) Java classes and log4j configuration to transfer logs to DB.














<Error DB>


In web application, there are many properties to save such as belows.



project-name, server-name, loginid(userid), access date-time, log level, url, form, cookie, referer, execpetionmessage 





You can specify those more concisely. For example, exceptionmessage is divided to head and body according to java.lang.Exception.






I made error log db, table. Specially I made indexing in mysql. And in mysql DB, I made a batch script to rotate log db.








#!/bin/sh


TODAY=`date -d "-1 day" +%Y%m%d`


TO_DELETE_DAY=`date -d "-30 day" +%Y%m%d`


mysql  -u userid -p password mysql db-schema-name << !





# rotate log table 

alter table lgt_logdata rename logdata_${TODAY}; 





drop table if  exists logdata_${TO_DELETE_DAY};



create table logdata (

  logid int(11) NOT NULL auto_increment,


  projectname varchar(30) ,


  servername varchar(20) ,

....

  KEY lgt_logdata_ix1 (projectname,loglevel)

)















<Error-collectiong>


Error-collecting log module is implemented by log4j in simple. You can change the properites to save several log-level like error, warning. So, you can make warning log DB as well.










Let us suppose to save the errors to mysql. 




<Log4j.xml>



<appender name="ERROR-OUT" class="org.apache.log4j.jdbcplus.JDBCAppender">


<param name="Threshold" value="ERROR"/>


<param name="dbclass" value="com.mysql.jdbc.Driver"/>


<param name="url" value="jdbc:mysql://in.log.google.com"/>


<param name="username" value="error"/> 


<param name="password" value="nobodynobodywantyou"/>


<param name="sqlhandler" value="com.google.kr.MySQLHandler"/>


<param name="Buffer" value="1"/>


</appender>







<logger name="com" additivity="false">


<level value="DEBUG"/>


            <appender-ref ref="ERROR-OUT"/>


</logger>











You have to save error-loging policy in your web application.


I think every logs have to store error log DB, so, I have policy to save all tomcat logs. Because every any kinds of errors can affect the performance of web application.






I have three policies to store the DB. I think those are very important to design error DB.



1. Using Interceptor in web work or struts2, web data such as url, form have to save MDC. (MDC is api of log4j framework)



Before web application meet java exceptions, the interceptor save the web information and after invoking action it checks the result of invoking action are failed(or throw exception). If the action throws the exception, those errors will sqlhandler (ERROR-OUT)



MDC.put("projectname", projectname);

MDC.put("cookie",cookie);



2. Use Servlet in web.xml. 



You may extend and implements extends javax.servlet.http.HttpServlet class. Through the implemented class to handle error, you can store errors.




<servlet>


<servlet-name>errorHandler</servlet-name>


<servlet-class>com.google.kr.ErrorHandlerServlet</servlet-class>


</servlet>



3. Adding some fuctions to sqlhandler.

Most of error cases occur in calling action from users, but in some case internal web application invoker can make errors. For example batch or cache or scheduling based on Thread could make fault, but belows method can not include saving error logs.

And then, when you implement sqlhandler, you can add those.

You've got to adds some exceptional case on the sqlhandler.





import org.apache.log4j.jdbcplus.JDBCSqlHandler;


public class ErrorHandler implements JDBCSqlHandler {

...

}









<web applicatiion for error log>


You can show the errors stored in DB in simple.




Dynamically creating table in ibatis

Ibatis sqlclient provides "statement" tag to create table in dynamic. "Statement" tag are used in various way.

"http://www.ibatis.com/dtd/sql-map-2.dtd">

CREATE TABLE exam_$tableid$ (
logid int(11) NOT NULL auto_increment primary key,
projectname varchar(30),
disttype varchar(7),
contents longtext,
isSuccess char(1)
);

DROP TABLE IF EXISTS exam_$tableid$;


Map map = new HashMap();
map.put("tableid", "test");
sqlMap.queryForObject("com.google.com.test.createJavabasic", map);

checkng(or seeing) oracle jdbc driver(ojdbc.jar) version


Oracle provides the various version of oracle jdbc driver to java developers. But, released jdbc drivers' name are always ojdbc14.jar.

If you try to run java to see manifest file, you may be failed.
$ java -jar ojdbc14.jar
Failed to load Main-Class manifest attribute from
ojdbc14.jar

So, to know those version. there are 2 measures to check it.

1) Unzip and see MANIFEST.MF in ojdbc14.jar

Manifest-Version: 1.0
Specification-Title: "Oracle JDBC driver classes for use with JDK1.4"
Specification-Version: "Oracle JDBC Driver version - 9.0.2.0.0"
Specification-Vendor: "Oracle Corporation" .
Implementation-Title: "ojdbc14.jar"
Implementation-Version: "Oracle JDBC Driver version - 9.0.2.0.0"
Implementation-Vendor: "Oracle Corporation"
Implementation-Time: "Thu Apr 25 23:14:02 2002"


2) Java coding

DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver());

Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@123.345.321.121:231:SID","ID","PASS");

DatabaseMetaData meta = conn.getMetaData ();

System.out.println("Database Product Name : " + meta.getDatabaseProductName());

System.out.println("Database Product Version : " + meta.getDatabaseProductVersion());

System.out.println("JDBC Driver Name :" + meta.getDriverName());

System.out.println("JDBC Driver Version : " + meta.getDriverVersion());


3) From the ojdbc14.jar in oracle 11g, you can use below method to verify the version.
java jar ojdbc6.jar


In result, there is need to know the version of ojdbc14.jar, you have to rename the name of jar like ojdbc14-10.0.2.jar. It is easy to understand

Deadlock using commons-pool 1.4 and commons-dbcp 1.2.2


I have found critical situation about deadlock.
Http thread was full. Most of apache workers was in sending reply.

Busy http thread grew up until suspending its function. When the apache could not respond any request, it removed from L4 (it have L7 function) because of no response.

Every web servers (600 ea) always does not happen, but some web application servers(10ea) fell into the trouble per 2-3 days.

Apache Status :

Server
Application Version : 2.0.59, Mod_jk 1.2.18, Tomcat 5.5.17, JDK 6.0 update 6 Linux Kernel 2.6.9-67.0.22.ELsmp #1 SMP,
OS : CentOS
gcc version
: gcc version 3.4.6 20060404 (Red Hat 3.4.6-9)
glibc
버전 glibc-2.3.4-2.39


I was wonderying why happened.

1) I captured apache server status.
Apache Server Status

Server uptime: 4 days 20 hours 41 minutes 2 seconds
Total accesses: 43491259 - Total Traffic: 10.8 GB
CPU Usage: u33.75 s6.42 cu0 cs0 - .00956% CPU load
104 requests/sec - 27.0 kB/second - 266 B/request
109 requests currently being processed, 4 idle workers
WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW_WWW.WWWWWWWWWWWW CWWWWWWWWWWWWWWWWWWWWW_WWWWCWWWWWWWWWRWWWWWW__CCR.C............. ................................................................ ................................................................ 

Scoreboard Key:
"_" Waiting for Connection, "S" Starting up, "R" Reading Request,
"W" Sending Reply, "K" Keepalive (read), "D" DNS Lookup,
"C" Closing connection, "L" Logging, "G" Gracefully finishing,
"I" Idle cleanup of worker, "." Open slot with no current process


2) lsof
The number of CLOSE_WAIT status tcp sockets was 140. The number was close equal to the number of apache workers was in sending reply.
java 16907 www 52u IPv6 1014864218 TCP localhost.localdomain:8009->localhost.localdomain:35205 (CLOSE_WAIT)
java 16907 www 53u IPv6 1014869414 TCP localhost.localdomain:8009->localhost.localdomain:35414 (CLOSE_WAIT)
java 16907 www 54u IPv6 1014861929 TCP localhost.localdomain:8009->localhost.localdomain:35171 (CLOSE_WAIT)
java 16907 www 55u IPv6 1014863474 TCP localhost.localdomain:8009->localhost.localdomain:35200 (CLOSE_WAIT)
java 16907 www 56u IPv6 1014781598 TCP localhost.localdomain:8009->localhost.localdomain:60006 (CLOSE_WAIT)
java 16907 www 57u IPv6 1014860437 TCP localhost.localdomain:8009->localhost.localdomain:35091 (CLOSE_WAIT)
java 16907 www 58u IPv6 1014859292 TCP localhost.localdomain:8009->localhost.localdomain:35062 (CLOSE_WAIT)
java 16907 www 59u IPv6 1014853698 TCP localhost.localdomain:8009->localhost.localdomain:34860 (CLOSE_WAIT)
java 16907 www 60u IPv6 1014864865 TCP localhost.localdomain:8009->localhost.localdomain:35353 (CLOSE_WAIT)
java 16907 www 61u IPv6 1014868132 TCP localhost.localdomain:8009->localhost.localdomain:35356 (CLOSE_WAIT)
java 16907 www 62u IPv6 1014864283 TCP localhost.localdomain:8009->localhost.localdomain:35222 (CLOSE_WAIT)
..


3) Thread dump
I found dead lock situation in dumped thread in java log.

i. java.lang.Thread.State: BLOCKED (on object monitor)
at org.apache.commons.pool.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:916)
- waiting to lock <0x8fedb478> (a org.apache.commons.pool.impl.GenericObjectPool)

1. "TP-Processor2" daemon prio=10 tid=0x0821f400 nid=0x4286 waiting for monitor entry [0x88ed3000..0x88ed50a0]

2. "TP-Processor2" daemon prio=10 tid=0x0821f400 nid=0x4286 waiting for monitor entry [0x88ed3000..0x88ed50a0]



Found one Java-level deadlock:

=============================

"TP-Processor200":

waiting to lock monitor 0x080dfd24 (object 0x8fedb478, a org.apache.commons.pool.impl.GenericObjectPool),

which is held by "Timer-0"

"Timer-0":

waiting to lock monitor 0x080dfcc0 (object 0x8fee6f70, a org.apache.commons.dbcp.PoolableConnection),

which is held by "TP-Processor28"

"TP-Processor28":

waiting to lock monitor 0x080dfd24 (object 0x8fedb478, a org.apache.commons.pool.impl.GenericObjectPool),

which is held by "Timer-0"

"TP-Processor200": (Select query) return queryForList("xxx.selectXXXCount",param);

"TP-Processor28": (Select query)

queryForObject("selectmusic",params);



This deadlock reported in "Dead lock using the evictor"(https://issues.apache.org/jira/browse/DBCP-270)
In this jira, there are patch on that.


Commons Dbcp

Dead lock using the evictor

Created: 26/Jun/08 10:18 AM Updated: 27/Jun/08 03:41 AM
Component/s: None Affects Version/s: 1.2.2 Fix Version/s: 1.3

Patch attached, dead lock reported. the abandonedtrace does synchronized(this) when it really only needs to do synchronized(this.trace)

Dead lock reported when using the evictor

=============================
"Timer-3":
waiting to lock monitor 0x0000000053b40548 (object 0x00002aaabf3210f0, a
org.apache.tomcat.dbcp.dbcp.PoolableConnection),
which is held by "TP-Processor27"
"TP-Processor27":
waiting to lock monitor 0x0000000053b404d0 (object 0x00002aaab9fa8b08, a
org.apache.tomcat.dbcp.pool.impl.GenericObjectPool),
which is held by "Timer-3"

Java stack information for the threads listed above:

patch source

Index: src/java/org/apache/commons/dbcp/AbandonedTrace.java
===================================================================
--- src/java/org/apache/commons/dbcp/AbandonedTrace.java (revision 671937)
+++ src/java/org/apache/commons/dbcp/AbandonedTrace.java (working copy)
@@ -172,7 +172,7 @@
* @param trace AbandonedTrace object to add
*/
protected void addTrace(AbandonedTrace trace) {
- synchronized (this) {
+ synchronized (this.trace) {
this.trace.add(trace);
}
setLastUsed();
@@ -182,8 +182,8 @@
* Clear the list of objects being traced by this
* object.
*/
- protected synchronized void clearTrace() {
- if (this.trace != null) {
+ protected void clearTrace() {
+ synchronized(this.trace) {
this.trace.clear();
}
}
@@ -194,8 +194,10 @@
* @return List of objects
*/
protected List getTrace() {
- return trace;
+ synchronized (this.trace) {
+ return new ArrayList(trace);
}
+ }
/**
* If logAbandoned=true, print a stack trace of the code that
@@ -206,7 +208,7 @@
System.out.println(format.format(new Date(createdTime)));
createdBy.printStackTrace(System.out);
}
- synchronized(this) {
+ synchronized(this.trace) {
Iterator it = this.trace.iterator();
while (it.hasNext()) {
AbandonedTrace at = (AbandonedTrace)it.next();
@@ -220,8 +222,8 @@
*
* @param trace AbandonedTrace object to remvoe
*/
- protected synchronized void removeTrace(AbandonedTrace trace) {
- if (this.trace != null) {
+ protected void removeTrace(AbandonedTrace trace) {
+ synchronized(this.trace) {
this.trace.remove(trace);
}
}


Because Commons-dbcp 1.3 is not year released in formal. (now : 4. 2009) and I do not know what willing happen if the snapshop patch version, I used patched commons-dbcp 1.2.2 library.

After patching that version, there is no trouble or problem like that.