2

I'm using J2EE and my code is below

public class Notifier {

    private Connection conn;

    public void notifyThread() throws SQLException {
      conn  =   ConnectionManager.getConnection();
      Statement stmt = conn.createStatement();
      stmt.execute("NOTIFY notificationRecived");
      stmt.close();
    }
}

At the listener's end I'm using:

org.postgresql.PGNotification notifications[] = pgconn.getNotifications();

To extract the details of notification. Here I noticed that

notifications[i].getParameter();

Is a method to extract the data which is sent along with the NOTIFY statement. I saw few threads which suggest that we can pass data along with the NOTIFY statement like NOTIFY notificationRecived, "xyz" but this one throws an error at ,xyz.

Is there any other syntax to pass parameters along with NOTIFY?

2 Answers 2

1

Here is the updated code

public void notifyThread() throws SQLException {
  conn    =   ConnectionManager.getConnection();
  Statement stmt = conn.createStatement();
  stmt.execute("SELECT pg_notify('notificationRecived', 'xyz');");
  stmt.close();    
}    

To send a notification you can also use the function pg_notify(text, text). The function takes the channel name as the first argument and the payload as the second. The function is much easier to use than the NOTIFY command if you need to work with non-constant channel names and payloads.

Sign up to request clarification or add additional context in comments.

Comments

0
public void notifyThread() throws SQLException {
  conn  =   ConnectionManager.getConnection();
  Statement stmt = conn.createStatement();

  stmt.execute("NOTIFY notificationRecived, 'xyz'");

  stmt.close();
}

According to documentation: https://www.postgresql.org/docs/current/sql-notify.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.