1 | package org.cardanofoundation.explorer.api.event.blocksync; | |
2 | ||
3 | import java.sql.Connection; | |
4 | import java.util.function.Consumer; | |
5 | ||
6 | import javax.sql.DataSource; | |
7 | ||
8 | import lombok.RequiredArgsConstructor; | |
9 | import lombok.extern.slf4j.Slf4j; | |
10 | ||
11 | import org.springframework.scheduling.annotation.Async; | |
12 | import org.springframework.stereotype.Component; | |
13 | ||
14 | import org.postgresql.PGConnection; | |
15 | import org.postgresql.PGNotification; | |
16 | ||
17 | @Slf4j | |
18 | @RequiredArgsConstructor | |
19 | @Component | |
20 | public class BlockEventListener { | |
21 | ||
22 | private static final String NOTIFCATIONS = "new_block"; | |
23 | private final DataSource dataSource; | |
24 | ||
25 | @Async | |
26 | public void listenForNotifications(Consumer<PGNotification> consumer) { | |
27 | while (true) { | |
28 | try (Connection c = dataSource.getConnection()) { | |
29 | PGConnection pgconn = c.unwrap(PGConnection.class); | |
30 | c.createStatement().execute("LISTEN " + NOTIFCATIONS); | |
31 |
1
1. listenForNotifications : removed call to java/sql/Connection::commit → NO_COVERAGE |
c.commit(); |
32 | log.info( | |
33 | "Connection established: Listening for notifications on channel: [{}]", NOTIFCATIONS); | |
34 |
1
1. listenForNotifications : negated conditional → NO_COVERAGE |
while (!Thread.currentThread().isInterrupted()) { |
35 | PGNotification[] nts = pgconn.getNotifications(10000); | |
36 |
1
1. listenForNotifications : negated conditional → NO_COVERAGE |
if (nts == null) { |
37 | continue; | |
38 | } | |
39 | ||
40 | for (PGNotification nt : nts) { | |
41 |
1
1. listenForNotifications : removed call to java/util/function/Consumer::accept → NO_COVERAGE |
consumer.accept(nt); |
42 | } | |
43 | } | |
44 | } catch (Exception e) { | |
45 | log.warn("Error occurred while listening for notifications, attempting to reconnect...", e); | |
46 | } | |
47 | } | |
48 | } | |
49 | } | |
Mutations | ||
31 |
1.1 |
|
34 |
1.1 |
|
36 |
1.1 |
|
41 |
1.1 |