weli@power:~/projs/jboss-as-7.1.1.Final$ ls LICENSE.txt bundles jboss-modules.jar README.txt copyright.txt modules appclient docs standalone bin domain welcome-content
我们进到modules目录,创建postgres的目录:
mkdir -p org/postgresql/main
进入到创建好的目录,从Postgresql的网站下载所需的jdbc库,我下载的是postgresql-9.1-903.jdbc4.jar:
403_75@
@H_301_9@@H_301_9@weli@power@H_301_9@:~/projs/jboss-as-7.1@H_301_9@.1@H_301_9@.Final/modules/org/postgresql/main$ ls
@H_301_9@postgresql-9.1@H_301_9@-903@H_301_9@.jdbc4.jar
@H_weli@power:~/projs/jboss-as-7.1.1.Final/modules/org/postgresql/main$ ls postgresql-9.1-903.jdbc4.jar
然后我们要制作module.xml,内容如下:
- @H_301_9@<module@H_301_9@ xmlns@H_301_9@="urn:jboss:module:1.1"@H_301_9@ name@H_301_9@="org.postgresql">@H_301_9@
- @H_301_9@<!-- Load with jboss-cli command:
- @H_301_9@/subsystem@H_301_9@=datasources@H_301_9@/jdbc-driver@H_301_9@=postgresql@H_301_9@-driver:add(driver-name@H_301_9@=postgresql@H_301_9@-driver,driver-class-name@H_301_9@=org@H_301_9@.postgresql.Driver,driver-module-name@H_301_9@=org@H_301_9@.postgresql)
- @H_301_9@-->@H_301_9@
- @H_301_9@<resources>@H_301_9@
- @H_301_9@<resource-root@H_301_9@ path@H_301_9@="postgresql-9.1-903.jdbc4.jar"/>@H_301_9@
- @H_301_9@</resources>@H_301_9@
- @H_301_9@<dependencies>@H_301_9@
- @H_301_9@<module@H_301_9@ name@H_301_9@="javax.api"/>@H_301_9@
- @H_301_9@<module@H_301_9@ name@H_301_9@="javax.transaction.api"/>@H_301_9@
- @H_301_9@</dependencies>@H_301_9@
- @H_301_9@</module>@H_301_9@
<module xmlns="urn:jboss:module:1.1" name="org.postgresql"> <!-- Load with jboss-cli command: /subsystem=datasources/jdbc-driver=postgresql-driver:add(driver-name=postgresql-driver,driver-class-name=org.postgresql.Driver,driver-module-name=org.postgresql) --> <resources> <resource-root path="postgresql-9.1-903.jdbc4.jar"/> </resources> <dependencies> <module name="javax.api"/> <module name="javax.transaction.api"/> </dependencies> </module>
这样,这个module就算制作完成了:
weli@power:~/projs/jboss-as-7.1.1.Final/modules/org/postgresql/main$ ls module.xml postgresql-9.1-903.jdbc4.jar
接下来要修改AS7的配置文件,如果用standalone模式,那么就修改standalone/configuration/standalone.xml:
- @H_301_9@<datasources>@H_301_9@
- @H_301_9@<drivers>@H_301_9@
- @H_301_9@<driver@H_301_9@ name@H_301_9@="h2"@H_301_9@ module@H_301_9@="com.h2database.h2">@H_301_9@
- @H_301_9@<xa-datasource-class>@H_301_9@org.h2.jdbcx.JdbcDataSource</xa-datasource-class>@H_301_9@
- @H_301_9@</driver>@H_301_9@
- @H_301_9@<driver@H_301_9@ name@H_301_9@="postgresql-driver"@H_301_9@ module@H_301_9@="org.postgresql">@H_301_9@
- @H_301_9@<driver-class>@H_301_9@org.postgresql.Driver</driver-class>@H_301_9@
- @H_301_9@</driver>@H_301_9@
- @H_301_9@</drivers>@H_301_9@
- @H_301_9@</datasources>@H_301_9@
<datasources> <drivers> <driver name="h2" module="com.h2database.h2"> <xa-datasource-class>org.h2.jdbcx.JdbcDataSource</xa-datasource-class> </driver> <driver name="postgresql-driver" module="org.postgresql"> <driver-class>org.postgresql.Driver</driver-class> </driver> </drivers> </datasources>
AS7自带一个H2的module,下面的postgresql-driver是我们添加的。这样,就完成了对postgres模块的导入。其实,我们不必手工编辑standalone.xml来添加模块。可以在AS7启动后,通过命令终端来添加:
/subsystem=datasources/jdbc-driver=postgresql-driver:add(driver-name=postgresql-driver,driver-module-name=org.postgresql)
如果我们要使用这个模块,可以配置datasource:
- @H_301_9@<datasource@H_301_9@ jndi-name@H_301_9@="java:jboss/datasources/SOME_DB"@H_301_9@ pool-name@H_301_9@="SOME_DB"@H_301_9@ enabled@H_301_9@="true"@H_301_9@ use-java-context@H_301_9@="true">@H_301_9@
- @H_301_9@<connection-url>@H_301_9@jdbc:postgresql:SOME_DB</connection-url>@H_301_9@
- @H_301_9@<driver>@H_301_9@postgresql-driver</driver>@H_301_9@
- @H_301_9@<security>@H_301_9@
- @H_301_9@<user-name>@H_301_9@XXX</user-name>@H_301_9@
- @H_301_9@<password>@H_301_9@YYY</password>@H_301_9@
- @H_301_9@</security>@H_301_9@
- @H_301_9@</datasource>