如何在Java中实现db侦听器 [英] How to implement a db listener in Java
问题描述
我有一个要求,如果一个记录插入一个数据库表,然后自动一个java进程需要执行。什么是实现数据库监听器的最简单的方法?
我有一个Oracle解决方案。你不需要创建自己,因为现在Oracle买了Java,它为它发布了一个监听器。据我所知,这不在内部使用轮询,而是通知推送到Java端(可能基于一些触发器):
public interface oracle.jdbc.dcn.DatabaseChangeListener
extends java.util.EventListener {
void onDatabaseChangeNotification(oracle.jdbc.dcn.DatabaseChangeEvent arg0);
}
你可以像这样实现/ p>
public class DBListener implements DatabaseChangeListener {
private DbChangeNotification toNotify;
public BNSDBListener(DbChangeNotification toNotify){
this.toNotify = toNotify;
}
@Override
public void onDatabaseChangeNotification(oracle.jdbc.dcn.DatabaseChangeEvent e){
synchronized(toNotify){
try {
toNotify.notifyDBChangeEvent(e); // do sth
} catch(Exception ex){
Util.logMessage(CLASSNAME,onDatabaseChangeNotification,
通知对象上的错误,true);
Util.printStackTrace(ex);
Util.systemExit();
}
}
}
}
strong> EDIT:
您可以使用以下类来注册: oracle.jdbc.OracleConnectionWrapper
public class oracle.jdbc.OracleConnectionWrapper implements oracle.jdbc.OracleConnection {...}
假设你在某个地方创建一个方法:
public void registerPushNotification ){
oracle.jdbc.driver.OracleConnection oracleConnection = ...; //连接到db
dbProperties.setProperty(OracleConnection.DCN_NOTIFY_ROWIDS,true);
dbProperties.setProperty(OracleConnection.DCN_QUERY_CHANGE_NOTIFICATION,true);
//这是在db端实际注册的内容
oracle.jdbc.dcn.DatabaseChangeRegistration dbChangeRegistration = oracleConnection.registerDatabaseChangeNotification(dbProperties);
//现在你可以添加我的EDIT之前创建的监听器
listener = new DBListener(this);
dbChangeRegistration.addListener(listener);
//现在你需要添加你想监视的表
语句stmt = oracleConnection.createStatement();
//将语句与注册相关联:
((OracleStatement)stmt).setDatabaseChangeRegistration(dbChangeRegistration); //查找此方法的文档[http://docs.oracle.com/cd/E11882_01/appdev.112/e13995/oracle/jdbc/OracleStatement.html#setDatabaseChangeRegistration_oracle_jdbc_dcn_DatabaseChangeRegistration_]
ResultSet rs = stmt.executeQuery(sql); //你必须执行查询以将其链接到要监视的语句
while(rs.next()){...如果感兴趣的话... do sth与结果...}
//查看正在监视的表
String [] tableNames = dbChangeRegistration.getTables();
for(int i = 0; i< tableNames.length; i ++){
System.out.println(tableNames [i] +已经注册。
}
rs.close();
stmt.close();
}
此示例不包括try-catch子句或任何异常处理。 p>
I have a requirement where if a record is inserted in a db table , then automatically a java process needs to be executed.What is the easiest way to implement a db listener ?
I have a solution for Oracle. You don't need to create your own since now that Oracle bought Java it released a listener for it. As far as I know this does not use polling internally, instead notifications are pushed to the Java side (probably based on some trigger):
public interface oracle.jdbc.dcn.DatabaseChangeListener
extends java.util.EventListener {
void onDatabaseChangeNotification(oracle.jdbc.dcn.DatabaseChangeEvent arg0);
}
And you can implement it like this (this is just a sample):
public class DBListener implements DatabaseChangeListener {
private DbChangeNotification toNotify;
public BNSDBListener(DbChangeNotification toNotify) {
this.toNotify = toNotify;
}
@Override
public void onDatabaseChangeNotification(oracle.jdbc.dcn.DatabaseChangeEvent e) {
synchronized( toNotify ) {
try {
toNotify.notifyDBChangeEvent(e); //do sth
} catch (Exception ex) {
Util.logMessage(CLASSNAME, "onDatabaseChangeNotification",
"Errors on the notifying object.", true);
Util.printStackTrace(ex);
Util.systemExit();
}
}
}
}
EDIT:
You can use the following class to register: oracle.jdbc.OracleConnectionWrapper
public class oracle.jdbc.OracleConnectionWrapper implements oracle.jdbc.OracleConnection {...}
Say you create a method somewhere:
public void registerPushNotification(String sql) {
oracle.jdbc.driver.OracleConnection oracleConnection = ...;//connect to db
dbProperties.setProperty(OracleConnection.DCN_NOTIFY_ROWIDS, "true");
dbProperties.setProperty(OracleConnection.DCN_QUERY_CHANGE_NOTIFICATION, "true");
//this is what does the actual registering on the db end
oracle.jdbc.dcn.DatabaseChangeRegistration dbChangeRegistration= oracleConnection.registerDatabaseChangeNotification(dbProperties);
//now you can add the listener created before my EDIT
listener = new DBListener(this);
dbChangeRegistration.addListener(listener);
//now you need to add whatever tables you want to monitor
Statement stmt = oracleConnection.createStatement();
//associate the statement with the registration:
((OracleStatement) stmt).setDatabaseChangeRegistration(dbChangeRegistration); //look up the documentation to this method [http://docs.oracle.com/cd/E11882_01/appdev.112/e13995/oracle/jdbc/OracleStatement.html#setDatabaseChangeRegistration_oracle_jdbc_dcn_DatabaseChangeRegistration_]
ResultSet rs = stmt.executeQuery(sql); //you have to execute the query to link it to the statement for it to be monitored
while (rs.next()) { ...do sth with the results if interested... }
//see what tables are being monitored
String[] tableNames = dbChangeRegistration.getTables();
for (int i = 0; i < tableNames.length; i++) {
System.out.println(tableNames[i] + " has been registered.");
}
rs.close();
stmt.close();
}
This example does not include try-catch clauses or any exception handling.
这篇关于如何在Java中实现db侦听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!