mirror of https://github.com/apache/activemq.git
minor refactor to reuse the lazy construction logic of the DataSource across the journaled and pure JDBC persistence adapter
git-svn-id: https://svn.apache.org/repos/asf/incubator/activemq/trunk@412110 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
d273cf9c8c
commit
a0397468fa
|
@ -19,29 +19,26 @@ package org.apache.activemq.store;
|
|||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.apache.activeio.journal.Journal;
|
||||
import org.apache.activeio.journal.active.JournalImpl;
|
||||
import org.apache.activemq.store.jdbc.DataSourceSupport;
|
||||
import org.apache.activemq.store.jdbc.JDBCAdapter;
|
||||
import org.apache.activemq.store.jdbc.JDBCPersistenceAdapter;
|
||||
import org.apache.activemq.store.jdbc.Statements;
|
||||
import org.apache.activemq.store.journal.JournalPersistenceAdapter;
|
||||
import org.apache.activemq.store.journal.QuickJournalPersistenceAdapter;
|
||||
import org.apache.activemq.thread.TaskRunnerFactory;
|
||||
import org.apache.derby.jdbc.EmbeddedDataSource;
|
||||
|
||||
/**
|
||||
* Factory class that can create PersistenceAdapter objects.
|
||||
*
|
||||
* @version $Revision: 1.4 $
|
||||
*/
|
||||
public class DefaultPersistenceAdapterFactory implements PersistenceAdapterFactory {
|
||||
public class DefaultPersistenceAdapterFactory extends DataSourceSupport implements PersistenceAdapterFactory {
|
||||
|
||||
private int journalLogFileSize = 1024*1024*20;
|
||||
private int journalLogFiles = 2;
|
||||
private File dataDirectory;
|
||||
private DataSource dataSource;
|
||||
private TaskRunnerFactory taskRunnerFactory;
|
||||
private Journal journal;
|
||||
private boolean useJournal=true;
|
||||
|
@ -50,11 +47,11 @@ public class DefaultPersistenceAdapterFactory implements PersistenceAdapterFacto
|
|||
private JDBCPersistenceAdapter jdbcPersistenceAdapter = new JDBCPersistenceAdapter();
|
||||
|
||||
public PersistenceAdapter createPersistenceAdapter() throws IOException {
|
||||
File dataDirectory = getDataDirectory();
|
||||
jdbcPersistenceAdapter.setDataSource(getDataSource());
|
||||
|
||||
if( !useJournal )
|
||||
if( !useJournal ) {
|
||||
return jdbcPersistenceAdapter;
|
||||
}
|
||||
|
||||
// Setup the Journal
|
||||
if( useQuickJournal ) {
|
||||
|
@ -64,17 +61,6 @@ public class DefaultPersistenceAdapterFactory implements PersistenceAdapterFacto
|
|||
}
|
||||
}
|
||||
|
||||
public File getDataDirectory() {
|
||||
if( dataDirectory==null ) {
|
||||
dataDirectory = new File("activemq-data");
|
||||
}
|
||||
return dataDirectory;
|
||||
}
|
||||
|
||||
public void setDataDirectory(File dataDirectory) {
|
||||
this.dataDirectory = dataDirectory;
|
||||
}
|
||||
|
||||
public int getJournalLogFiles() {
|
||||
return journalLogFiles;
|
||||
}
|
||||
|
@ -91,17 +77,6 @@ public class DefaultPersistenceAdapterFactory implements PersistenceAdapterFacto
|
|||
this.journalLogFileSize = journalLogFileSize;
|
||||
}
|
||||
|
||||
public DataSource getDataSource() throws IOException {
|
||||
if (dataSource == null) {
|
||||
dataSource = createDataSource();
|
||||
}
|
||||
return dataSource;
|
||||
}
|
||||
|
||||
public void setDataSource(DataSource dataSource) {
|
||||
this.dataSource = dataSource;
|
||||
}
|
||||
|
||||
public JDBCPersistenceAdapter getJdbcAdapter() {
|
||||
return jdbcPersistenceAdapter;
|
||||
}
|
||||
|
@ -175,21 +150,6 @@ public class DefaultPersistenceAdapterFactory implements PersistenceAdapterFacto
|
|||
jdbcPersistenceAdapter.setStatements(statements);
|
||||
}
|
||||
|
||||
// Implementation methods
|
||||
// -------------------------------------------------------------------------
|
||||
protected DataSource createDataSource() throws IOException {
|
||||
|
||||
// Setup the Derby datasource.
|
||||
System.setProperty("derby.system.home", getDataDirectory().getCanonicalPath());
|
||||
System.setProperty("derby.storage.fileSyncTransactionLog", "true");
|
||||
System.setProperty("derby.storage.pageCacheSize", "100");
|
||||
|
||||
final EmbeddedDataSource ds = new EmbeddedDataSource();
|
||||
ds.setDatabaseName("derbydb");
|
||||
ds.setCreateDatabase("create");
|
||||
return ds;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws IOException
|
||||
*/
|
||||
|
|
|
@ -0,0 +1,82 @@
|
|||
/**
|
||||
*
|
||||
* Copyright 2005-2006 The Apache Software Foundation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.activemq.store.jdbc;
|
||||
|
||||
import org.apache.derby.jdbc.EmbeddedDataSource;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* A helper class which provides a factory method to create a default
|
||||
* {@link DataSource) if one is not provided.
|
||||
*
|
||||
* @version $Revision$
|
||||
*/
|
||||
public class DataSourceSupport {
|
||||
|
||||
private File dataDirectory;
|
||||
private DataSource dataSource;
|
||||
|
||||
public DataSourceSupport() {
|
||||
}
|
||||
|
||||
public DataSourceSupport(DataSource dataSource) {
|
||||
this.dataSource = dataSource;
|
||||
}
|
||||
|
||||
public File getDataDirectory() {
|
||||
if (dataDirectory == null) {
|
||||
dataDirectory = new File("activemq-data");
|
||||
}
|
||||
return dataDirectory;
|
||||
}
|
||||
|
||||
public void setDataDirectory(File dataDirectory) {
|
||||
this.dataDirectory = dataDirectory;
|
||||
}
|
||||
|
||||
public DataSource getDataSource() throws IOException {
|
||||
if (dataSource == null) {
|
||||
dataSource = createDataSource();
|
||||
if (dataSource == null) {
|
||||
throw new IllegalArgumentException("No dataSource property has been configured");
|
||||
}
|
||||
}
|
||||
return dataSource;
|
||||
}
|
||||
|
||||
public void setDataSource(DataSource dataSource) {
|
||||
this.dataSource = dataSource;
|
||||
}
|
||||
|
||||
protected DataSource createDataSource() throws IOException {
|
||||
|
||||
// Setup the Derby datasource.
|
||||
System.setProperty("derby.system.home", getDataDirectory().getCanonicalPath());
|
||||
System.setProperty("derby.storage.fileSyncTransactionLog", "true");
|
||||
System.setProperty("derby.storage.pageCacheSize", "100");
|
||||
|
||||
final EmbeddedDataSource ds = new EmbeddedDataSource();
|
||||
ds.setDatabaseName("derbydb");
|
||||
ds.setCreateDatabase("create");
|
||||
return ds;
|
||||
}
|
||||
|
||||
}
|
|
@ -57,13 +57,12 @@ import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
|
|||
*
|
||||
* @version $Revision: 1.9 $
|
||||
*/
|
||||
public class JDBCPersistenceAdapter implements PersistenceAdapter {
|
||||
public class JDBCPersistenceAdapter extends DataSourceSupport implements PersistenceAdapter {
|
||||
|
||||
private static final Log log = LogFactory.getLog(JDBCPersistenceAdapter.class);
|
||||
private static FactoryFinder factoryFinder = new FactoryFinder("META-INF/services/org/apache/activemq/store/jdbc/");
|
||||
|
||||
private WireFormat wireFormat = new OpenWireFormat();
|
||||
private DataSource dataSource;
|
||||
private Statements statements;
|
||||
private JDBCAdapter adapter;
|
||||
private MemoryTransactionStore transactionStore;
|
||||
|
@ -76,24 +75,31 @@ public class JDBCPersistenceAdapter implements PersistenceAdapter {
|
|||
}
|
||||
|
||||
public JDBCPersistenceAdapter(DataSource ds, WireFormat wireFormat) {
|
||||
this.dataSource = ds;
|
||||
super(ds);
|
||||
this.wireFormat = wireFormat;
|
||||
}
|
||||
|
||||
public Set getDestinations() {
|
||||
// Get a connection and insert the message into the DB.
|
||||
TransactionContext c = getTransactionContext();
|
||||
TransactionContext c = null;
|
||||
try {
|
||||
c = getTransactionContext();
|
||||
return getAdapter().doGetDestinations(c);
|
||||
} catch (IOException e) {
|
||||
}
|
||||
catch (IOException e) {
|
||||
return Collections.EMPTY_SET;
|
||||
} catch (SQLException e) {
|
||||
JDBCPersistenceAdapter.log("JDBC Failure: ",e);
|
||||
}
|
||||
catch (SQLException e) {
|
||||
JDBCPersistenceAdapter.log("JDBC Failure: ", e);
|
||||
return Collections.EMPTY_SET;
|
||||
} finally {
|
||||
try {
|
||||
c.close();
|
||||
} catch (Throwable e) {
|
||||
}
|
||||
finally {
|
||||
if (c != null) {
|
||||
try {
|
||||
c.close();
|
||||
}
|
||||
catch (Throwable e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -172,20 +178,26 @@ public class JDBCPersistenceAdapter implements PersistenceAdapter {
|
|||
}
|
||||
|
||||
public void cleanup() {
|
||||
TransactionContext c = getTransactionContext();
|
||||
TransactionContext c = null;
|
||||
try {
|
||||
log.debug("Cleaning up old messages.");
|
||||
c = getTransactionContext();
|
||||
getAdapter().doDeleteOldMessages(c);
|
||||
} catch (IOException e) {
|
||||
}
|
||||
catch (IOException e) {
|
||||
log.warn("Old message cleanup failed due to: " + e, e);
|
||||
} catch (SQLException e) {
|
||||
}
|
||||
catch (SQLException e) {
|
||||
log.warn("Old message cleanup failed due to: " + e);
|
||||
JDBCPersistenceAdapter.log("Failure Details: ",e);
|
||||
} finally {
|
||||
try {
|
||||
c.close();
|
||||
} catch (Throwable e) {
|
||||
JDBCPersistenceAdapter.log("Failure Details: ", e);
|
||||
}
|
||||
finally {
|
||||
if (c != null) {
|
||||
try {
|
||||
c.close();
|
||||
}
|
||||
catch (Throwable e) {
|
||||
}
|
||||
}
|
||||
log.debug("Cleanup done.");
|
||||
}
|
||||
|
@ -260,14 +272,6 @@ public class JDBCPersistenceAdapter implements PersistenceAdapter {
|
|||
this.adapter.setStatements(getStatements());
|
||||
}
|
||||
|
||||
public DataSource getDataSource() {
|
||||
return dataSource;
|
||||
}
|
||||
|
||||
public void setDataSource(DataSource dataSource) {
|
||||
this.dataSource = dataSource;
|
||||
}
|
||||
|
||||
public WireFormat getWireFormat() {
|
||||
return wireFormat;
|
||||
}
|
||||
|
@ -276,24 +280,21 @@ public class JDBCPersistenceAdapter implements PersistenceAdapter {
|
|||
this.wireFormat = wireFormat;
|
||||
}
|
||||
|
||||
public TransactionContext getTransactionContext(ConnectionContext context) {
|
||||
public TransactionContext getTransactionContext(ConnectionContext context) throws IOException {
|
||||
if (context == null) {
|
||||
return getTransactionContext();
|
||||
} else {
|
||||
TransactionContext answer = (TransactionContext) context.getLongTermStoreContext();
|
||||
if (answer == null) {
|
||||
answer = new TransactionContext(dataSource);
|
||||
answer = new TransactionContext(getDataSource());
|
||||
context.setLongTermStoreContext(answer);
|
||||
}
|
||||
return answer;
|
||||
}
|
||||
}
|
||||
|
||||
public TransactionContext getTransactionContext() {
|
||||
if (dataSource == null) {
|
||||
throw new IllegalArgumentException("No dataSource property has been configured");
|
||||
}
|
||||
return new TransactionContext(dataSource);
|
||||
public TransactionContext getTransactionContext() throws IOException {
|
||||
return new TransactionContext(getDataSource());
|
||||
}
|
||||
|
||||
public void beginTransaction(ConnectionContext context) throws IOException {
|
||||
|
|
Loading…
Reference in New Issue