Skip to content

Commit 87f01b2

Browse files
committed
[AMQ-9806] Add H2 database support
1 parent 3b4e7cb commit 87f01b2

File tree

7 files changed

+162
-2
lines changed

7 files changed

+162
-2
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.activemq.store.jdbc.adapter;
18+
19+
import org.apache.activemq.store.jdbc.Statements;
20+
21+
/**
22+
*
23+
* @org.apache.xbean.XBean element="h2-jdbc-adapter"
24+
*/
25+
public class H2JDBCAdapter extends BytesJDBCAdapter {
26+
27+
@Override
28+
public void setStatements(Statements statements) {
29+
statements.setBinaryDataType("BLOB");
30+
super.setStatements(statements);
31+
}
32+
33+
@Override
34+
public String limitQuery(String query) {
35+
return query + " LIMIT " + getMaxRows();
36+
}
37+
38+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
## ---------------------------------------------------------------------------
2+
## Licensed to the Apache Software Foundation (ASF) under one or more
3+
## contributor license agreements. See the NOTICE file distributed with
4+
## this work for additional information regarding copyright ownership.
5+
## The ASF licenses this file to You under the Apache License, Version 2.0
6+
## (the "License"); you may not use this file except in compliance with
7+
## the License. You may obtain a copy of the License at
8+
##
9+
## http://www.apache.org/licenses/LICENSE-2.0
10+
##
11+
## Unless required by applicable law or agreed to in writing, software
12+
## distributed under the License is distributed on an "AS IS" BASIS,
13+
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
## See the License for the specific language governing permissions and
15+
## limitations under the License.
16+
## ---------------------------------------------------------------------------
17+
class=org.apache.activemq.store.jdbc.adapter.H2JDBCAdapter

activemq-unit-tests/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,12 @@
128128
<groupId>org.springframework</groupId>
129129
<artifactId>spring-context</artifactId>
130130
</dependency>
131+
<dependency>
132+
<groupId>com.h2database</groupId>
133+
<artifactId>h2</artifactId>
134+
<scope>test</scope>
135+
<optional>true</optional>
136+
</dependency>
131137
<dependency>
132138
<groupId>org.apache.derby</groupId>
133139
<artifactId>derby</artifactId>

activemq-unit-tests/src/test/java/org/apache/activemq/store/jdbc/JDBCStoreBrokerTest.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,28 @@
2222
import org.apache.activemq.broker.BrokerTest;
2323
import org.apache.derby.jdbc.EmbeddedDataSource;
2424

25+
import javax.sql.DataSource;
26+
2527
public class JDBCStoreBrokerTest extends BrokerTest {
2628

29+
protected void configureJDBCPersistenceAdapter(JDBCPersistenceAdapter jdbc, final String name) throws Exception {
30+
31+
}
32+
33+
@Override
2734
protected BrokerService createBroker() throws Exception {
2835
BrokerService broker = new BrokerService();
29-
JDBCPersistenceAdapter jdbc = new JDBCPersistenceAdapter();
36+
var jdbc = new JDBCPersistenceAdapter();
37+
configureJDBCPersistenceAdapter(jdbc, "JDBCStoreBrokerTest");
3038
jdbc.deleteAllMessages();
3139
broker.setPersistenceAdapter(jdbc);
3240
return broker;
3341
}
34-
42+
3543
protected BrokerService x_createRestartedBroker() throws Exception {
3644
BrokerService broker = new BrokerService();
45+
var jdbc = new JDBCPersistenceAdapter();
46+
configureJDBCPersistenceAdapter(jdbc, "JDBCStoreBrokerTest");
3747
broker.setPersistenceAdapter(new JDBCPersistenceAdapter());
3848
return broker;
3949
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.activemq.store.jdbc.h2;
18+
19+
import javax.sql.DataSource;
20+
import org.h2.jdbcx.JdbcDataSource;
21+
22+
import java.io.IOException;
23+
24+
public class H2DB {
25+
26+
public static DataSource createDataSource(String db) throws IOException {
27+
var ds = new JdbcDataSource();
28+
ds.setURL(createURL(db));
29+
ds.setUser(getUser());
30+
ds.setPassword(getPassword());
31+
return ds;
32+
}
33+
34+
public static String createURL(String db) {
35+
return "jdbc:h2:./target/h2-db/" + db + "/" + db + "-h2.db;DB_CLOSE_DELAY=-1";
36+
}
37+
38+
public static String getUser() {
39+
return "sa";
40+
}
41+
42+
public static String getPassword() {
43+
return "";
44+
}
45+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.activemq.store.jdbc.h2;
18+
19+
import junit.framework.Test;
20+
import org.apache.activemq.store.jdbc.JDBCPersistenceAdapter;
21+
import org.apache.activemq.store.jdbc.JDBCStoreBrokerTest;
22+
import org.apache.activemq.store.jdbc.adapter.H2JDBCAdapter;
23+
24+
import javax.sql.DataSource;
25+
26+
public class H2JDBCStoreBrokerTest extends JDBCStoreBrokerTest {
27+
28+
@Override
29+
protected void configureJDBCPersistenceAdapter(final JDBCPersistenceAdapter jdbc, String dbname) throws Exception {
30+
jdbc.setDataSource(H2DB.createDataSource("H2JDBCStoreBrokerTest"));
31+
jdbc.setAdapter(new H2JDBCAdapter());
32+
}
33+
34+
public static Test suite() {
35+
return suite(H2JDBCStoreBrokerTest.class);
36+
}
37+
}

pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
<jmock-version>2.13.1</jmock-version>
7575
<jolokia-version>2.4.3</jolokia-version>
7676
<junit-version>4.13.2</junit-version>
77+
<h2-version>2.4.240</h2-version>
7778
<hamcrest-version>1.3</hamcrest-version>
7879
<karaf-version>4.3.7</karaf-version>
7980
<log4j-version>2.25.3</log4j-version>
@@ -766,6 +767,12 @@
766767
<version>${org-apache-derby-version}</version>
767768
</dependency>
768769

770+
<dependency>
771+
<groupId>com.h2database</groupId>
772+
<artifactId>h2</artifactId>
773+
<version>${h2-version}</version>
774+
</dependency>
775+
769776
<dependency>
770777
<groupId>org.apache.commons</groupId>
771778
<artifactId>commons-dbcp2</artifactId>

0 commit comments

Comments
 (0)