-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding Derby for dev purposes plus formmating
- Loading branch information
1 parent
aaff654
commit 5f40713
Showing
26 changed files
with
1,039 additions
and
984 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,13 @@ | ||
package ar.cpfw.jqueue; | ||
|
||
public class JQueueException extends RuntimeException { | ||
private static final long serialVersionUID = 1L; | ||
|
||
private static final long serialVersionUID = 1L; | ||
public JQueueException(final String msg) { | ||
super(msg); | ||
} | ||
|
||
public JQueueException(final String msg) { | ||
super(msg); | ||
} | ||
|
||
public JQueueException(final Exception exception, final String msg) { | ||
super(msg, exception); | ||
} | ||
public JQueueException(final Exception exception, final String msg) { | ||
super(msg, exception); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,28 @@ | ||
package ar.cpfw.jqueue.push; | ||
|
||
import java.sql.Connection; | ||
import javax.sql.DataSource; | ||
import java.sql.Connection; | ||
|
||
public interface JTxQueue { | ||
|
||
void push(String data); | ||
static JTxQueue queue(DataSource dataSource, String tableName) { | ||
return new JdbcJQueue(dataSource, tableName); | ||
} | ||
|
||
JTxQueue channel(String channelName); | ||
static JTxQueue queue(Connection conn, String tableName) { | ||
return new JdbcJQueue(conn, tableName); | ||
} | ||
|
||
static JTxQueue queue(DataSource dataSource, String tableName) { | ||
return new JdbcJQueue(dataSource, tableName); | ||
} | ||
static JTxQueue queue(DataSource dataSource) { | ||
return new JdbcJQueue(dataSource, null); | ||
} | ||
|
||
static JTxQueue queue(Connection conn, String tableName) { | ||
return new JdbcJQueue(conn, tableName); | ||
} | ||
static JTxQueue queue(Connection conn) { | ||
return new JdbcJQueue(conn, null); | ||
} | ||
|
||
static JTxQueue queue(DataSource dataSource) { | ||
return new JdbcJQueue(dataSource, null); | ||
} | ||
void push(String data); | ||
|
||
static JTxQueue queue(Connection conn) { | ||
return new JdbcJQueue(conn, null); | ||
} | ||
JTxQueue channel(String channelName); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,75 @@ | ||
package ar.cpfw.jqueue.push; | ||
|
||
import ar.cpfw.jqueue.JQueueException; | ||
|
||
import javax.sql.DataSource; | ||
import java.sql.Connection; | ||
import java.sql.PreparedStatement; | ||
import java.sql.SQLException; | ||
import java.sql.Timestamp; | ||
import java.time.LocalDateTime; | ||
import java.util.Objects; | ||
import javax.sql.DataSource; | ||
import ar.cpfw.jqueue.JQueueException; | ||
|
||
class JdbcJQueue implements JTxQueue { | ||
final class JdbcJQueue implements JTxQueue { | ||
|
||
private static final int PUSHEDAT_COLUMN = 3; | ||
private static final int DATA_COLUMN = 2; | ||
private static final int CHANNEL_COLUMN = 1; | ||
private static final String DS_IS_NECESARY = | ||
"An instance of javax.sql.DataSource is necesary"; | ||
private Connection connection; | ||
private String channel; | ||
private static final String DEFAULT_CHANNEL = "default"; | ||
private static final String QUEUE_TABLE_NAME = "ar_cpfw_jqueue"; | ||
private final String databaseTableName; | ||
private static final int PUSHEDAT_COLUMN = 3; | ||
private static final int DATA_COLUMN = 2; | ||
private static final int CHANNEL_COLUMN = 1; | ||
private static final String DS_IS_NECESARY = | ||
"An instance of javax.sql.DataSource is necesary"; | ||
private static final String DEFAULT_CHANNEL = "default"; | ||
private static final String QUEUE_TABLE_NAME = "ar_cpfw_jqueue"; | ||
private final Connection connection; | ||
private final String databaseTableName; | ||
private String channel; | ||
|
||
public JdbcJQueue(final DataSource dataSource, final String tableName) { | ||
Objects.requireNonNull(dataSource, DS_IS_NECESARY); | ||
this.databaseTableName = tableName; | ||
try { | ||
this.connection = dataSource.getConnection(); | ||
} catch (SQLException e) { | ||
throw new JQueueException(e, | ||
"java.sql.Connection could not be obtained from the dataSource"); | ||
public JdbcJQueue(final DataSource dataSource, final String tableName) { | ||
Objects.requireNonNull(dataSource, DS_IS_NECESARY); | ||
this.databaseTableName = tableName; | ||
try { | ||
this.connection = dataSource.getConnection(); | ||
} catch (SQLException e) { | ||
throw new JQueueException(e, | ||
"java.sql.Connection could not be obtained from the dataSource"); | ||
} | ||
} | ||
} | ||
|
||
public JdbcJQueue(final Connection conn, final String tableName) { | ||
Objects.requireNonNull(conn, DS_IS_NECESARY); | ||
public JdbcJQueue(final Connection conn, final String tableName) { | ||
Objects.requireNonNull(conn, DS_IS_NECESARY); | ||
|
||
this.connection = conn; | ||
this.databaseTableName = tableName; | ||
} | ||
this.connection = conn; | ||
this.databaseTableName = tableName; | ||
} | ||
|
||
@Override | ||
public void push(final String data) { | ||
Objects.requireNonNull(data, "data must not be null"); | ||
@Override | ||
public void push(final String data) { | ||
Objects.requireNonNull(data, "data must not be null"); | ||
|
||
final var channelName = | ||
this.channel != null ? this.channel : DEFAULT_CHANNEL; | ||
final var table = this.databaseTableName != null ? this.databaseTableName | ||
: QUEUE_TABLE_NAME; | ||
final var channelName = | ||
this.channel != null ? this.channel : DEFAULT_CHANNEL; | ||
final var table = this.databaseTableName != null ? this.databaseTableName | ||
: QUEUE_TABLE_NAME; | ||
|
||
try { | ||
final PreparedStatement st = | ||
this.connection.prepareStatement("insert into " + table | ||
+ " (channel, data, attempt, delay, pushed_at) " | ||
+ "values (?, ?, null, 0, ?)"); | ||
try { | ||
final PreparedStatement st = | ||
this.connection.prepareStatement("insert into " + table | ||
+ " (channel, data, attempt, delay, pushed_at) " | ||
+ "values (?, ?, null, 0, ?)"); | ||
|
||
st.setString(CHANNEL_COLUMN, channelName); | ||
st.setString(DATA_COLUMN, data); | ||
st.setTimestamp(PUSHEDAT_COLUMN, Timestamp.valueOf(LocalDateTime.now())); | ||
st.executeUpdate(); | ||
st.setString(CHANNEL_COLUMN, channelName); | ||
st.setString(DATA_COLUMN, data); | ||
st.setTimestamp(PUSHEDAT_COLUMN, Timestamp.valueOf(LocalDateTime.now())); | ||
st.executeUpdate(); | ||
|
||
} catch (SQLException e) { | ||
throw new JQueueException(e, "push cannot be done"); | ||
} catch (SQLException e) { | ||
throw new JQueueException(e, "push cannot be done"); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public JTxQueue channel(final String channelName) { | ||
this.channel = channelName; | ||
return this; | ||
} | ||
@Override | ||
public JTxQueue channel(final String channelName) { | ||
this.channel = channelName; | ||
return this; | ||
} | ||
|
||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/ar/cpfw/jqueue/runner/DerbyDbQueryBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package ar.cpfw.jqueue.runner; | ||
|
||
class DerbyDbQueryBuilder extends StandardQueryBuilder { | ||
|
||
public DerbyDbQueryBuilder(final String tableName) { | ||
super(tableName); | ||
} | ||
|
||
@Override | ||
protected String calculateDate() { | ||
return "{fn TIMESTAMPADD(SQL_TSI_MINUTE, -delay, ?)}"; | ||
} | ||
|
||
@Override | ||
protected String limitOne() { | ||
return "FETCH FIRST 1 ROWS ONLY"; | ||
} | ||
|
||
@Override | ||
protected String lock() { | ||
//as of version 10.16.1.1 | ||
// Derby does not support for update with an order by | ||
return ""; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.