jboss 連接池 scheduler

將oracle-ds.xml 放到  jboss_home\server\default\deploy下

 

<?xml version="1.0" encoding="UTF-8"?>
<datasources>
  <local-tx-datasource>
    <jndi-name>MYDS</jndi-name>
    <connection-url>jdbc:oracle:thin:@1.18.18.137:1522:NAME</connection-url>
    <driver-class>oracle.jdbc.driver.OracleDriver</driver-class>
    <user-name>dcx</user-name>
    <password>dcx</password>
    <exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.OracleExceptionSorter</exception-sorter-class-name>
	  <min-pool-size>10</min-pool-size>  
      <max-pool-size>30</max-pool-size>
	  <idle-timeout-minutes>5</idle-timeout-minutes>
      <metadata>
         <type-mapping>Oracle9i</type-mapping>
      </metadata>
  </local-tx-datasource>
</datasources>

 

oracle jar放到  jboss_home\server\default\lib 下

 

 

 

java 代碼 獲得連接

import java.sql.SQLException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class DS {
    public static DataSource ds = null;
    
    public static java.sql.Connection getConnection() throws SQLException{
        if(ds == null){
            Context ctx;
            try {
                ctx = new InitialContext();
                ds = (DataSource) ctx.lookup("java:MYDS");
            } catch (NamingException e) {
                e.printStackTrace();
                throw new RuntimeException("init data source error");
            }
        }
        return ds.getConnection();
    }
}
 

 

 

查看運行時的連接池狀態

 

http://localhost:8080/web-console/

 

System

--JMS MBeans

----jboss.jca

------jboss.jca:service=ManagedConnectionPool,name=MYDS

--------這下面有很多屬性(如 ConnectionCount), 可以選中 右鍵  graph查看圖示

 

!!!!

如果把jboss-common.jar 放到WEB-INF/lib 下, 最後被部署到jboss上,

會導致

ds = (DataSource) ctx.lookup("java:MYDS");

得到的ds == null, 所以不要這麼做

!!!!

 

 

 

 

jboss scheduler docs

http://docs.jboss.org/jbossas/jboss4guide/r1/html/ch10.html

 

http://hi.baidu.com/trstones/blog/item/b8f25416d43c9e49f2de32ac.html

寫一個 實現 import org.jboss.varia.scheduler.Schedulable 的類

 

 

1. export the project's src to a jar file mysrc.jar

2. put the  mysrc.jar, dependcy.jar in jboss_home/server/default/lib

3. put scheduler-service.xml in jboss_home/server/default/deploy

 

 

   <mbean code="org.jboss.varia.scheduler.Scheduler"

          name=":service=estoretestItem">

      <attribute name="StartAtStartup">true</attribute>

      <attribute name="SchedulableClass">com.lich.MySrcSchedulable</attribute>

      <attribute name="SchedulableArguments">64039,64039,01</attribute> //傳給構造器的參數

      <attribute name="SchedulableArgumentTypes">java.lang.String,java.lang.String,java.lang.String</attribute> //參數類型

      <attribute name="InitialStartDate">0</attribute>  //開始時間  ,0是馬上

      <attribute name="SchedulePeriod">90000</attribute>  //運行頻率

      <attribute name="InitialRepetitions">-1</attribute>

      <attribute name="FixedRate">true</attribute>    

   </mbean>

 

scheduler 任務是server級別的, 和war是無關的

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章