|
20 | 20 | import com.example.response.Result; |
21 | 21 | import com.zaxxer.hikari.HikariConfig; |
22 | 22 | import com.zaxxer.hikari.HikariDataSource; |
| 23 | +import org.postgresql.PGConnection; |
| 24 | +import org.postgresql.PGNotification; |
| 25 | +import org.slf4j.Logger; |
| 26 | +import org.slf4j.LoggerFactory; |
23 | 27 | import org.springframework.beans.factory.annotation.Autowired; |
24 | 28 | import org.springframework.beans.factory.annotation.Value; |
25 | 29 | import org.springframework.boot.SpringApplication; |
26 | 30 | import org.springframework.boot.autoconfigure.SpringBootApplication; |
27 | 31 | import org.springframework.context.annotation.Bean; |
28 | | -import org.springframework.http.HttpEntity; |
29 | 32 | import org.springframework.http.ResponseEntity; |
| 33 | +import org.springframework.scheduling.annotation.EnableScheduling; |
| 34 | +import org.springframework.scheduling.annotation.Scheduled; |
30 | 35 | import org.springframework.stereotype.Controller; |
31 | 36 | import org.springframework.web.bind.annotation.RequestMapping; |
32 | 37 | import org.springframework.web.bind.annotation.RequestParam; |
|
41 | 46 | import java.text.MessageFormat; |
42 | 47 | import java.util.ArrayList; |
43 | 48 | import java.util.List; |
44 | | -import java.util.Map; |
45 | 49 |
|
46 | 50 | @Controller |
47 | 51 | @SpringBootApplication |
| 52 | +@EnableScheduling |
48 | 53 | public class Main { |
49 | 54 |
|
| 55 | + private static final Logger logger = LoggerFactory.getLogger(Main.class); |
| 56 | + |
50 | 57 | @Value("${spring.datasource.url}") |
51 | 58 | private String dbUrl; |
52 | 59 |
|
@@ -106,6 +113,37 @@ ResponseEntity<Result> getCpAccount(@RequestParam(name = "id")String id) { |
106 | 113 | return restTemplate.getForEntity(formattedQuoteUrl, Result.class); |
107 | 114 | } |
108 | 115 |
|
| 116 | + @Scheduled(fixedDelay=3000) |
| 117 | + public void listen() |
| 118 | + { |
| 119 | + try(Connection conn = dataSource.getConnection()) |
| 120 | + { |
| 121 | + // issue a dummy query to contact the backend |
| 122 | + // and receive any pending notifications. |
| 123 | + Statement stmt = conn.createStatement(); |
| 124 | + stmt.execute("LISTEN select_event"); |
| 125 | + ResultSet rs = stmt.executeQuery("SELECT * from salesforcecgoconnect.account"); |
| 126 | + rs.close(); |
| 127 | + stmt.close(); |
| 128 | + |
| 129 | + org.postgresql.PGNotification notifications[] = (conn.unwrap(PGConnection.class)).getNotifications(); |
| 130 | + |
| 131 | + logger.debug(notifications == null ? "0": String.valueOf(notifications.length)); |
| 132 | + |
| 133 | + if (notifications != null) { |
| 134 | + |
| 135 | + for (PGNotification notification : notifications) { |
| 136 | + logger.debug("Got notification: " + notification.getName() + notification.getParameter()); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + } |
| 141 | + catch (Exception e) |
| 142 | + { |
| 143 | + e.printStackTrace(); |
| 144 | + } |
| 145 | + } |
| 146 | + |
109 | 147 | @Bean |
110 | 148 | public DataSource dataSource() throws SQLException { |
111 | 149 | if (dbUrl == null || dbUrl.isEmpty()) { |
|
0 commit comments