【Thorntail】project-defaults.ymlの置き場所

ドキュメント
Auto detection picks up project-defaults.yml from src/main/resources
と書いてあるので、src/main/resourcesフォルダを作って、project-defaults.ymlを置いておけばいいのかな?
その下の
ClassLoader cl = Main.class.getClassLoader();
URL stageConfig = cl.getResource(“project-defaults.yml”);
がベタ過ぎてこまる気がする。
どう書くのかは、この辺
でもデータベースとかポートとかは、この辺かな?
JUnit風にテストするには、arquillianを使うらしい。
本家のドキュメントには
pom.xmlに

<dependencies>
  <dependency>
    <groupId>io.thorntail</groupId>
    <artifactId>arquillian</artifactId>
    <scope>test</scope>
  </dependency>
</dependencies> を付け、さらにproject-defaults.ymlにデータベースの設定(例)が
swarm:
  datasources:
    data-sources:
      MyDS:
        driver-name: myh2
        connection-url: jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
        user-name: sa
        password: sa
    jdbc-drivers:
      myh2:
        driver-class-name: org.h2.Driver
        xa-datasource-name: org.h2.jdbcx.JdbcDataSource
        driver-module-name: com.h2database.h2 と続き、
package org.wildfly.swarm.howto.incontainer;
@RunWith(Arquillian.class)
@DefaultDeployment(type = DefaultDeployment.Type.JAR)
public class InContainerTest {
@ArquillianResource

InContainerTest context;

で、インスタンスを確保して、

@Test
public void testDataSourceIsBound() throws Exception {
    DataSource ds = (DataSource) context.lookup("java:jboss/datasources/MyDS");
    assertNotNull( ds );
}

が走るようになればいいのかな?

}

これで、InContainerTestをGoすればいつもの様にJDBCの設定のテストができるっぽい。
でも、記憶を遡ると、JBOSSのJDBCの設定って通算で4通りくらいあったし、最近のJBOSS系の設定はサッパリだ。
違うドキュメントにはJUnitっぽい方法が書いてあったり、この辺は実際に動かしてみたいと(><; ) わかんないんです!なぁ。
実際のSQLは、どうなるのかな?とググってみると
Connection con;PreparedStatement prepStmt ;ResultSet rs ;
        try {
            Context context = new InitialContext();
            DataSource ds = (DataSource) context.lookup("java:jboss/datasources/${DS名}");
            con = ds.getConnection();
            String selectStatement =  "select * from ${table名} where ${field名} = ?";
            prepStmt = con.prepareStatement(selectStatement);
            prepStmt.setString(1, "*");
            rs = prepStmt.executeQuery();
            while (rs.next()) String title = rs.getString(0);
        } catch (NamingException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            rs.close(); prepStmt.close(); con.close();
        }
のように書けばいいらしい。
というか
<dependency>
  <groupId>io.thorntail</groupId>
  <artifactId>camel-mybatis</artifactId>
</dependency>
を使った方がいいかな。
ここを見たけど、挫折しそう。ここはMyBatisの本家がいい。
本家のドキュメントには The task of the JDBC driver is communicating with the database while providing a constant API to application developers. An application must supply its own JDBC driver because of the wide range of available databases and the driver version. Usually, the application does not directly interact with the JDBC driver; instead, the underlying runtime manages creating a datasource, which provides an efficient way to share and manage a discrete connection or a pool of connections to a particular database using the driver. と書いてあるから先の様にDataSourceを参照すれば、自動的にPostgreSQLConnectionPoolDataSourceが使われてコネクション・プール化されるような気がする。 ただし、postgreSQLの場合はdataSourceNameやinitialConnectionsやmaxConnectionsも必要らしいので、project-defaults.ymlの設定項目が swarm: datasources: jdbc-drivers: org.postgresql: driver-class-name: org.postgresql.Driver xa-datasource-class-name: org.postgresql.xa.PGXADataSource driver-module-name: org.postgresql data-sources:   ${DS名}: driver-name: org.postgresql connection-url: jdbc:postgresql://localhost:5432/postgres user-name: postgres password: postgres で十分なのか?不安がある。 ログ用の書き方はとても長いけど、ちゃんと作らないと困りそう。
swarm:
  logging:
    pattern-formatters:
      LOG_FORMATTER:
        pattern: "CUSTOM LOG FORMAT %p [%c] %s%e%n"
    periodic-rotating-file-handlers:
      FILE:
        file:
          path: path/to/your/file.log
        suffix: .yyyy-MM-dd
        named-formatter: LOG_FORMATTER
    root-logger:
      handlers:
      - CONSOLE
      - FILE

 
 




コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

CAPTCHA