-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'upstream/maintable-beta' into sharedblogin
* upstream/maintable-beta: Switch to org.postgresql (#4031)
- Loading branch information
Showing
5 changed files
with
64 additions
and
67 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
39 changes: 31 additions & 8 deletions
39
src/main/java/org/jabref/logic/shared/listener/PostgresSQLNotificationListener.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 |
---|---|---|
@@ -1,27 +1,50 @@ | ||
package org.jabref.logic.shared.listener; | ||
|
||
import java.sql.SQLException; | ||
|
||
import org.jabref.logic.shared.DBMSProcessor; | ||
import org.jabref.logic.shared.DBMSSynchronizer; | ||
|
||
import com.impossibl.postgres.api.jdbc.PGNotificationListener; | ||
import org.postgresql.PGConnection; | ||
import org.postgresql.PGNotification; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* A listener for PostgreSQL database notifications. | ||
*/ | ||
public class PostgresSQLNotificationListener implements PGNotificationListener { | ||
public class PostgresSQLNotificationListener extends Thread { | ||
|
||
private final DBMSSynchronizer dbmsSynchronizer; | ||
private static final Logger LOGGER = LoggerFactory.getLogger(PostgresSQLNotificationListener.class); | ||
|
||
private final DBMSSynchronizer dbmsSynchronizer; | ||
private final PGConnection pgConnection; | ||
|
||
public PostgresSQLNotificationListener(DBMSSynchronizer dbmsSynchronizer) { | ||
public PostgresSQLNotificationListener(DBMSSynchronizer dbmsSynchronizer, PGConnection pgConnection) { | ||
this.dbmsSynchronizer = dbmsSynchronizer; | ||
this.pgConnection = pgConnection; | ||
} | ||
|
||
@Override | ||
public void notification(int processId, String channel, String payload) { | ||
if (!payload.equals(DBMSProcessor.PROCESSOR_ID)) { | ||
dbmsSynchronizer.pullChanges(); | ||
public void run() { | ||
try { | ||
//noinspection InfiniteLoopStatement | ||
while (true) { | ||
PGNotification notifications[] = pgConnection.getNotifications(); | ||
|
||
if (notifications != null) { | ||
for (PGNotification notification : notifications) { | ||
if (!notification.getName().equals(DBMSProcessor.PROCESSOR_ID)) { | ||
dbmsSynchronizer.pullChanges(); | ||
} | ||
} | ||
} | ||
|
||
// Wait a while before checking again for new notifications | ||
Thread.sleep(500); | ||
} | ||
} catch (SQLException | InterruptedException exception) { | ||
LOGGER.error("Error while listening for updates to PostgresSQL", 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