activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From clebertsuco...@apache.org
Subject [06/47] activemq-artemis git commit: Allow users to configure jdbc driver class name
Date Mon, 08 Feb 2016 17:10:53 GMT
Allow users to configure jdbc driver class name

This patch allows users to configure the Driver class that the JDBC
store and journal uses and removes Derby as a default.


Project: http://git-wip-us.apache.org/repos/asf/activemq-artemis/repo
Commit: http://git-wip-us.apache.org/repos/asf/activemq-artemis/commit/32a9d60a
Tree: http://git-wip-us.apache.org/repos/asf/activemq-artemis/tree/32a9d60a
Diff: http://git-wip-us.apache.org/repos/asf/activemq-artemis/diff/32a9d60a

Branch: refs/heads/refactor-openwire
Commit: 32a9d60a3d8082f9bd2aed5b49f144f70ef6c8a4
Parents: 17f5c9a
Author: Martyn Taylor <mtaylor@redhat.com>
Authored: Mon Feb 8 11:11:39 2016 +0000
Committer: Clebert Suconic <clebertsuconic@apache.org>
Committed: Mon Feb 8 11:23:38 2016 -0500

----------------------------------------------------------------------
 .../config/ActiveMQDefaultConfiguration.java    |  9 +++++-
 artemis-jdbc-store/pom.xml                      |  1 +
 .../activemq/artemis/jdbc/store/JDBCUtils.java  | 30 +++++++-------------
 .../jdbc/store/journal/JDBCJournalImpl.java     | 20 +++++++++----
 .../storage/DatabaseStorageConfiguration.java   |  9 ++++++
 .../deployers/impl/FileConfigurationParser.java |  1 +
 .../impl/journal/JDBCJournalStorageManager.java |  4 +--
 .../resources/schema/artemis-configuration.xsd  |  7 +++++
 .../artemis/tests/util/ActiveMQTestBase.java    |  1 +
 .../test/resources/database-store-config.xml    |  1 +
 docs/user-manual/en/persistence.md              |  6 ++++
 pom.xml                                         |  1 +
 .../jdbc/store/journal/JDBCJournalTest.java     |  7 ++---
 13 files changed, 66 insertions(+), 31 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/32a9d60a/artemis-core-client/src/main/java/org/apache/activemq/artemis/api/config/ActiveMQDefaultConfiguration.java
----------------------------------------------------------------------
diff --git a/artemis-core-client/src/main/java/org/apache/activemq/artemis/api/config/ActiveMQDefaultConfiguration.java
b/artemis-core-client/src/main/java/org/apache/activemq/artemis/api/config/ActiveMQDefaultConfiguration.java
index b4fc493..efaa780 100644
--- a/artemis-core-client/src/main/java/org/apache/activemq/artemis/api/config/ActiveMQDefaultConfiguration.java
+++ b/artemis-core-client/src/main/java/org/apache/activemq/artemis/api/config/ActiveMQDefaultConfiguration.java
@@ -403,7 +403,10 @@ public final class ActiveMQDefaultConfiguration {
    private static String DEFAULT_STORE_TYPE = "FILE";
 
    // Default database url.  Derby database is used by default.
-   private static String DEFAULT_DATABASE_URL = "jdbc:derby:data/derby;create=true";
+   private static String DEFAULT_DATABASE_URL = null;
+
+   // Default JDBC Driver class name
+   private static String DEFAULT_JDBC_DRIVER_CLASS_NAME = null;
 
    // Default message table name, used with Database storage type
    private static String DEFAULT_MESSAGE_TABLE_NAME = "MESSAGES";
@@ -1096,4 +1099,8 @@ public final class ActiveMQDefaultConfiguration {
    public static String getDefaultBindingsTableName() {
       return DEFAULT_BINDINGS_TABLE_NAME;
    }
+
+   public static String getDefaultDriverClassName() {
+      return DEFAULT_JDBC_DRIVER_CLASS_NAME;
+   }
 }

http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/32a9d60a/artemis-jdbc-store/pom.xml
----------------------------------------------------------------------
diff --git a/artemis-jdbc-store/pom.xml b/artemis-jdbc-store/pom.xml
index a0430b4..86fe1a6 100644
--- a/artemis-jdbc-store/pom.xml
+++ b/artemis-jdbc-store/pom.xml
@@ -56,6 +56,7 @@
       <dependency>
          <groupId>org.apache.derby</groupId>
          <artifactId>derby</artifactId>
+         <scope>test</scope>
       </dependency>
 
       <dependency>

http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/32a9d60a/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/JDBCUtils.java
----------------------------------------------------------------------
diff --git a/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/JDBCUtils.java
b/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/JDBCUtils.java
index c2ffa96..b44f225 100644
--- a/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/JDBCUtils.java
+++ b/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/JDBCUtils.java
@@ -22,26 +22,16 @@ import java.sql.DriverManager;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.sql.Statement;
-import java.util.Collections;
-import java.util.List;
-
-import org.apache.derby.jdbc.AutoloadedDriver;
 
 public class JDBCUtils {
 
-   public static Driver getDriver() throws Exception {
-      Driver dbDriver = null;
-      // Load Database driver, sets Derby Autoloaded Driver as lowest priority.
-      List<Driver> drivers = Collections.list(DriverManager.getDrivers());
-      if (drivers.size() <= 2 && drivers.size() > 0) {
-         dbDriver = drivers.get(0);
-         boolean isDerby = dbDriver instanceof AutoloadedDriver;
+   public static Driver getDriver(String className) throws Exception {
 
-         if (drivers.size() > 1 && isDerby) {
-            dbDriver = drivers.get(1);
-         }
+      try {
+         Driver driver = (Driver) Class.forName(className).newInstance();
 
-         if (isDerby) {
+         // Shutdown the derby if using the derby embedded driver.
+         if (className.equals("org.apache.derby.jdbc.EmbeddedDriver")) {
             Runtime.getRuntime().addShutdownHook(new Thread() {
                @Override
                public void run() {
@@ -53,12 +43,14 @@ public class JDBCUtils {
                }
             });
          }
+         return driver;
+      }
+      catch (ClassNotFoundException cnfe) {
+         throw new RuntimeException("Could not find class: " + className);
       }
-      else {
-         String error = drivers.isEmpty() ? "No DB driver found on class path" : "Too many
DB drivers on class path, not sure which to use";
-         throw new RuntimeException(error);
+      catch (Exception e) {
+         throw new RuntimeException("Unable to instantiate driver class: ", e);
       }
-      return dbDriver;
    }
 
    public static void createTableIfNotExists(Connection connection, String tableName, String
sql) throws SQLException {

http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/32a9d60a/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/journal/JDBCJournalImpl.java
----------------------------------------------------------------------
diff --git a/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/journal/JDBCJournalImpl.java
b/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/journal/JDBCJournalImpl.java
index cc7462b..e7a0165 100644
--- a/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/journal/JDBCJournalImpl.java
+++ b/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/journal/JDBCJournalImpl.java
@@ -56,6 +56,8 @@ public class JDBCJournalImpl implements Journal {
 
    private final String tableName;
 
+   private final String jdbcDriverClass;
+
    private Connection connection;
 
    private List<JDBCJournalRecord> records;
@@ -88,9 +90,10 @@ public class JDBCJournalImpl implements Journal {
    // Sequence ID for journal records
    private AtomicLong seq = new AtomicLong(0);
 
-   public JDBCJournalImpl(String jdbcUrl, String tableName) {
+   public JDBCJournalImpl(String jdbcUrl, String tableName, String jdbcDriverClass) {
       this.tableName = tableName;
       this.jdbcUrl = jdbcUrl;
+      this.jdbcDriverClass = jdbcDriverClass;
       timerThread = "Timer JDBC Journal(" + tableName + ")";
 
       records = new ArrayList<>();
@@ -98,8 +101,15 @@ public class JDBCJournalImpl implements Journal {
 
    @Override
    public void start() throws Exception {
-      dbDriver = JDBCUtils.getDriver();
-      connection = dbDriver.connect(jdbcUrl, new Properties());
+      dbDriver = JDBCUtils.getDriver(jdbcDriverClass);
+
+      try {
+         connection = dbDriver.connect(jdbcUrl, new Properties());
+      }
+      catch (SQLException e) {
+         ActiveMQJournalLogger.LOGGER.error("Unable to connect to database using URL: " +
jdbcUrl);
+         throw new RuntimeException("Error connecting to database", e);
+      }
 
       JDBCUtils.createTableIfNotExists(connection, tableName, JDBCJournalRecord.createTableSQL(tableName));
 
@@ -109,8 +119,8 @@ public class JDBCJournalImpl implements Journal {
       deleteJournalRecords = connection.prepareStatement(JDBCJournalRecord.deleteRecordsSQL(tableName));
       deleteJournalTxRecords = connection.prepareStatement(JDBCJournalRecord.deleteJournalTxRecordsSQL(tableName));
 
-      syncTimer = new Timer(timerThread, true);
-      syncTimer.scheduleAtFixedRate(new JDBCJournalSync(this), SYNC_DELAY * 2, SYNC_DELAY);
+
+      syncTimer.schedule(new JDBCJournalSync(this), SYNC_DELAY * 2, SYNC_DELAY);
 
       started = true;
    }

http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/32a9d60a/artemis-server/src/main/java/org/apache/activemq/artemis/core/config/storage/DatabaseStorageConfiguration.java
----------------------------------------------------------------------
diff --git a/artemis-server/src/main/java/org/apache/activemq/artemis/core/config/storage/DatabaseStorageConfiguration.java
b/artemis-server/src/main/java/org/apache/activemq/artemis/core/config/storage/DatabaseStorageConfiguration.java
index f284af0..8ff62ee 100644
--- a/artemis-server/src/main/java/org/apache/activemq/artemis/core/config/storage/DatabaseStorageConfiguration.java
+++ b/artemis-server/src/main/java/org/apache/activemq/artemis/core/config/storage/DatabaseStorageConfiguration.java
@@ -27,6 +27,7 @@ public class DatabaseStorageConfiguration implements StoreConfiguration
{
 
    private String jdbcConnectionUrl = ActiveMQDefaultConfiguration.getDefaultDatabaseUrl();
 
+   private String jdbcDriverClassName = ActiveMQDefaultConfiguration.getDefaultDriverClassName();
    @Override
    public StoreType getStoreType() {
       return StoreType.DATABASE;
@@ -55,4 +56,12 @@ public class DatabaseStorageConfiguration implements StoreConfiguration
{
    public String getJdbcConnectionUrl() {
       return jdbcConnectionUrl;
    }
+
+   public void setJdbcDriverClassName(String jdbcDriverClassName) {
+      this.jdbcDriverClassName = jdbcDriverClassName;
+   }
+
+   public String getJdbcDriverClassName() {
+      return jdbcDriverClassName;
+   }
 }

http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/32a9d60a/artemis-server/src/main/java/org/apache/activemq/artemis/core/deployers/impl/FileConfigurationParser.java
----------------------------------------------------------------------
diff --git a/artemis-server/src/main/java/org/apache/activemq/artemis/core/deployers/impl/FileConfigurationParser.java
b/artemis-server/src/main/java/org/apache/activemq/artemis/core/deployers/impl/FileConfigurationParser.java
index 793abd2..12ac5e1 100644
--- a/artemis-server/src/main/java/org/apache/activemq/artemis/core/deployers/impl/FileConfigurationParser.java
+++ b/artemis-server/src/main/java/org/apache/activemq/artemis/core/deployers/impl/FileConfigurationParser.java
@@ -1150,6 +1150,7 @@ public final class FileConfigurationParser extends XMLConfigurationUtil
{
       conf.setBindingsTableName(getString(storeNode, "bindings-table-name", conf.getBindingsTableName(),
Validators.NO_CHECK));
       conf.setMessageTableName(getString(storeNode, "message-table-name", conf.getMessageTableName(),
Validators.NO_CHECK));
       conf.setJdbcConnectionUrl(getString(storeNode, "jdbc-connection-url", conf.getJdbcConnectionUrl(),
Validators.NO_CHECK));
+      conf.setJdbcDriverClassName(getString(storeNode, "jdbc-driver-class-name", conf.getJdbcDriverClassName(),
Validators.NO_CHECK));
       return conf;
    }
 

http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/32a9d60a/artemis-server/src/main/java/org/apache/activemq/artemis/core/persistence/impl/journal/JDBCJournalStorageManager.java
----------------------------------------------------------------------
diff --git a/artemis-server/src/main/java/org/apache/activemq/artemis/core/persistence/impl/journal/JDBCJournalStorageManager.java
b/artemis-server/src/main/java/org/apache/activemq/artemis/core/persistence/impl/journal/JDBCJournalStorageManager.java
index 4616e78..f23b58b 100644
--- a/artemis-server/src/main/java/org/apache/activemq/artemis/core/persistence/impl/journal/JDBCJournalStorageManager.java
+++ b/artemis-server/src/main/java/org/apache/activemq/artemis/core/persistence/impl/journal/JDBCJournalStorageManager.java
@@ -42,10 +42,10 @@ public class JDBCJournalStorageManager extends JournalStorageManager {
    protected void init(Configuration config, IOCriticalErrorListener criticalErrorListener)
{
       DatabaseStorageConfiguration dbConf = (DatabaseStorageConfiguration) config.getStoreConfiguration();
 
-      Journal localBindings = new JDBCJournalImpl(dbConf.getJdbcConnectionUrl(), dbConf.getBindingsTableName());
+      Journal localBindings = new JDBCJournalImpl(dbConf.getJdbcConnectionUrl(), dbConf.getBindingsTableName(),
dbConf.getJdbcDriverClassName());
       bindingsJournal = localBindings;
 
-      Journal localMessage = new JDBCJournalImpl(dbConf.getJdbcConnectionUrl(), dbConf.getMessageTableName());
+      Journal localMessage = new JDBCJournalImpl(dbConf.getJdbcConnectionUrl(), dbConf.getMessageTableName(),
dbConf.getJdbcDriverClassName());
       messageJournal = localMessage;
    }
 

http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/32a9d60a/artemis-server/src/main/resources/schema/artemis-configuration.xsd
----------------------------------------------------------------------
diff --git a/artemis-server/src/main/resources/schema/artemis-configuration.xsd b/artemis-server/src/main/resources/schema/artemis-configuration.xsd
index 2f71454..ef6f106 100644
--- a/artemis-server/src/main/resources/schema/artemis-configuration.xsd
+++ b/artemis-server/src/main/resources/schema/artemis-configuration.xsd
@@ -1516,6 +1516,13 @@
 
    <xsd:complexType name="databaseStoreType">
       <xsd:all>
+         <xsd:element name="jdbc-driver-class-name" type="xsd:string" minOccurs="1" maxOccurs="1">
+            <xsd:annotation>
+               <xsd:documentation>
+                  The JDBC Driver class name
+               </xsd:documentation>
+            </xsd:annotation>
+         </xsd:element>
          <xsd:element name="jdbc-connection-url" type="xsd:string" minOccurs="1" maxOccurs="1">
             <xsd:annotation>
                <xsd:documentation>

http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/32a9d60a/artemis-server/src/test/java/org/apache/activemq/artemis/tests/util/ActiveMQTestBase.java
----------------------------------------------------------------------
diff --git a/artemis-server/src/test/java/org/apache/activemq/artemis/tests/util/ActiveMQTestBase.java
b/artemis-server/src/test/java/org/apache/activemq/artemis/tests/util/ActiveMQTestBase.java
index 06f9891..3581924 100644
--- a/artemis-server/src/test/java/org/apache/activemq/artemis/tests/util/ActiveMQTestBase.java
+++ b/artemis-server/src/test/java/org/apache/activemq/artemis/tests/util/ActiveMQTestBase.java
@@ -395,6 +395,7 @@ public abstract class ActiveMQTestBase extends Assert {
       dbStorageConfiguration.setJdbcConnectionUrl(getTestJDBCConnectionUrl());
       dbStorageConfiguration.setBindingsTableName("BINDINGS");
       dbStorageConfiguration.setMessageTableName("MESSAGES");
+      dbStorageConfiguration.setJdbcDriverClassName("org.apache.derby.jdbc.EmbeddedDriver");
 
       configuration.setStoreConfiguration(dbStorageConfiguration);
 

http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/32a9d60a/artemis-server/src/test/resources/database-store-config.xml
----------------------------------------------------------------------
diff --git a/artemis-server/src/test/resources/database-store-config.xml b/artemis-server/src/test/resources/database-store-config.xml
index e387c2c..7d08ad4 100644
--- a/artemis-server/src/test/resources/database-store-config.xml
+++ b/artemis-server/src/test/resources/database-store-config.xml
@@ -24,6 +24,7 @@
             <jdbc-connection-url>jdbc:derby:target/derby/database-store;create=true</jdbc-connection-url>
             <bindings-table-name>BINDINGS_TABLE</bindings-table-name>
             <message-table-name>MESSAGE_TABLE</message-table-name>
+            <jdbc-driver-class-name>org.apache.derby.jdbc.EmbeddedDriver</jdbc-driver-class-name>
          </database-store>
       </store>
    </core>

http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/32a9d60a/docs/user-manual/en/persistence.md
----------------------------------------------------------------------
diff --git a/docs/user-manual/en/persistence.md b/docs/user-manual/en/persistence.md
index e5bcf81..83180b7 100644
--- a/docs/user-manual/en/persistence.md
+++ b/docs/user-manual/en/persistence.md
@@ -376,6 +376,7 @@ To configure Apache ActiveMQ Artemis to use a database for persisting
messages a
             <jdbc-connection-url>jdbc:derby:target/derby/database-store;create=true</jdbc-connection-url>
             <bindings-table-name>BINDINGS_TABLE</bindings-table-name>
             <message-table-name>MESSAGE_TABLE</message-table-name>
+            <jdbc-driver-class-name>org.apache.derby.jdbc.EmbeddedDriver</jdbc-driver-class-name>
          </database-store>
       </store>
 ```
@@ -392,6 +393,11 @@ To configure Apache ActiveMQ Artemis to use a database for persisting
messages a
 
     The name of the table in which messages and related data will be persisted for the ActiveMQ
Artemis server.  Specifying table names allows users to share single database amongst multiple
servers, without interference.
 
+-   `jdbc-driver-class-name`
+
+    The fully qualified class name of the desired database Driver.
+
+
 ## Configuring Apache ActiveMQ Artemis for Zero Persistence
 
 In some situations, zero persistence is sometimes required for a

http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/32a9d60a/pom.xml
----------------------------------------------------------------------
diff --git a/pom.xml b/pom.xml
index aae8f1f..8294bd0 100644
--- a/pom.xml
+++ b/pom.xml
@@ -208,6 +208,7 @@
             <groupId>org.apache.derby</groupId>
             <artifactId>derby</artifactId>
             <version>${apache.derby.version}</version>
+            <scope>test</scope>
          </dependency>
          <dependency>
             <groupId>org.eclipse.paho</groupId>

http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/32a9d60a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/jdbc/store/journal/JDBCJournalTest.java
----------------------------------------------------------------------
diff --git a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/jdbc/store/journal/JDBCJournalTest.java
b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/jdbc/store/journal/JDBCJournalTest.java
index d978f08..ac2188d 100644
--- a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/jdbc/store/journal/JDBCJournalTest.java
+++ b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/jdbc/store/journal/JDBCJournalTest.java
@@ -18,7 +18,6 @@ package org.apache.activemq.artemis.tests.integration.jdbc.store.journal;
 
 import java.util.ArrayList;
 import java.util.List;
-import java.util.Properties;
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.TimeUnit;
 
@@ -41,16 +40,16 @@ public class JDBCJournalTest {
 
    private static final String JOURNAL_TABLE_NAME = "MESSAGE_JOURNAL";
 
+   private static final String DRIVER_CLASS = "org.apache.derby.jdbc.EmbeddedDriver";
+
    private JDBCJournalImpl journal;
 
    private String jdbcUrl;
 
-   private Properties jdbcConnectionProperties;
-
    @Before
    public void setup() throws Exception {
       jdbcUrl = "jdbc:derby:target/data;create=true";
-      journal = new JDBCJournalImpl(jdbcUrl, JOURNAL_TABLE_NAME);
+      journal = new JDBCJournalImpl(jdbcUrl, JOURNAL_TABLE_NAME, DRIVER_CLASS);
       journal.start();
    }
 


Mime
View raw message