Friday, March 10, 2006

Amazon Tools - java gui

I found out a java tools that I developed months ago, when I clear up my file sources.

The java swing is not so good at develop windows like forms compared to C#.

I designed the appearance according to JBuilder.

 

The tabpanels is a good container. I want to add a close button onto the tab, but it hasn’t come true by now. :(.

Maybe I’ll refine it later into my new projects.

Posted by vankevin in 09:48:56 | Permalink | No Comments »

Wednesday, December 14, 2005

How to use multi-datasource in DBCP without JNDI

In order to optimize our mq’s consumer, we import the connection pool and concurrency into our consumer. I’ll describe some problems we met during the development.

Please download the commons.dbcp,pool and connection jars from http://jakata.apache.org first of all.

The consumer.java is used to create connection pools and registed them to the DriverManager.

The DBO.java is a brief capsulated DB operator.

consumer.java
————————————
public static void main(String[] args){
Class.forName(“com.mysql.jdbc.Driver”);
smarterConsumer.setupDriver(“T1″);
smarterConsumer.setupDriver(“T2″);
try{
DBO dboT1 = new DBO(“T1″);
DBO dboT2 = new DBO(“T2″);
dboT1.execUpdate(“create table Test1(ID int(4))”);
dboT2.execUpdate(“create table Test2(ID int(5))”;
}catch(Exception e){
System.out.println(e.getMessage());
}

}

public void setupDriver(String tag) throws Exception {
String connectURI = “jdbc:mysql://localhost/”;
if(tag.equalsIgnoreCase(“etl”)){
connectURI = connectURI + “db1?user=user&password=***”;
}else{
connectURI = connectURI + “db2?user=user&password=***”;
}

ObjectPool connectionPool = new GenericObjectPool(null);
ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectURI,null);
PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,connectionPool,null,null,false,true);
Class.forName(“org.apache.commons.dbcp.PoolingDriver”);
PoolingDriver driver = (PoolingDriver) DriverManager.getDriver(“jdbc:apache:commons:dbcp:”);
driver.registerPool(“DS”+tag,connectionPool);

}

DBO.java
————————————
public DBO(String tag) {
try{
conn = DriverManager.getConnection(“jdbc:apache:commons:dbcp:DS”+tag);
}catch(Exception namingE){
System.out.println(namingE.getMessage());
}
}

public int execUpdate(String sql) throws SQLException {
int rs = 0;
Statement stmt = conn.createStatement();
try {
rs = stmt.executeUpdate(sql);
} catch (SQLException sqle) {
SQLErr:” + sqle.getMessage());
}finally{
stmt.close();
}
return rs;
}

Please pay attention the connections return from the DBO. Do not declare the connections static, otherwise they will be confused.

When you use the connection, remember to close the connection when you abande the instance.

The connection we used in the DBCP is a PoolableConnection, the close() method is to return the connection to the pool. The reallyClose() will release the connection.

I’d like to explain this sentence to you:

conn = DriverManager.getConnection(“jdbc:apache:commons:dbcp:DS”+tag);

The DriverManager is used to distribute the request to connectionFactories by sending the URI parameter to connectionFactory. If the connectionFactory can handle the URI, a ‘true’ will return.

The jdbc:apache:commons:dbcp:* will be handled by the DBCP, while the jdbc:mysql:* will be handled directly by mysql-connector-java-3.1.4-bin.jar

You may take the docs as a reference:

http://jakarta.apache.org/commons/dbcp/apidocs/index.html

http://jakarta.apache.org/commons/pool/apidocs/index.html

Posted by vankevin in 09:56:12 | Permalink | No Comments »

Wednesday, November 30, 2005

Concurrency design pattern of JDK1.5

Sun has released the jdk1.5 for about 1 year, but the articles of how to use the java.util.concurrency are still much less. I’ll look into it, and post it here later.

It’s much easier to use the concurrency in jdk1.5.

Here is a short example of it:

—————————————————————————————

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

—————————————————————————————

ThreadPoolExecutor threadPool = new ThreadPoolExecutor(
      corePoolSize, maximumPoolSize, keepAliveTime,
      TimeUnit.SECONDS, new ArrayBlockingQueue(5),
      new ThreadPoolExecutor.DiscardOldestPolicy());
 while (rs.next()) {
     Offer offer = new Offer(channelID, rs.getInt(“ProductID”),  merID);
     offer.setChannelInfo(channelInfo);
     offer.setSyncType(type);
     offer.initial();
     threadPool.execute(offer);
 }

—————————————————————————————

Posted by vankevin in 07:36:41 | Permalink | No Comments »

Thursday, November 24, 2005

Monitor Tomcat5.5.12 by JMX via RMI

Tomcat has provides a http adapter based monitor tool.

You can login the manager app to monitor your web applications that run in the web container.

If you want to monitor the scripts via RMI, the JMX of tomcat should be bind to a RMI skeleton.

Let’s take the tomcat5.5.12 as a example, following the follow two steps:

step 1. add the following into your startup.sh, anywhere before the exec sentence. 

export CATALINA_OPTS=”-Dcom.sun.menagement.jmxremote.port=8009″

step2. check the management.properties setting located at JDK1.5 _Home/jre/lib/management/. Make sure the

 com.sun.management.jmxremote.port=8009

has been open.

Then restart your tomcat, and let’s check the catalina’s MBean by JConsole:

expand the catalina root node and find your web application from the WebModule node.

From the operation tab, you can find the methods that can be performed on the node.

For further jmx implement, please read the JMX doc in J2SE API document.

Posted by vankevin in 15:08:40 | Permalink | No Comments »