Asterisk-Java shutdown after connect

97 Views Asked by At

I am using Asterisk with asterisk-java library to detect events. The follwoing code excerpt shows my usage. The connection should stay open, but it just closes after initializing when i am not using a endless loop. How can i keep the connection till the application closes?

[...]
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();
}
[...]
0

There are 0 best solutions below