0

目前我正在尝试制作一个模块,该模块将通过 Postgres 上的触发器监听任何更改。我正在使用 pgjdbc-ng ver 0.8.2,从 maven repo central 下载 JAR 并将其添加为项目参考。

以下是我使用的代码:

public class ListenNotify
{
// Create the queue that will be shared by the producer and consumer
private BlockingQueue queue = new ArrayBlockingQueue(10);

    // Database connection
    PGConnection connection;

    public ListenNotify()
    {
        // Get database info from environment variables
        /*
        String DBHost = System.getenv("DBHost"); 
        String DBName = System.getenv("DBName");
        String DBUserName = System.getenv("DBUserName");
        String DBPassword = System.getenv("DBPassword");
        */
        String DBHost = "127.0.0.1"; 
        String DBName = "dbname";
        String DBUserName = "postgres";
        String DBPassword = "postgres";
        // Create the listener callback
        PGNotificationListener listener = new PGNotificationListener()
        {
            @Override
            public void notification(int processId, String channelName, String payload)
            {
            // Add event and payload to the queue
            queue.add("/channels/" + channelName + " " + payload);
            }
        };

        try
        {
            // Create a data source for logging into the db
            PGDataSource dataSource = new PGDataSource();
            dataSource.setHost(DBHost);
            dataSource.setPort(5432);
            dataSource.setDatabaseName(DBName);
            dataSource.setUser(DBUserName);
            dataSource.setPassword(DBPassword);

            // Log into the db
            connection = (PGConnection) dataSource.getConnection();

            // add the callback listener created earlier to the connection
            connection.addNotificationListener(listener);

            // Tell Postgres to send NOTIFY q_event to our connection and listener
            Statement statement = connection.createStatement();
            statement.execute("LISTEN q_event");
            statement.close();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    /**
    * @return shared queue
    */
    public BlockingQueue getQueue()
    {
        return queue;
    }

    /**
    *
    * main entry point
    *
    * @param args
    */
    public static void main(String[] args)
    {
    // Create a new listener
        ListenNotify ln = new ListenNotify();

        // Get the shared queue
        BlockingQueue queue = ln.getQueue();

        // Loop forever pulling messages off the queue
        while (true)
        {
            try
            {
                // queue blocks until something is placed on it
                String msg = queue.take().toString();

                // Do something with the event
                System.out.println(msg);
            }
            catch (InterruptedException e)
            {
                e.printStackTrace();
            }
        }
    }
}

运行后,我得到了异常:

病态区域:印度尼西亚 [在索引 0]

我已经阅读了官方的 git,说它应该在某个版本号内修复。

我如何应用这些修复?

谢谢

4

1 回答 1

1

我知道它有点晚了;)

我有同样的问题,也读到问题已经解决。但似乎并非如此。无论如何,问题是在创建 postgres 数据库时,LC_COLLATE 可能设置为 Indonesia_Indonesia.1252。尝试建立连接时,会将此值与 java 语言环境进行比较。在 Java Locales 类中,该值可能是您的语言,因此无法找到该条目。但是,要解决此问题,您可以将 Java 语言环境的默认值设置为英语。这当然不是解决问题的最佳方法,但它确实有效。为了安全起见,我会在连接建立后放回去

您可以按如下方式设置默认值:

Locale.setDefault(Locale.ENGLISH)

于 2019-04-12T09:05:33.973 回答