| Method from org.jboss.ejb.txtimer.GeneralPurposeDatabasePersistencePlugin Detail: |
public void clearTimers() throws SQLException {
Connection con = null;
PreparedStatement st = null;
ResultSet rs = null;
try
{
con = ds.getConnection();
st = con.prepareStatement("delete from " + getTableName());
st.executeUpdate();
}
finally
{
JDBCUtil.safeClose(rs);
JDBCUtil.safeClose(st);
JDBCUtil.safeClose(con);
}
}
Clear all persisted timers |
public void createTableIfNotExists() throws SQLException {
Connection con = null;
Statement st = null;
try
{
JDBCTypeMappingMetaData typeMapping = (JDBCTypeMappingMetaData)server.getAttribute(metaDataName, "TypeMappingMetaData");
if (typeMapping == null)
throw new IllegalStateException("Cannot obtain type mapping from: " + metaDataName);
JDBCMappingMetaData objectMetaData = typeMapping.getTypeMappingMetaData(Object.class);
binarySqlType = objectMetaData.getJdbcType();
if (!SQLUtil.tableExists(getTableName(), ds))
{
con = ds.getConnection();
String dateType = typeMapping.getTypeMappingMetaData(Timestamp.class).getSqlType();
String longType = typeMapping.getTypeMappingMetaData(Long.class).getSqlType();
String objectType = objectMetaData.getSqlType();
// The create table DDL
StringBuffer createTableDDL = new StringBuffer("create table " + getTableName() + " (" +
" " + getColumnTimerID() + " varchar(80) not null," +
" " + getColumnTargetID() + " varchar(250) not null," +
" " + getColumnInitialDate() + " " + dateType + " not null," +
" " + getColumnTimerInterval() + " " + longType + "," +
" " + getColumnInstancePK() + " " + objectType + "," +
" " + getColumnInfo() + " " + objectType + ", ");
// Add the primary key constraint using the pk-constraint-template
JDBCFunctionMappingMetaData pkConstraint = typeMapping.getPkConstraintTemplate();
String[] templateParams = new String[] {
getTableName() + "_PK",
getColumnTimerID() + ", " + getColumnTargetID()
};
pkConstraint.getFunctionSql(templateParams, createTableDDL);
// Complete the statement
createTableDDL.append(" )");
log.debug("Executing DDL: " + createTableDDL);
st = con.createStatement();
st.executeUpdate(createTableDDL.toString());
}
}
catch (SQLException e)
{
throw e;
}
catch (Exception e)
{
log.error("Cannot create timer table", e);
}
finally
{
JDBCUtil.safeClose(st);
JDBCUtil.safeClose(con);
}
}
Create the timer table if it does not exist already |
public void deleteTimer(String timerId,
TimedObjectId timedObjectId) throws SQLException {
Connection con = null;
PreparedStatement st = null;
ResultSet rs = null;
try
{
con = ds.getConnection();
String sql = "delete from " + getTableName() + " where " + getColumnTimerID() + "=? and " + getColumnTargetID() + "=?";
st = con.prepareStatement(sql);
st.setString(1, timerId);
st.setString(2, timedObjectId.toString());
int rows = st.executeUpdate();
// This appears when a timer is created & persisted inside a tx,
// but then the tx is rolled back, at which point we go back
// to remove the entry, but no entry is found.
// Is this because we are "enlisting" the datasource in the tx, too?
if (rows != 1)
{
log.debug("Unable to remove timer for: " + timerId);
}
}
finally
{
JDBCUtil.safeClose(rs);
JDBCUtil.safeClose(st);
JDBCUtil.safeClose(con);
}
}
|
protected Object deserialize(byte[] bytes) {
if (bytes == null)
return null;
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
try
{
// Use an ObjectInputStream that instantiates objects
// using the Thread Context ClassLoader (TCL)
ObjectInputStream oos = new MarshalledValueInputStream(bais);
return oos.readObject();
}
catch (Exception e)
{
log.error("Cannot deserialize", e);
return null;
}
}
|
protected Object deserialize(InputStream input) {
if (input == null)
return null;
byte[] barr = new byte[1024];
ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
try
{
for (int b = 0; (b = input.read(barr)) > 0;)
{
baos.write(barr, 0, b);
}
return deserialize(baos.toByteArray());
}
catch (Exception e)
{
log.error("Cannot deserialize", e);
return null;
}
}
|
public String getColumnInfo() {
return "INFO";
}
|
public String getColumnInitialDate() {
return "INITIALDATE";
}
Get the initial date column name |
public String getColumnInstancePK() {
return "INSTANCEPK";
}
Get the instance PK column name |
public String getColumnTargetID() {
return "TARGETID";
}
Get the target ID column name |
public String getColumnTimerID() {
return "TIMERID";
}
Get the timer ID column name |
public String getColumnTimerInterval() {
// Note 'INTERVAL' is a reserved word in MySQL
return "TIMERINTERVAL";
}
Get the timer interval column name |
public String getTableName() {
return tableName;
}
|
public void init(MBeanServer server,
ObjectName dataSourceName) throws SQLException {
this.server = server;
this.dataSourceName = dataSourceName;
// Get the DataSource from JNDI
try
{
String dsJndiTx = (String)server.getAttribute(dataSourceName, "BindName");
ds = (DataSource)new InitialContext().lookup(dsJndiTx);
}
catch (Exception e)
{
throw new SQLException("Failed to lookup data source: " + dataSourceName);
}
// Get the DataSource meta data
String dsName = dataSourceName.getKeyProperty("name");
metaDataName = ObjectNameFactory.create("jboss.jdbc:datasource=" + dsName + ",service=metadata");
if (this.server.isRegistered(metaDataName) == false)
throw new IllegalStateException("Cannot find datasource meta data: " + metaDataName);
}
|
public void init(MBeanServer server,
ObjectName dataSource,
String tableName) throws SQLException {
if (tableName == null)
throw new IllegalArgumentException("Timers tableName is null");
if (tableName.length() == 0)
throw new IllegalArgumentException("Timers tableName is empty");
this.tableName = tableName;
init(server, dataSource);
}
Initialize the plugin and set also the timers tablename |
public void insertTimer(String timerId,
TimedObjectId timedObjectId,
Date initialExpiration,
long intervalDuration,
Serializable info) throws SQLException {
Connection con = null;
PreparedStatement st = null;
try
{
con = ds.getConnection();
String sql = "insert into " + getTableName() + " " +
"(" + getColumnTimerID() + "," + getColumnTargetID() + "," + getColumnInitialDate() + "," + getColumnTimerInterval() + "," + getColumnInstancePK() + "," + getColumnInfo() + ") " +
"values (?,?,?,?,?,?)";
st = con.prepareStatement(sql);
st.setString(1, timerId);
st.setString(2, timedObjectId.toString());
st.setTimestamp(3, new Timestamp(initialExpiration.getTime()));
st.setLong(4, intervalDuration);
byte[] bytes = serialize(timedObjectId.getInstancePk());
if(bytes == null)
{
st.setNull(5, binarySqlType);
}
else
{
st.setBytes(5, bytes);
}
bytes = serialize(info);
if(bytes == null)
{
st.setNull(6, binarySqlType);
}
else
{
st.setBytes(6, bytes);
}
int rows = st.executeUpdate();
if (rows != 1)
log.error("Unable to insert timer for: " + timedObjectId);
}
finally
{
JDBCUtil.safeClose(st);
JDBCUtil.safeClose(con);
}
}
|
public List selectTimers(ObjectName containerId) throws SQLException {
Connection con = null;
Statement st = null;
ResultSet rs = null;
try
{
con = ds.getConnection();
List list = new ArrayList();
st = con.createStatement();
rs = st.executeQuery("select * from " + getTableName());
while (rs.next())
{
String timerId = rs.getString(getColumnTimerID());
TimedObjectId targetId = TimedObjectId.parse(rs.getString(getColumnTargetID()));
// add this handle to the returned list, if a null containerId was used
// or the containerId filter matches
if (containerId == null || containerId.equals(targetId.getContainerId()))
{
Date initialDate = rs.getTimestamp(getColumnInitialDate());
long interval = rs.getLong(getColumnTimerInterval());
Serializable pKey = (Serializable)deserialize(rs.getBytes(getColumnInstancePK()));
Serializable info = null;
try
{
info = (Serializable)deserialize(rs.getBytes(getColumnInfo()));
}
catch (Exception e)
{
// may happen if listing all handles (containerId is null)
// with a stored custom info object coming from a scoped
// deployment.
log.warn("Cannot deserialize custom info object", e);
}
// is this really needed? targetId encapsulates pKey as well!
targetId = new TimedObjectId(targetId.getContainerId(), pKey);
TimerHandleImpl handle = new TimerHandleImpl(timerId, targetId, initialDate, interval, info);
list.add(handle);
}
}
return list;
}
finally
{
JDBCUtil.safeClose(rs);
JDBCUtil.safeClose(st);
JDBCUtil.safeClose(con);
}
}
Select a list of currently persisted timer handles |
protected byte[] serialize(Object obj) {
if (obj == null)
return null;
ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
try
{
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(obj);
oos.close();
}
catch (IOException e)
{
log.error("Cannot serialize: " + obj, e);
}
return baos.toByteArray();
}
|