廣告聯播

2008年11月14日 星期五

[JNDI] Java + Tomcat + MySQL 的連結設定

From: Polin Wei

環境:
Web Server: apache-tomcat-6.0.14
DB Version: MySQL 5.0.45
DB : test
Table: users(username varchar(30),password varchar(30))
JDBC Driver: mysql-connector-java-5.1.7-bin.jar
JAVA: jdk1.6.0_07


在Web步驟如下:
Step 1: Copy MySQL Connector/J 的 JAR 檔
mysql-connector-java-5.1.7-bin.jar 複製到 $TOMCAT_HOME/lib

Step 2:在 $TOMCAT_HOME/conf/server.xml 的 <Context></Context> 中加入下列的參數
<Resource name="jdbc/MySQLDS01" auth="Container" type="javax.sql.DataSource" factory="org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/test" username="username" password="password" maxActive="20" maxIdle="10" maxWait="-1" />




Step 3: 撰寫一個網頁 MySQLConnTest.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@page import="com.mysql.*,java.util.*,javax.naming.*,java.sql.*,javax.sql.*" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%    
    Context ctx = new InitialContext();     
    String strLookup = "java:comp/env/jdbc/MySQLDS01";
    DataSource ds =(DataSource) ctx.lookup(strLookup);
    Connection con = ds.getConnection();
    Statement smt = con.createStatement();
    String sql = "";
    ResultSet rs = null;   
     
    sql = "SELECT * FROM users";
    rs = smt.executeQuery(sql);
    while(rs.next())
    {
         out.println(rs.getString(1));
         out.println(rs.getString(2));        
    }


%>


</body>
</html>




這樣就可以利用 JNDI 的方式連到 MySQL , 並在網頁中顯示資料庫test中表格users裡的資料.

若是純以 Java Code 來執行連結MySQL , 則程式碼如下:

package com.demos;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


public class MySQLConnTest {


 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  Connection conn = null;
  String Server_IP = "127.0.0.1";


  try {
   Class.forName("com.mysql.jdbc.Driver");
   String sql = "";
   conn = DriverManager.getConnection("jdbc:mysql://" + Server_IP
     + ":3306/test", "username", "password");
   sql = "select * from users";
   // Statement stmt =
   // conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
   // ResultSet.CONCUR_READ_ONLY);
   Statement stmt = conn.createStatement();
   ResultSet rs = stmt.executeQuery(sql);
   if (rs.next()) {
    System.out.println(rs.getString("userName"));
    System.out.println(rs.getString("password"));
   } else {
    System.out.print("Cound not find data in test of MySQL");
   }
  } catch (Exception e) {
   // TODO: handle exception
   e.printStackTrace();
   try {
    conn.rollback();
   } catch (SQLException e1) {
    e1.printStackTrace();
   }
  } finally {
   try {
    if (conn != null && !conn.isClosed()) {
     conn.close();
    }
   } catch (Exception e) {
    e.printStackTrace();
   }
  }


 }


}



2008年11月4日 星期二

[Java] quartz 工作定時排程器

From: Polin Wei

  在一個應用系統中常要定時去執行某些程式, 例如: 同步異質平台的資料, 或是定時發信件; Java是一種跨平台(Window/Linux)的程式語言, 雖然在 Window 平台可以用 AT 指令來作排程, 在 Linux 可以用 crontab 來排程, 但總是無法與系統作密切的結合.

  quartz是個不錯的選擇, 它一樣可以跨異質平台作業, 而且與 Java 的結合也很密切, 只要在Java程式段有
public void execute(JobExecutionContext context)
throws JobExecutionException {....}
quartz作接口即可
, 下面我們來看一下如何實作它.總共有幾個步驟, 就可以讓它順利執行.

設定

1. 下載 quartz 所需的 JAR 檔

2. WEB-INF/web.xml 中加入quartz的 servlet
<servlet>
<servlet-name>QuartzInitializer</servlet-name>
<servlet-class>
org.quartz.ee.servlet.QuartzInitializerServlet
</servlet-class>
<init-param>
<param-name>config-file</param-name>
<param-value>/quartz.properties</param-value>
</init-param>
<init-param>
<param-name>shutdown-on-unload</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>start-scheduler-on-load</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>


3. 建立 quartz的設定檔
  在 Java Resource 的目錄建立二個檔案 quartz.properties 及 jobSchedule.xml(此檔的檔名可以自定), 檔案內容如下:

quartz.properties 檔
org.quartz.jobStore.class=org.quartz.simpl.RAMJobStore
org.quartz.jobStore.misfireThreshold=60000
org.quartz.plugin.jobInitializer.class=org.quartz.plugins.xml.JobInitializationPlugin
org.quartz.plugin.jobInitializer.failOnFileNotFound=true
org.quartz.plugin.jobInitializer.fileName=/jobSchedule.xml => 這個檔案是定義 JOB 的排程用
org.quartz.plugin.jobInitializer.overWriteExistingJobs=true
org.quartz.plugin.jobInitializer.validating=false
org.quartz.plugin.jobInitializer.validatingSchema=true
org.quartz.plugin.triggHistory.class=org.quartz.plugins.history.LoggingTriggerHistoryPlugin
org.quartz.plugin.triggHistory.triggerCompleteMessage=Trigger {1}.{0} completed firing job {6}.{5} at {4, date, HH\:mm\:ss MM/dd/yyyy} with resulting trigger instruction code\: {9}
org.quartz.plugin.triggHistory.triggerFiredMessage=Trigger {1}.{0} fired job {6}.{5} at\: {4, date, HH\:mm\:ss MM/dd/yyyy}
org.quartz.scheduler.instanceId=1
org.quartz.scheduler.instanceName=sched1
org.quartz.threadPool.class=org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount=100
org.quartz.threadPool.threadPriority=5


jobSchedule.xml 檔
<?xml version="1.0" encoding="UTF-8"?>
<quartz>
<job>
<job-detail>
<name>job_mail_demo</name> ==> 定義工作的名字: job_mail_demo
<group>DEFAULT</group>
<description>job_mail_demo</description>
<job-class>com.demos.job_mail_demo</job-class> ==> 定義排程器要執行那一個 Java 程式
<job-data-map allows-transient-data="true">
<entry>
<key>name</key>
<value>job_mail_demo</value>
</entry>
</job-data-map>
</job-detail>
<trigger>
<cron>
<name>Cron_job_mail</name> ==> 定義排程器的名字: Cron_job_mail
<group>DEFAULT</group>
<job-name>job_mail_demo</job-name> ==> 定義排程器[Cron_job_mail]內要執行的工作[job_mail_demo]
<job-group>DEFALUT</job-group>
<cron-expression>0 0/5 * * * ?</cron-expression> ==> 每5分鐘執行工作排程 job_mail_demo 一次
</cron>
</trigger>
</job>
</quartz>
<!-- Cron 的小小說明表示方式意義
"0 0 12 * * ?" Fire at 12pm (noon) every day
"0 15 10 ? * *" Fire at 10:15am every day
"0 15 10 * * ?" Fire at 10:15am every day
"0 15 10 * * ? *" Fire at 10:15am every day
"0 15 10 * * ? 2005" Fire at 10:15am every day during the year 2005
"0 * 14 * * ?" Fire every minute starting at 2pm and ending at 2:59pm, every day
"0 0/5 14 * * ?" Fire every 5 minutes starting at 2pm and ending at 2:55pm, every day
"0 0/5 14,18 * * ?" Fire every 5 minutes starting at 2pm and ending at 2:55pm, AND fire every 5 minutes starting at 6pm and ending at 6:55pm, every day
"0 0-5 14 * * ?" Fire every minute starting at 2pm and ending at 2:05pm, every day
"0 10,44 14 ? 3 WED" Fire at 2:10pm and at 2:44pm every Wednesday in the month of March.
"0 15 10 ? * MON-FRI" Fire at 10:15am every Monday, Tuesday, Wednesday, Thursday and Friday
"0 15 10 15 * ?" Fire at 10:15am on the 15th day of every month
"0 15 10 L * ?" Fire at 10:15am on the last day of every month
"0 15 10 ? * 6L" Fire at 10:15am on the last Friday of every month
"0 15 10 ? * 6L" Fire at 10:15am on the last Friday of every month
"0 15 10 ? * 6L 2002-2005" Fire at 10:15am on every last friday of every month during the years 2002, 2003, 2004 and 2005
"0 15 10 ? * 6#3" Fire at 10:15am on the third Friday of every month
-->


4. Java 程式段 job_mail_demo.java
package com.demos;

import java.sql.Connection;
import java.util.Date;
import org.apache.log4j.Logger;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import com.gu.utils.BA_TOOLS;

public class job_mail_demo implements Job{
static Logger loger = Logger.getLogger(job_mail_demo.class.getName());

public void execute(JobExecutionContext context)
throws JobExecutionException {
String name = context.getJobDetail().getJobDataMap().getString("name");

System.out.println("run " + name + " at " + new java.util.Date());
loger.warn("Executing job: executing at " + new Date());

try {
BA_TOOLS tools = BA_TOOLS.getInstance();
tools.sendmail("localhost", "polin.wei@mail.com", "polin.wei@mail.com", "Schedule Job Demo",
"Schedule Job Demo");

System.out.println("END " + name + " at " + new java.util.Date());
loger.warn("job END: END at " + new Date());
} catch (Exception e) {
e.printStackTrace();
}

}

}



  系統執行時, quartz 就會依照排程定時去發 mail , 因為在 Java 程式有設 log4j 來作偵測 quartz 是否有定時執行, 所以可以看到 Java Console 的顯示如下:

[ERROR] 04 十一月 06:10:05.456 下午 Thread-8 [com.gu.utils.MailThread-run]
polin.wei@xxx.com郵件送出失敗!

2008/11/4 下午 06:15:00 org.quartz.plugins.history.LoggingTriggerHistoryPlugin triggerComplete
資訊: Trigger DEFAULT.Cron_job_mail completed firing job DEFAULT.job_mail_demo at 18:15:00 11/04/2008 with resulting trigger instruction code: DO NOTHING
[DEBUG] 04 十一月 06:15:05.679 下午 Thread-8 [com.gu.utils.MailThread-run]
polin.wei@mail.com郵件已送出!