目前,我正在尝试制作一个模块,它将通过Postgres上的触发器来监听任何更改。我使用的是pgjdbc-ng v0.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,说它应该在某个版本号内修复。
我如何应用这些修复?
谢谢
发布于 2019-04-12 09:05:34
我知道现在有点晚了;)
我也有同样的问题,而且我也读到问题已经解决了。但情况似乎并非如此。无论如何,问题是在创建postgres数据库时,LC_COLLATE可能被设置为Indonesian_Indonesia.1252。当尝试建立连接时,将该值与java语言环境进行比较。在Java Locales类中,该值可能使用您的语言,因此无法找到该条目。但是,为了解决这个问题,您可以将Java语言环境的缺省值设置为English。这当然不是解决问题的最好方法,但它是有效的。为了安全起见,我会在连接建立后放回
您可以按如下方式设置默认值:
Locale.setDefault(Locale.ENGLISH)
https://stackoverflow.com/questions/54863521
复制相似问题