这是我的情景: 每分钟一个线程检查数据库以查找某些项目。 找到项目后,它们将被传递给另一个线程(消费者)。
我在申请中使用spring,但这并不意味着我必须使用spring的课程,对吧?
所以,现在我完全迷失在ScheduledExecutorService
(java),ExecutorService
(java),TaskExecutor
(春天),TaskScheduler
(春天)的树林里, @Scheduled
(春天)。
请帮助我了解实施我的方案的正确方法。
答案 0 :(得分:1)
如果我理解了您的问题,那么您使用的是Shared Database Pattern,因为many reason对于Messaging有点气馁并且被用作最后一个资源。
如果您希望多个应用程序进行通信并进行解耦,则应使用Spring Cloud Stream(例如:Shared Database)。
无论如何,如果您需要拥有Listen / Notify,您可能需要https://jdbc.postgresql.org/documentation/81/listennotify.html提供的内容。
来自{{3}}:
import java.sql.*;
public class NotificationTest {
public static void main(String args[]) throws Exception {
Class.forName("org.postgresql.Driver");
String url = "jdbc:postgresql://localhost:5432/test";
// Create two distinct connections, one for the notifier
// and another for the listener to show the communication
// works across connections although this example would
// work fine with just one connection.
Connection lConn = DriverManager.getConnection(url,"test","");
Connection nConn = DriverManager.getConnection(url,"test","");
// Create two threads, one to issue notifications and
// the other to receive them.
Listener listener = new Listener(lConn);
Notifier notifier = new Notifier(nConn);
listener.start();
notifier.start();
}
}
class Listener extends Thread {
private Connection conn;
private org.postgresql.PGConnection pgconn;
Listener(Connection conn) throws SQLException {
this.conn = conn;
this.pgconn = (org.postgresql.PGConnection)conn;
Statement stmt = conn.createStatement();
stmt.execute("LISTEN mymessage");
stmt.close();
}
public void run() {
while (true) {
try {
// issue a dummy query to contact the backend
// and receive any pending notifications.
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT 1");
rs.close();
stmt.close();
org.postgresql.PGNotification notifications[] = pgconn.getNotifications();
if (notifications != null) {
for (int i=0; i<notifications.length; i++) {
System.out.println("Got notification: " + notifications[i].getName());
}
}
// wait a while before checking again for new
// notifications
Thread.sleep(500);
} catch (SQLException sqle) {
sqle.printStackTrace();
} catch (InterruptedException ie) {
ie.printStackTrace();
}
}
}
}
class Notifier extends Thread {
private Connection conn;
public Notifier(Connection conn) {
this.conn = conn;
}
public void run() {
while (true) {
try {
Statement stmt = conn.createStatement();
stmt.execute("NOTIFY mymessage");
stmt.close();
Thread.sleep(2000);
} catch (SQLException sqle) {
sqle.printStackTrace();
} catch (InterruptedException ie) {
ie.printStackTrace();
}
}
}
}
答案 1 :(得分:0)
除了使用ScheduledExecutorService之外,您还可以使用石英调度程序,该调度程序用于按特定时间间隔调度某些作业,在每种情况下都是如此。它可以很容易地与弹簧集成。 Cron表达式用于指定计划的时间。 您可以编写检查数据库的逻辑,以查找扩展QuartzJobBean类的类中的某些项。 看到: https://examples.javacodegeeks.com/enterprise-java/quartz/spring-quartz-scheduler-example/