当前,我正在尝试制作一个模块,该模块将通过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,说它应该在某个发行版号中固定。
我如何应用这些修补程序?
谢谢
答案 0 :(得分:0)
我有点晚了;)
我遇到了同样的问题,并且还阅读到该问题已解决。但事实并非如此。 无论如何,问题是在创建postgres数据库时,LC_COLLATE可能设置为Indonesian_Indonesia.1252。尝试建立连接时,将此值与Java语言环境进行比较。在Java Locales类中,该值可能是您所用的语言,因此找不到该条目。但是,要解决该问题,可以将Java语言环境的默认值设置为English。当然,这不是解决问题的最佳方法,但它确实有效。为了安全起见,我会在建立连接后退回
您可以如下设置默认值:
Locale.setDefault(Locale.ENGLISH)