自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

使用Maven配置JBoss、Wildfly數(shù)據(jù)源

開發(fā) 后端
大多數(shù)Java EE應(yīng)用在其業(yè)務(wù)邏輯層中會(huì)訪問數(shù)據(jù)庫,所以開發(fā)者會(huì)經(jīng)常需要為應(yīng)用服務(wù)器配置數(shù)據(jù)庫驅(qū)動(dòng)和數(shù)據(jù)庫連接。這篇文章會(huì)討論如何用Maven自動(dòng)化JBoss、Wildfly和Postgre數(shù)據(jù)庫的配置。

大多數(shù)Java EE應(yīng)用在其業(yè)務(wù)邏輯層中會(huì)訪問數(shù)據(jù)庫,所以開發(fā)者會(huì)經(jīng)常需要為應(yīng)用服務(wù)器配置數(shù)據(jù)庫驅(qū)動(dòng)和數(shù)據(jù)庫連接。這篇文章會(huì)討論如何用Maven自動(dòng)化JBoss、Wildfly和Postgre數(shù)據(jù)庫的配置。

Maven 配置

讓我們從下面的pom.xml 開始吧,

Wildfly Maven Plugin

  1. <plugin> 
  2.     <groupid>org.wildfly.plugins</groupid> 
  3.     <artifactid>wildfly-maven-plugin</artifactid> 
  4.     <version>1.0.2.Final</version> 
  5.     <configuration> 
  6.         <executecommands> 
  7.             <batch>false</batch> 
  8.             <scripts>%MINIFYHTML7db47c7a4774fb3aa46c5ca8120866ec8%</scripts> 
  9.         </executecommands> 
  10.     </configuration> 
  11.     <dependencies> 
  12.         <dependency> 
  13.             <groupid>org.postgresql</groupid> 
  14.             <artifactid>postgresql</artifactid> 
  15.             <version>9.3-1102-jdbc41</version> 
  16.         </dependency> 
  17.     </dependencies> 
  18. </plugin> 

我們開始使用Wildfly Maven Plugin在應(yīng)用服務(wù)器執(zhí)行命令腳本。我們已經(jīng)添加了 Postgre的依賴, Maven會(huì)下載依賴, 因?yàn)槲覀儗⒁诤竺姘阉拥椒?wù)器中。這里有一個(gè) ${cli.file} 屬性, 將指明將執(zhí)行哪一個(gè)腳本。

讓我們?cè)趐om.xml中添加下面內(nèi)容:

Maven Resources Plugin

  1. <plugin> 
  2.     <groupid>org.apache.maven.plugins</groupid> 
  3.     <artifactid>maven-resources-plugin</artifactid> 
  4.     <version>2.6</version> 
  5.     <executions> 
  6.         <execution> 
  7.             <id>copy-resources</id> 
  8.             <phase>process-resources</phase> 
  9.             <goals> 
  10.                 <goal>copy-resources</goal> 
  11.             </goals> 
  12.             <configuration> 
  13.                 <outputdirectory>${basedir}/target/scripts</outputdirectory> 
  14.                 <resources> 
  15.                     <resource> 
  16.                         <directory>src/main/resources/scripts</directory> 
  17.                         <filtering>true</filtering> 
  18.                     </resource> 
  19.                 </resources> 
  20.                 <filters> 
  21.                     <filter>${basedir}/src/main/resources/configuration.properties</filter> 
  22.                 </filters> 
  23.             </configuration> 
  24.         </execution> 
  25.     </executions> 
  26. </plugin> 

用這個(gè)插件,我們可以過濾包含在src/main/resources/scripts這個(gè)目錄中的腳本。使用${basedir}/src/main/resources/configuration.properties這個(gè)文件中的屬性進(jìn)行替換。

最后添加一些 Maven屬性到pom.xml文件中:

Maven Profiles

  1. <profiles> 
  2.     <profile> 
  3.         <id>install-driver</id> 
  4.         <properties> 
  5.             <cli.file>wildfly-install-postgre-driver.cli</cli.file> 
  6.         </properties> 
  7.     </profile> 
  8.  
  9.     <profile> 
  10.         <id>remove-driver</id> 
  11.         <properties> 
  12.             <cli.file>wildfly-remove-postgre-driver.cli</cli.file> 
  13.         </properties> 
  14.     </profile> 
  15.  
  16.     <profile> 
  17.         <id>install-wow-auctions</id> 
  18.         <properties> 
  19.             <cli.file>wow-auctions-install.cli</cli.file> 
  20.         </properties> 
  21.     </profile> 
  22.  
  23.     <profile> 
  24.         <id>remove-wow-auctions</id> 
  25.         <properties> 
  26.             <cli.file>wow-auctions-remove.cli</cli.file> 
  27.         </properties> 
  28.     </profile> 
  29. </profiles> 

Wildfly Script Files

添加驅(qū)動(dòng)

添加驅(qū)動(dòng)的腳本:

wildfly-install-postgre-driver.cli

  1. # Connect to Wildfly instance 
  2. connect 
  3.  
  4. # Create Oracle JDBC Driver Module 
  5. # If the module already exists, Wildfly will output a message saying that the module already exists and the script exits. 
  6. module add \ 
  7.     --name=org.postgre \ 
  8.     --resources=${settings.localRepository}/org/postgresql/postgresql/9.3-1102-jdbc41/postgresql-9.3-1102-jdbc41.jar \ 
  9.     --dependencies=javax.api,javax.transaction.api 
  10.  
  11. # Add Driver Properties 
  12. /subsystem=datasources/jdbc-driver=postgre: \ 
  13.     add( \ 
  14.         driver-name="postgre", \ 
  15.         driver-module-name="org.postgre"

數(shù)據(jù)庫驅(qū)動(dòng)作為Wildfly的一個(gè)模塊(Module)。這樣數(shù)據(jù)庫驅(qū)動(dòng)可以被部署在服務(wù)器中的所有應(yīng)用使用。使用${settings.localRepository} 配置,我們指定數(shù)據(jù)庫驅(qū)動(dòng)下載到你的本地Maven倉庫。還記得我們加到 Wildfly Maven Plugin的依賴嗎,在你插件運(yùn)行的時(shí)候他將下載驅(qū)動(dòng)并加到服務(wù)器中。要運(yùn)行腳本(必須保證應(yīng)用服務(wù)器正在運(yùn)行中)可以執(zhí)行下面的命令:

  1. mvn process-resources wildfly:execute-commands -P "install-driver" 

需要用process-resources生命周期替換腳本中的屬性。在這個(gè)例子中 ${settings.localRepository} 被替換為 /Users/radcortez/.m3/repository/. 。檢查target/scripts 文件夾。在運(yùn)行命令后,可以在Maven的日志看到以下輸出:

  1. {"outcome" => "success"

服務(wù)器上的日志:

  1. INFO  [org.jboss.as.connector.subsystems.datasources] (management-handler-thread - 4) JBAS010404: Deploying non-JDBC-compliant driver class org.postgresql.Driver (version 9.3
  2. INFO  [org.jboss.as.connector.deployers.jdbc] (MSC service thread 1-4) JBAS010417: Started Driver service with driver-name = postgre 

wildfly-remove-postgre-driver.cli

  1. # Connect to Wildfly instance 
  2. connect 
  3.  
  4. if (outcome == success) of /subsystem=datasources/jdbc-driver=postgre:read-attribute(name=driver-name) 
  5.  
  6.     # Remove Driver 
  7.     /subsystem=datasources/jdbc-driver=postgre:remove 
  8.  
  9. end-if 
  10.  
  11. # Remove Oracle JDBC Driver Module 
  12. module remove --name=org.postgre 

這段腳本是把驅(qū)動(dòng)從你的服務(wù)器上刪除。允許 mvn wildfly:execute-commands -P “remove-driver”,如果你已經(jīng)執(zhí)行了以前的命令就不需要再配置process-resource,除非腳本發(fā)生改變。

添加數(shù)據(jù)源

wow-auctions-install.cli

這個(gè)腳本使用命令添加了一個(gè)數(shù)據(jù)源

wow-auctions-install.cli

  1. # Connect to Wildfly instance 
  2. connect 
  3.  
  4. # Create Datasource 
  5. /subsystem=datasources/data-source=WowAuctionsDS: \ 
  6.     add( \ 
  7.         jndi-name="${datasource.jndi}", \ 
  8.         driver-name=postgre, \ 
  9.         connection-url="${datasource.connection}", \ 
  10.         user-name="${datasource.user}", \ 
  11.         password="${datasource.password}"
  12.  
  13. /subsystem=ee/service=default-bindings:write-attribute(name="datasource", value="${datasource.jndi}"

我們依然需要一個(gè)文件來定義這些屬性。

configuration.properties

  1. datasource.jndi=java:/datasources/WowAuctionsDS 
  2. datasource.connection=jdbc:postgresql://localhost:5432/wowauctions 
  3. datasource.user=wowauctions 
  4. datasource.password=wowauctions 

Java EE 7 默認(rèn)數(shù)據(jù)源

Java EE 7中, 指定容器必須提供一個(gè)默認(rèn)數(shù)據(jù)源。不要在程序中使用 java:/datasources/WowAuctionsDS JNDI 定義的數(shù)據(jù)源,我們將指定一個(gè)新創(chuàng)建的數(shù)據(jù)源 /subsystem=ee/service=default-bindings:write- attribute(name=”datasource”, value=”${datasource.jndi}”)。 這樣就無需改變程序中的任何配置。 執(zhí)行 mvn wildfly:execute-commands -P “install-wow-auctions”,就可以得到以下輸出:

  1. org.jboss.as.cli.impl.CommandContextImpl printLine 
  2. INFO: {"outcome" => "success"
  3. {"outcome" => "success"
  4. org.jboss.as.cli.impl.CommandContextImpl printLine 
  5. INFO: {"outcome" => "success"
  6. {"outcome" => "success"

服務(wù)器日志:

  1. INFO  [org.jboss.as.connector.subsystems.datasources] (MSC service thread 1-1) JBAS010400: Bound data source 

wow-auctions-remove.cli

  1. # Connect to Wildfly instance 
  2. connect 
  3.  
  4. # Remove Datasources 
  5. /subsystem=datasources/data-source=WowAuctionsDS:remove 
  6.  
  7. /subsystem=ee/service=default-bindings:write-attribute(name="datasource", value="java:jboss/datasources/ExampleDS"

上面是刪除數(shù)據(jù)源轉(zhuǎn)為Java EE 7 默認(rèn)數(shù)據(jù)源的腳本。執(zhí)行時(shí)用這個(gè)命令:mvn wildfly:execute-commands -P "remove-wow-auctions"。

總結(jié)

這篇博客展示了如何自動(dòng)在Wildfly實(shí)例中添加刪除添加驅(qū)動(dòng)和數(shù)據(jù)源。如果需要在不同數(shù)據(jù)庫之間切換或者打算重頭配置服務(wù)器,本文的內(nèi)容會(huì)對(duì)你非常有幫助。在做持續(xù)集成(CI)時(shí),這些腳本稍作調(diào)整就可以轉(zhuǎn)到其他驅(qū)動(dòng)。

你可以在這里得到代碼WoW Auctions Github repo。

原文鏈接: javacodegeeks 翻譯: ImportNew.com 孫 彪彪
譯文鏈接: http://www.importnew.com/13718.html

 

 

責(zé)任編輯:張偉 來源: ImportNew
相關(guān)推薦

2009-06-15 13:24:46

JBoss數(shù)據(jù)源

2009-06-16 16:20:58

JBoss4配置Inf

2010-12-27 09:59:11

ODBC數(shù)據(jù)源

2024-10-30 10:22:17

2023-09-07 08:39:39

copy屬性數(shù)據(jù)源

2020-12-31 07:55:33

spring bootMybatis數(shù)據(jù)庫

2015-12-25 16:37:02

RxJava數(shù)據(jù)源數(shù)據(jù)

2009-06-26 17:37:37

JBOSS配置

2012-06-17 13:04:45

2010-05-14 15:32:51

配置MySQL

2023-01-04 09:33:31

SpringBootMybatis

2023-11-27 09:16:53

Python數(shù)據(jù)源類型

2017-09-04 14:52:51

Tomcat線程數(shù)據(jù)源

2012-09-04 13:48:57

云計(jì)算數(shù)據(jù)源Odata API

2009-07-14 09:55:33

MyEclipse W

2022-12-19 07:21:35

Hutool-db數(shù)據(jù)庫JDBC

2009-08-14 10:26:27

ibatis多數(shù)據(jù)源

2014-08-15 09:55:09

SSIS

2017-06-14 23:42:27

大數(shù)據(jù)數(shù)據(jù)源架構(gòu)

2009-09-08 11:09:39

LINQ數(shù)據(jù)源
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)