PostgreSQL学习第十四篇 listen与notify

PostgreSQL提供了client端之间通过服务器端进行消息通信的机制。这种机制是通过listen和notify命令来完成的。

session1:
postgres=# listen postgres;
LISTEN
postgres=# listen liming
postgres-# ;
LISTEN

session2:
postgres=# notify postgres,'hello word';
NOTIFY
postgres=# notify liming,'hello liming';
NOTIFY
postgres=#

session1;
postgres=# select 1;   --随便执行了一个命令
 ?column?
----------
        1
(1 row)

Asynchronous notification "postgres" with payload "hello word" received from server process with PID 12241.
Asynchronous notification "liming" with payload "hello liming" received from server process with PID 12241.
postgres=#  

listen和notify的相关命令:

	1. listen:监听消息通道
	2. unlisten:取消先前的监听
	3. notify:发送消息到消息通道中
	4. pg_notify():与notify命令功能相同
	5. pg_listening_channels():调用此函数可以查询当前session已注册了哪些消息监听


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
在Spring Boot中,可以使用Spring Data JPA库来操作PostgreSQL数据库,并使用PostgreSQLNOTIFY/LISTEN机制来监听指定表的变化。具体步骤如下: 1. 配置PostgreSQL数据源和JPA库 在application.properties文件中添加如下配置: ``` spring.datasource.url=jdbc:postgresql://localhost:5432/db_name spring.datasource.username=username spring.datasource.password=password spring.jpa.hibernate.ddl-auto=update spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect ``` 这里的db_name为数据库名称,username和password分别为数据库的用户名和密码。同时,用于自动建表的配置也需要添加。 2. 创建实体类 使用JPA库创建实体类,例如: ```java @Entity @Table(name = "book") public class Book { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; private String author; // 省略getter和setter方法 } ``` 这里创建了一个名为book的表,包含id、name和author三个字段。 3. 创建监听器 创建一个监听器,用于监听PostgreSQL中指定表的变化。在Spring Boot中,可以使用@EventListener注解来监听事件。例如: ```java @Component public class BookTableListener { @Autowired private DataSource dataSource; @EventListener(ApplicationReadyEvent.class) public void init() throws SQLException { Connection connection = dataSource.getConnection(); Statement statement = connection.createStatement(); statement.execute("LISTEN book_change"); Executors.newSingleThreadExecutor().execute(() -> { try { while (true) { if (connection.isClosed()) { break; } PGNotification[] notifications = ((PGConnection) connection).getNotifications(); if (notifications != null) { for (PGNotification notification : notifications) { System.out.println("Table [book] has been changed. New data id is: " + notification.getParameter()); } } Thread.sleep(1000); } } catch (SQLException | InterruptedException e) { e.printStackTrace(); } }); } } ``` 在init()方法中,创建了一个JDBC连接,并在该连接上执行了“LISTEN book_change”命令,表示监听book表的变化。然后创建一个新的线程,在该线程中轮询数据库中的通知,如果有通知则说明表发生了变化,可以进行相应的处理。 需要注意的是,这里使用了PostgreSQL的JDBC驱动,因此需要在pom.xml中添加如下依赖: ```xml <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <version>42.2.14</version> </dependency> ``` 4. 测试监听器 运行Spring Boot应用后,在数据库中对book表进行插入、更新或删除操作,会发现控制台输出了相应的通知信息,表明监听器已经正常工作。 以上就是使用Spring Boot和PostgreSQLNOTIFY/LISTEN机制来监听指定表的变化的完整流程。需要注意的是,该机制只能监听整个表的变化,无法监听特定行或列的变化。如果需要监听特定行或列的变化,可以使用PostgreSQL的触发器机制来实现。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

丹心明月

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值