我正在使用带有asterisk-java
库的 Asterisk 来检测事件。以下代码摘录显示了我的用法。连接应该保持打开状态,但是当我不使用无限循环时,它会在初始化后关闭。如何在应用程序关闭之前保持连接?
[...]
public AsteriskManager(AsteriskAccountData asteriskAccountData)
throws ManagerCommunicationException {
this.asteriskAccountData = asteriskAccountData;
this.asteriskStateModel = new AsteriskStateModel();
new Thread(() -> {
asteriskServer = new DefaultAsteriskServer(
asteriskAccountData.getHost(),
asteriskAccountData.getUser(),
asteriskAccountData.getPassword()
);
ManagerConnectionFactory factory = new ManagerConnectionFactory(
asteriskAccountData.getHost(),
asteriskAccountData.getUser(),
asteriskAccountData.getPassword()
);
this.managerConnection = factory.createManagerConnection();
try {
asteriskServer.addAsteriskServerListener(this);
managerConnection.addEventListener(this);
registerListener();
//FIXME endless thread
while (true) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} catch (ManagerCommunicationException ex) {
//FIXME error handling
}
}, "AsteriskManagerThread").start();
}
[...]