javaFX : How to periodically load information from db and show it on a Label? - database

I want to execute a method periodically, this method get informations from database it show it into a label, I tried the following code :
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
//update information
miseAjour();
}
}, 0, 2000);
when i run the main program, the background service run also normaly but when the informations changes on db i get this exception:
Exception in thread "Timer-0" java.lang.IllegalStateException: Not on FX application thread; currentThread = Timer-0
And this is the code of method miseAjour :
public void miseAjour(){
try {
dbConnection db = new dbConnection();
Connection connect = db.connectiondb();
connect.setAutoCommit(false);
Statement stmt= connect.createStatement();
ResultSet rs = stmt.executeQuery("SELECT count(*) as nbrAderent FROM gss_aderent ");
int nbrAderent = rs.getInt("nbrAderent");
rs.close();
stmt.close();
connect.commit();
connect.close();
main_nbrAdrTot.setText(nbrAderent + "");
} catch (SQLException ex) {
Logger.getLogger(SimpleController.class.getName()).log(Level.SEVERE, null, ex);
}
}

You can Timer for this, but I would recommend to use the JavaFX provided API called as ScheduledService.
ScheduledService is made to execute the same Task at regular intervals and since it creates a Task internally, there are API which help you to bind the value to the UI controls.
ScheduledService<Object> service = new ScheduledService<Object>() {
protected Task<Object> createTask() {
return new Task<Object>() {
protected Object call() {
// Call the method and update the message
updateMessage(miseAjour());
return object; // Useful in case you want to return data, else null
}
};
}
};
service.setPeriod(Duration.seconds(10)); //Runs every 10 seconds
//bind the service message properties to your Label
label.textProperty().bind(service.messageProperty()); // or use your label -> main_nbrAdrTot
Inside the dbcall method miseAjour, return the value that you have fetched and you want to update the label with :
public String miseAjour(){
String nbrAderent = null;
try {
dbConnection db = new dbConnection();
Connection connect = db.connectiondb();
connect.setAutoCommit(false);
Statement stmt= connect.createStatement();
ResultSet rs = stmt.executeQuery("SELECT count(*) as nbrAderent FROM gss_aderent ");
nbrAderent = String.valueOf(rs.getInt("nbrAderent"));
connect.commit();
} catch (SQLException ex) {
Logger.getLogger(SimpleController.class.getName()).log(Level.SEVERE, null, ex);
}
finally {
rs.close();
stmt.close();
connect.close();
}
return nbrAderent;
}

Finnaly i resolved the problem ,here is the code :
public class TimerServiceApp {
public void start() throws Exception {
TimerService service = new TimerService();
service.setPeriod(Duration.seconds(10));
service.setOnSucceeded(new EventHandler<WorkerStateEvent>() {
#Override
public void handle(WorkerStateEvent t) {
main_nbrAdrTot.setText(t.getSource().getMessage());
}
});
service.start();
}
private class TimerService extends ScheduledService<Integer> {
private final StringProperty nbrTotAderent = new SimpleStringProperty();
public final void setTotalAderentNumber(String value ) {
nbrTotAderent.set(value);
}
public String getTotalAderentNumber() throws SQLException {
String nbrAderent = null;
ResultSet rs=null;
Statement stmt=null;
Connection connect=null;
try {
dbConnection db = new dbConnection();
connect = db.connectiondb();
connect.setAutoCommit(false);
stmt= connect.createStatement();
rs = stmt.executeQuery("SELECT count(*) as nbrAderent FROM gss_aderent ");
nbrAderent = String.valueOf(rs.getInt("nbrAderent"));
connect.commit();
} catch (SQLException ex) {
Logger.getLogger(SimpleController.class.getName()).log(Level.SEVERE, null, ex);
}
finally {
rs.close();
stmt.close();
connect.close();
}
System.out.println(" Total aderent number updated to :" + nbrAderent + " Aderents ");
return nbrAderent;
}
protected Task<Integer> createTask() {
return new Task<Integer>() {
protected Integer call() throws SQLException {
nbrTotAderent.setValue(getTotalAderentNumber());
updateMessage(getTotalAderentNumber());
return Integer.parseInt(getTotalAderentNumber());
}
};
}
}
} `
and i called this service by :
TimerServiceApp s = new TimerServiceApp();
s.start();
i dont know if the solution is optimised but it work :) thank you #ItachiUchiha i took the solution from yout answer in the following link

Related

BaseX parrallel Client

I have client like this :
import org.basex.api.client.ClientSession;
#Slf4j
#Component(value = "baseXAircrewClient")
#DependsOn(value = "baseXAircrewServer")
public class BaseXAircrewClient {
#Value("${basex.server.host}")
private String basexServerHost;
#Value("${basex.server.port}")
private int basexServerPort;
#Value("${basex.admin.password}")
private String basexAdminPassword;
#Getter
private ClientSession session;
#PostConstruct
private void createClient() throws IOException {
log.info("##### Creating BaseX client session {}", basexServerPort);
this.session = new ClientSession(basexServerHost, basexServerPort, UserText.ADMIN, basexAdminPassword);
}
}
It is a singleton injected in a service which run mulitple queries like this :
Query query = client.getSession().query(finalQuery);
return query.execute();
All threads query and share the same session.
With a single thread all is fine but with multiple thread I get some random (and weird) error, like the result of a query to as a result of another.
I feel that I should put a synchronized(){} arround query.execute() or open and close session for each query, or create a pool of session.
But I don't find any documentation how the use the session in parrallel.
Is this implementation fine for multithreading (and my issue is comming from something else) or should I do it differently ?
I ended creating a simple pool by adding removing the client from a ArrayBlockingQueue and it is working nicely :
#PostConstruct
private void createClient() throws IOException {
log.info("##### Creating BaseX client session {}", basexServerPort);
final int poolSize = 5;
this.resources = new ArrayBlockingQueue < ClientSession > (poolSize) {
{
for (int i = 0; i < poolSize; i++) {
add(initClient());
}
}
};
}
private ClientSession initClient() throws IOException {
ClientSession clientSession = new ClientSession(basexServerHost, basexServerPort, UserText.ADMIN, basexAdminPassword);
return clientSession;
}
public Query query(String finalQuery) throws IOException {
ClientSession clientSession = null;
try {
clientSession = resources.take();
Query result = clientSession.query(finalQuery);
return result;
} catch (InterruptedException e) {
log.error("Error during query execution: " + e.getMessage(), e);
} finally {
if (clientSession != null) {
try {
resources.put(clientSession);
} catch (InterruptedException e) {
log.error("Error adding to pool : " + e.getMessage(), e);
}
}
}
return null;
}

How to set a Camel exchangeProperty from a unit test

I have a bunch of unit tests that run camel routes with code like
// setup code here...
String route = "direct:someroute";
try (CamelContext context = new DefaultCamelContext()) {
Object response = runCamelRoute(context, route, ci);
checkResponse(route, response);
}
but this route expects an exchange property to be set before it gets here - how can I set it? CamelContext has a whole lot of methods but I can't seem to find something like:
CamelRoute cr = context.getRoute(route);
cr.getExchange().setProperty("propertyName", "propetyValue");
Here is my camel run method for unit testing, with a bit of extra code for setting up an Oracle connection, etc.
protected Object runCamelRoute(CamelContext context, String route, Object message) throws Exception {
context.addRoutes(new MyRouteBuilder() {
});
setupRegistry(context);
context.start();
FluentProducerTemplate template = context.createFluentProducerTemplate();
template.withBody(message)
.withHeader("hello", "goodbye")
.withProcessor(e -> e.setProperty("propertyName", "propertyValue")) // fail use header instead
.to(route);
try {
Future<Object> future = template.asyncRequest();
return future.get();
}
catch(Exception ex) {
System.out.println(route + " " + ex.getClass().getCanonicleName() + " " + ex.getMessage());
throw ex;
}
finally {
template.stop();
context.stop();
}
}
private void setupRegistry(CamelContext context) {
DataSource ds = DataSourceHelper.createConnectionPoolDev();
context.getRegistry().bind("dataSource", ds);
context.getRegistry().bind("Transformer", new Transformer());
}
public static OracleDataSource createConnectionPoolDev() {
try {
OracleDataSource ds = new OracleDataSource();
ds.setConnectionCacheName("oraCache");
ds.setURL("jdbc:oracle:thin:#//cluster:1521/server.domain.ca");
ds.setUser("user");
ds.setPassword("pass");
return ds;
}
catch (Exception ex) {
logger.error("Failed to create connection to the database " + ex.getMessage());
}
return null;
}
Something like this may be ?
context.createFluentProducerTemplate()
.withBody(...)
.withHeader(..., ...)
.withProcessor( e -> e.setProperty(propertyName, propertyValue) )
.to("direct:someroute")
.send();

setValue of a Non-editable comboBox

Can someone share some sample codes on how to set the value of a non-editable Combobox? It is similar to this, but when I tried to insert it in my code. It returns a null value
Code
ObservableList<City> data = FXCollections.observableArrayList();
data = AddressGetWay.getCityByProvince("Batanes")
cmbCity.setItems(data); // set the items from the database
cmbCity.setConverter(new StringConverter<City>() {
#Override
public String toString(City object) {
return object.getCityName();
}
#Override
public City fromString(String string) {
return cmbCity.getItems().stream().filter(ap
-> ap.getCityName().equals(string)).findFirst().orElse(null);
}
});
cmbCity.valueProperty().addListener(
(ObservableValue<? extends City> observable, City oldValue, City newVal) -> {
if (newVal != null) {
//
}
}
);
// TODO: get the data stored in the database (Column City)
// and set the value of the ComboBox.
Predicate<City> predicate = city -> city.getCityName() == "Itbayat"; // Let's assume that the data stored in the database is "Itbayat"
Optional<City> opt = data.stream().filter(predicate).findAny();
cmbCity.getSelectionModel().select(opt.orElse(null)); // the ComboBox value should be "Itbayat".
I'm using a Singleton Class (correct me if I'm wrong) to retrieve the data from the database
public class AddressGetWay {
static Connection con; //connect to the database
static PreparedStatement pst = null;
static ResultSet rs = null;
public static ObservableList<City> getCityByProvince(String prov) {
ObservableList<City> listData = FXCollections.observableArrayList();
String sql = "SELECT pk_cit_id, cit_nm, zip_code FROM city_mun WHERE prov_code = (SELECT prov_code FROM provinces WHERE prov_nm = ?)";
try {
pst = con.prepareStatement(sql);
pst.setString(1, prov);
rs = pst.executeQuery();
while (rs.next()) {
listData.add(new City(
rs.getInt(1),
rs.getString(2),
rs.getInt(3)
));
}
} catch (SQLException ex) {
Logger.getLogger(AddressGetWay.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
pst.close();
rs.close();
} catch (SQLException ex) {
Logger.getLogger(AddressGetWay.class.getName()).log(Level.SEVERE, null, ex);
}
}
return listData;
}
}
This should be my desired Output
But I got this output using the above code
As for my naming conventions. I tried to do this if there's something I need to correct, feel free to pinpoint.
It seems that you are having issue on this code block,
ObservableList<City> data = AddressGetWay.getCityByProvince("Batanes");
//....
Predicate<City> predicate = city -> city.getCityName() == "Itbayat";
Optional<City> opt = data.stream().filter(predicate).findAny();
cmbCity.getSelectionModel().select(opt.orElse(null));
I suspect some loop-holes that is overcoming the null-value.
As you retrieving data through province Batanes i.e. ObservableList<City> data = AddressGetWay.getCityByProvince("Batanes"), maybe it's not retrieving any data.
Your predicate is comparing cityName Itbayat, which may not be getting matched with the retrieved data.
Please check the database value for the city table as to compare those data as well
AddressGateWay
public class AddressGateWay {
private static Connection connection;
private static PreparedStatement statement = null;
private static ResultSet result = null;
static {
connection = loadConnection();
}
public static ObservableList<City> getCityByProvince(String prov) {
ObservableList<City> listData = FXCollections.observableArrayList();
String sql = "SELECT pk_cit_id, cit_nm, zip_code FROM city_mun WHERE prov_code = (SELECT prov_code FROM provinces WHERE prov_nm = ?)";
try {
statement = connection.prepareStatement(sql);
statement.setString(1, prov);
result = statement.executeQuery();
while (result.next()) {
listData.add(new City(
result.getInt(1),
result.getString(2)
));
}
} catch (SQLException ex) {
Logger.getLogger(AddressGateWay.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
statement.close();
result.close();
} catch (SQLException ex) {
Logger.getLogger(AddressGateWay.class.getName()).log(Level.SEVERE, null, ex);
}
}
return listData;
}
// for editing
public static ObservableList<Address> selectedItem(int item) {
ObservableList<Address> listData = FXCollections.observableArrayList();
String sql = "SELECT prov_nm, city_nm FROM emp_address WHERE pk_address_id = ?";
try {
statement = connection.prepareStatement(sql);
statement.setString(1, item);
result = statement.executeQuery();
if (result.next()) {
listData.add(new Address(
result.getString(1),
result.getString(2)
));
}
} catch (SQLException ex) {
Logger.getLogger(AddressGateWay.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
statement.close();
result.close();
} catch (SQLException ex) {
Logger.getLogger(AddressGateWay.class.getName()).log(Level.SEVERE, null, ex);
}
}
return listData;
}
}
City
public class City {
private int id;
private String cityName;
public City(int id, String cityName) {
this.id = id;
this.cityName = cityName;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getCityName() {
return cityName;
}
public void setCityName(String cityName) {
this.cityName = cityName;
}
}
Controller Logic Code
ObservableList<City> cities = AddressGateWay.getCityByProvince(provinceComboBox.getValue()); // assume the value is "Batanes"
cityComboBox.setItems(cities);
cityComboBox.setConverter(new StringConverter<City>() {
#Override
public String toString(City object) {
return object.getCityName();
}
#Override
public City fromString(String string) {
return cityComboBox.getItems().stream().filter(ap
-> ap.getCityName().equals(string)).findFirst().orElse(null);
}
});
cityComboBox.valueProperty().addListener(
(ObservableValue<? extends City> observable, City oldValue, City newVal) -> {
if (newVal != null) {
//
}
}
);
City city = cities
.stream()
.filter(c -> c.getCityName() == "Itbayat")
.findAny()
.orElse(null);
cityComboBox.getSelectionModel().select(city);
// summing up the below updates and your codes,
// I have set the value of cityComboBox by using . . .
Address ads = new Address();
ads = AddressGetWay.selectedItem(item); // the item to be edit
// assume the value from the database is "Itbayat"
Predicate<City> predicate = city -> city.getCityName() == ads.getCity().getCityName();
Optional<City> opt = cities.stream().filter(predicate).findAny();
cityComboBox.getSelectionModel().select(opt.orElse(null));
// Thank you, it finally work :)
Some key points based on the updated code
Refactoring code based on unnecessary code
Implementing basic Java naming Conventions
Missing connection initialization (need to load based on your database)
Inined code (for Predicate, and Optional)
EDIT
Province
public class Province {
private String provinceCode;
private String provinceName;
//Constructors
//Getters and Setters
}
Address
public class Address {
private City city;
private Province province;
//Constructors
//Getters and Setters
}

Entity Framework Slow Closing Connection

I am builing an MVC 5 web app and I am using entity framework 6 and I have MS SQL Server 2008 R2 as my database. I use connection pooling to ensure that I get a connection as fast as possible for db operations. My connection string in my Web.config is as follows
<add name="MyConnectionPoolConnectionString" connectionString="Data Source=MyHOST;Initial Catalog=TEST_DB;User Id=sa;Password=password1; Min Pool Size=10;MultipleActiveResultSets=True;" providerName="System.Data.SqlClient" />
I notice that obtaining a connection from the connection pool is very fast i.e. some tens to hundreds of milliseconds
However, what I have also noticed is that closing the connection (which in a connection pool means the connection is returned to the pool) takes a rather long time (i.e. on the average between 2000ms and 5000ms (2 - 5 secs)).
Here is an excerpt from my profiling log
Time taken to open connection
2015-02-18 21:06:55,497 Opened connection at 18/02/2015 21:06:55 92.65 ms
Time taken to close / return connection
Closed connection at 18/02/2015 21:07:01 2543.06 ms
Notice that opening the connection takes 92ms and closing the connection takes 2500ms.
BTW, these stats are provided my the EF framework itself i.e. by setting the Log property of the Database prop on the EF context i.e.
MyTestAppDbContext.Database.Log = Logger.Debug;
What I dont understand is why it is so fast to get a connection from the connection pool but it takes so long for EF to return the connection back to the pool (i.e. close the connection) and more importantly how to speed up the release/closing of the connection
This is quite important for me because the web app needs to be quite responsive and as I have to create the EF Dbcontext for every request, if EF adds an extra 2 - 5 secs overhead to just close / release a connection, it reduces the responsiveness of the app.
MyTestAppDbContext.cs
public partial class MyTestAppDbContext : DbContext
{
public ILogger Logger { get; set; }
static MyTestAppDbContext()
{
System.Data.Entity.Database.SetInitializer<MyTestAppDbContext>(null);
}
public MyTestAppDbContext(ILogger logger)
: base("Name=MyConnectionPoolConnectionString")
{
this.Configuration.LazyLoadingEnabled = false;
this.Configuration.ProxyCreationEnabled = false;
Logger = logger;
this.Database.Log = Logger.Debug;
}
public override int SaveChanges()
{
Exception entityFrameworkExceptions = null;
String exMsg = "";
int result = 0;
try
{
if (this.ChangeTracker.HasChanges())
{
// Get all Added/Deleted/Modified entities (not Unmodified or Detached)
foreach (var ent in this.ChangeTracker.Entries().Where(p => p.State == EntityState.Added || p.State == EntityState.Deleted || p.State == EntityState.Modified))
{
// For each changed record, log the activity performed
Logger.Debug("Detected Changes");
}
result = base.SaveChanges();
}
}
catch (DbEntityValidationException dbEx)
{
entityFrameworkExceptions = dbEx;
foreach (var validationErrors in dbEx.EntityValidationErrors)
{
foreach (var validationError in validationErrors.ValidationErrors)
{
//System.Diagnostics.Trace.TraceInformation("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);
exMsg += String.Format("\nProperty: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);
Logger.Debug(String.Format("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage));
}
}
}
catch (DbUpdateException dbEx)
{
entityFrameworkExceptions = dbEx;
Logger.Debug(String.Format("Exception Message: {0}", dbEx.Message));
Logger.Debug(String.Format("InnerException: {0}", dbEx.InnerException));
Logger.Debug(String.Format("StackTrace: {0}", dbEx.StackTrace));
Logger.Debug(String.Format("Call Stack: {0}", CommonUtils.GetCallStackAsString()));
}
catch (Exception ex)
{
entityFrameworkExceptions = ex;
Logger.Debug(String.Format("Exception Message: {0}", ex.Message));
Logger.Debug(String.Format("InnerException: {0}", ex.InnerException));
Logger.Debug(String.Format("StackTrace: {0}", ex.StackTrace));
Logger.Debug(String.Format("Call Stack: {0}", CommonUtils.GetCallStackAsString()));
}
if (entityFrameworkExceptions != null)
{
exMsg = exMsg + "\n" + entityFrameworkExceptions.Message + #"\n" + entityFrameworkExceptions.StackTrace + #"\n" + entityFrameworkExceptions.InnerException;
entityFrameworkExceptions = new HttpException(500, #"Exception applying changes in PaceDBContext (i.e. Entity Framework)\n" + exMsg+"\n"+CommonUtils.GetCallStackAsString());
throw entityFrameworkExceptions;
}
return result;
}
}
IUnitOfWork.cs
public interface IUnitOfWork /*: IDisposable*/
{
MyTestAppDbContext MyTestAppDbContext { get; set; }
Exception Save();
}
UnitOfWork.cs
public class UnitOfWork : IUnitOfWork, IDisposable
{
private bool disposed = false;
public MyTestAppDbContext MyTestAppDbContext {get; set;}
public ILogger Logger { get; set; }
public Exception Save()
{
Exception entityFrameworkExceptions = null;
String exMsg = "";//, callStackAsString = "";
try
{
System.Diagnostics.Trace.TraceInformation("Saving Db Changes in UnitOfWork");
MyTestAppDbContext.SaveChanges();
System.Diagnostics.Trace.TraceInformation("Finished saving Db Changes in UnitOfWork");
}
catch (DbEntityValidationException dbEx)
{
entityFrameworkExceptions = dbEx;
foreach (var validationErrors in dbEx.EntityValidationErrors)
{
foreach (var validationError in validationErrors.ValidationErrors)
{
exMsg += String.Format("\nProperty: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);
Logger.Debug(String.Format("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage));
}
}
}
catch (DbUpdateException dbEx)
{
entityFrameworkExceptions = dbEx;
this.LogException(dbEx);
}
catch (Exception ex)
{
entityFrameworkExceptions = ex;
this.LogException(ex);
}
if (entityFrameworkExceptions != null)
{
exMsg = "\n" + entityFrameworkExceptions.Message + #"\n" + entityFrameworkExceptions.StackTrace + #"\n" + entityFrameworkExceptions.InnerException+"\n"+CommonUtils.GetCallStackAsString();
entityFrameworkExceptions = new HttpException(500, #"Exception applying changes in PaceDBContext (i.e. Entity Framework)\n" + exMsg);
throw entityFrameworkExceptions;
}
return entityFrameworkExceptions;
}
protected virtual void Dispose(bool disposing)
{
this.Save();
if (!this.disposed)
{
if (disposing)
{
MyTestAppDbContext.Dispose();
}
}
this.disposed = true;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
private void LogException(Exception ex){
Logger.Debug(String.Format("Exception Message: {0}", ex.Message));
Logger.Debug(String.Format("InnerException: {0}", ex.InnerException));
Logger.Debug(String.Format("StackTrace: {0}", ex.StackTrace));
}
}
UserRepository.cs
public class UserRepository : IUserRepository
{
public IUnitOfWork UnitOfWork { get; private set; }
public UserRepository(IUnitOfWork unitOfWork)
{
this.UnitOfWork = unitOfWork;
}
public Users GetUserByUserId(string userId)
{
if (!String.IsNullOrEmpty(userId)){
var user = from u in UnitOfWork.MyTestAppDbContext.Users
where u.UserId.Trim().Equals(userId.Trim())
select u;
return user.SingleOrDefault();
}
return null;
}
public string GetUserFullName(string userId)
{
string fullname = null;
if (!String.IsNullOrEmpty(userId))
{
var user = (from u in UnitOfWork.MyTestAppDbContext.Users
where u.UserId.Trim().Equals(userId.Trim())
select u).SingleOrDefault();
if (user != null)
{
if (!String.IsNullOrEmpty(user.FirstName))
fullname += user.FirstName + " ";
if (!String.IsNullOrEmpty(user.LastName))
fullname += user.LastName;
}
}
return fullname;
}
public long GetUserGroupId(string userId)
{
if (!String.IsNullOrEmpty(userId))
{
var user = (from u in UnitOfWork.MyTestAppDbContext.Users
where u.UserId.Trim().Equals(userId.Trim())
select u).SingleOrDefault();
if (user != null)
return user.UserGroupId;
}
return -1;
}
}
Thanks

Using H2 database server how to notify changes to clients (JMS messaging)

I am successfully using H2 database in AUTO_SERVER mode so that a database file is shared among a number of desktop clients on a network transparently.
This way a server is elected among the clients and all other clients read from the tcp server.
What I'm missing is how a client or the server can notify all other desktop clients something has been changed in the database.
Right now I'm using a JGroups channel to let all clients comunicate one with each other however this is another point of failure and another leader election algorithm which runs in parallel with H2.
Isn't there any other method?
I have read about the JMS (Java Message Service Java API) which is supported in some databases. Any hint for H2?
Thanks
EDIT:
The following code is an adaptation of the current answer, if I start the Sender first (set args as "sender") he connects as server to the H2 database, then I execute Receiver (set args as "receiver") in remote machines and they connect as clients.
Yet only the server receives notifications, clients don't receive anything.
This makes sense from what I currently know: a trigger is only called on the server, a user defined function called from a client or server is called on the client or server but not across all clients (and server) connected to the database.
So is there a way to adapt the below to notify all connected instances of a change in the database?
import java.io.File;
import java.sql.*;
import java.util.concurrent.atomic.AtomicLong;
import org.h2.tools.TriggerAdapter;
public class TestSimpleDB2
{
public static void main(String[] args) throws Exception
{
//final String url = "jdbc:h2:mem:test;multi_threaded=true";
final String url = "jdbc:h2:" + File.separator + "mnt/testdir/PlanIGS" + File.separator
+ "persondb;create=true;AUTO_SERVER=TRUE;multi_threaded=true";
Connection conn = DriverManager.getConnection(url);
Statement stat = conn.createStatement();
boolean isSender = false;
args = new String[]
{
"sender"
};
for (String arg : args)
{
if (arg.contains("receiver"))
{
System.out.println("receiver starting");
isSender = false;
}
else if (arg.contains("sender"))
{
System.out.println("sender starting");
isSender = true;
}
}
if (isSender)
{
stat.execute("create alias wait_for_change for \""
+ TestSimpleDB2.class.getName()
+ ".waitForChange\"");
stat.execute("create table test(id identity)");
stat.execute("create trigger notifier "
+ "before insert, update, delete, rollback "
+ "on test call \""
+ TestSimpleDB2.Notifier.class.getName() + "\"");
Thread.sleep(1000);
for (int i = 0; i < 10; i++)
{
System.out.println("Sender: I change something...");
stat.execute("insert into test values(null)");
Thread.sleep(2000);
}
}
else
{
new Thread()
{
public void run()
{
try
{
Connection conn = DriverManager.getConnection(url);
for (int i = 0; i < 10; i++)
{
conn.createStatement().execute(
"call wait_for_change(100000)");
System.out.println("Receiver: event received");
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}.start();
}
conn.close();
}
static AtomicLong modCount = new AtomicLong();
public static void waitForChange(long maxWaitMillis)
{
synchronized (modCount)
{
try
{
modCount.wait(maxWaitMillis);
}
catch (InterruptedException e)
{
// ignore
}
}
}
public static class Notifier extends TriggerAdapter
{
public void fire(Connection conn, ResultSet oldRow, ResultSet newRow)
throws SQLException
{
modCount.incrementAndGet();
synchronized (modCount)
{
modCount.notifyAll();
}
}
}
}
H2 does not implement JMS (in fact I don't know of a database that does). However, you could build a simple notify mechanism within H2, using a trigger and a user defined function, as follows. Please note this would require the multi-threaded mode in H2, which is not fully tested yet. Because of that, it might make sense to use a separate database for messaging than the database you use for your data.
import java.sql.*;
import java.util.concurrent.atomic.AtomicLong;
import org.h2.tools.TriggerAdapter;
public class TestSimpleDb {
public static void main(String[] args) throws Exception {
final String url = "jdbc:h2:mem:test;multi_threaded=true";
Connection conn = DriverManager.getConnection(url);
Statement stat = conn.createStatement();
stat.execute("create alias wait_for_change for \"" +
TestSimpleDb.class.getName() +
".waitForChange\"");
stat.execute("create table test(id identity)");
stat.execute("create trigger notifier " +
"before insert, update, delete, rollback " +
"on test call \"" +
TestSimpleDb.Notifier.class.getName() + "\"");
new Thread() {
public void run() {
try {
Connection conn = DriverManager.getConnection(url);
for (int i = 0; i < 10; i++) {
conn.createStatement().execute(
"call wait_for_change(10000)");
System.out.println("Receiver: event received");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
Thread.sleep(500);
for (int i = 0; i < 10; i++) {
System.out.println("Sender: I change something...");
stat.execute("insert into test values(null)");
Thread.sleep(1000);
}
conn.close();
}
static AtomicLong modCount = new AtomicLong();
public static void waitForChange(long maxWaitMillis) {
synchronized (modCount) {
try {
modCount.wait(maxWaitMillis);
} catch (InterruptedException e) {
// ignore
}
}
}
public static class Notifier extends TriggerAdapter {
public void fire(Connection conn, ResultSet oldRow, ResultSet newRow)
throws SQLException {
modCount.incrementAndGet();
synchronized (modCount) {
modCount.notifyAll();
}
}
}
}

Resources