我正在使用带有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();
}
[...]