Skip to content

Commit 0f6192d

Browse files
author
Parvatam
committed
Events
1 parent ea72435 commit 0f6192d

1 file changed

Lines changed: 40 additions & 2 deletions

File tree

src/main/java/com/example/Main.java

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,18 @@
2020
import com.example.response.Result;
2121
import com.zaxxer.hikari.HikariConfig;
2222
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;
2327
import org.springframework.beans.factory.annotation.Autowired;
2428
import org.springframework.beans.factory.annotation.Value;
2529
import org.springframework.boot.SpringApplication;
2630
import org.springframework.boot.autoconfigure.SpringBootApplication;
2731
import org.springframework.context.annotation.Bean;
28-
import org.springframework.http.HttpEntity;
2932
import org.springframework.http.ResponseEntity;
33+
import org.springframework.scheduling.annotation.EnableScheduling;
34+
import org.springframework.scheduling.annotation.Scheduled;
3035
import org.springframework.stereotype.Controller;
3136
import org.springframework.web.bind.annotation.RequestMapping;
3237
import org.springframework.web.bind.annotation.RequestParam;
@@ -41,12 +46,14 @@
4146
import java.text.MessageFormat;
4247
import java.util.ArrayList;
4348
import java.util.List;
44-
import java.util.Map;
4549

4650
@Controller
4751
@SpringBootApplication
52+
@EnableScheduling
4853
public class Main {
4954

55+
private static final Logger logger = LoggerFactory.getLogger(Main.class);
56+
5057
@Value("${spring.datasource.url}")
5158
private String dbUrl;
5259

@@ -106,6 +113,37 @@ ResponseEntity<Result> getCpAccount(@RequestParam(name = "id")String id) {
106113
return restTemplate.getForEntity(formattedQuoteUrl, Result.class);
107114
}
108115

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+
109147
@Bean
110148
public DataSource dataSource() throws SQLException {
111149
if (dbUrl == null || dbUrl.isEmpty()) {

0 commit comments

Comments
 (0)