Thread: [Asterisk-java-users] Execute Action after connect or reconnect
Brought to you by:
srt
From: Henrik B. <hen...@fr...> - 2007-09-04 15:20:39
|
Hi, is there a way to execute code after asterisk-java has reconnected to an asterisk server, e.g. set global variables via asteriskserver.setGlobalVariable()? If I execute this after the initial login it works. I then tried to create an event handler for ConnectEvents to find out when asterisk-java has reconnected. When executing setGlobalVariable afterwards I always get: Exception in thread "Asterisk-Java ManagerConnection-0-Reconnect-0" java.util.ConcurrentModificationException at java.util.AbstractList Itr.checkForComodification(AbstractList.java:449) at java.util.AbstractList$Itr.next(AbstractList.java:420) at org.asteriskjava.manager.internal.ManagerConnectionImpl.fireEvent(ManagerConnectionImpl.java:1204) at org.asteriskjava.manager.internal.ManagerConnectionImpl.doLogin(ManagerConnectionImpl.java:590) at org.asteriskjava.manager.internal.ManagerConnectionImpl.reconnect(ManagerConnectionImpl.java:1290) at org.asteriskjava.manager.internal.ManagerConnectionImpl.access $100(ManagerConnectionImpl.java:84) at org.asteriskjava.manager.internal.ManagerConnectionImpl $1.run(ManagerConnectionImpl.java:1160) at java.lang.Thread.run(Thread.java:595) any ideas? Henrik |
From: Martin S. <ma...@be...> - 2007-09-04 16:42:08
|
Hello Henrik, Could you show the list what code you are using to add the ConnectEvent listener, and what you try to do when you receive a new ConnectEvent? Are you modifying the AJ source directly? Martin Smith, Systems Developer ma...@be... Bureau of Economic and Business Research University of Florida (352) 392-0171 Ext. 221=20 =20 > -----Original Message----- > From: ast...@li...=20 > [mailto:ast...@li...] On=20 > Behalf Of Henrik Buchholz > Sent: Tuesday, September 04, 2007 11:20 AM > To: ast...@li... > Subject: [Asterisk-java-users] Execute Action after connect=20 > or reconnect >=20 > Hi, >=20 > is there a way to execute code after asterisk-java has=20 > reconnected to an > asterisk server, e.g. set global variables via > asteriskserver.setGlobalVariable()? >=20 > If I execute this after the initial login it works. I then tried to > create an event handler for ConnectEvents to find out when=20 > asterisk-java > has reconnected. When executing setGlobalVariable afterwards I always > get: >=20 > Exception in thread "Asterisk-Java ManagerConnection-0-Reconnect-0" > java.util.ConcurrentModificationException=20 > at java.util.AbstractList > Itr.checkForComodification(AbstractList.java:449) > at java.util.AbstractList$Itr.next(AbstractList.java:420) > at > org.asteriskjava.manager.internal.ManagerConnectionImpl.fireEv > ent(ManagerConnectionImpl.java:1204) > at > org.asteriskjava.manager.internal.ManagerConnectionImpl.doLogi > n(ManagerConnectionImpl.java:590) > at > org.asteriskjava.manager.internal.ManagerConnectionImpl.reconn > ect(ManagerConnectionImpl.java:1290) > at > org.asteriskjava.manager.internal.ManagerConnectionImpl.access > $100(ManagerConnectionImpl.java:84) > at org.asteriskjava.manager.internal.ManagerConnectionImpl > $1.run(ManagerConnectionImpl.java:1160) > at java.lang.Thread.run(Thread.java:595) >=20 > any ideas? >=20 > Henrik >=20 >=20 >=20 > -------------------------------------------------------------- > ----------- > This SF.net email is sponsored by: Splunk Inc. > Still grepping through log files to find problems? Stop. > Now Search log events and configuration files using AJAX and=20 > a browser. > Download your FREE copy of Splunk now >> http://get.splunk.com/ > _______________________________________________ > Asterisk-java-users mailing list > Ast...@li... > https://lists.sourceforge.net/lists/listinfo/asterisk-java-users >=20 |
From: Henrik B. <hen...@fr...> - 2007-09-05 07:43:19
|
Am Dienstag, den 04.09.2007, 12:19 -0400 schrieb Martin Smith: > Hello Henrik, > > Could you show the list what code you are using to add the ConnectEvent > listener, and what you try to do when you receive a new ConnectEvent? sure. thats the connect to the server and the adding of the listener: ManagerConnectionFactory factory = new ManagerConnectionFactory(astserver, asteriskuser, asteriskpasswd); this.managerConnection = factory.createManagerConnection(); managerConnection.login(); this.asteriskserver = new DefaultAsteriskServer(this.managerConnection); managerConnection.addEventListener(new ConnectEventListener(asteriskserver)); and this is the listener class: public class ConnectEventListener implements ManagerEventListener { private AsteriskServer asteriskserver; public ConnectEventListener(AsteriskServer conn){ this.asteriskserver = conn; } public void onManagerEvent(ManagerEvent event) { String eventname = event.getClass().getName(); if (eventname.equals("org.asteriskjava.manager.event.ConnectEvent")){ ConnectEvent connevent = (ConnectEvent) event; writeGlobalAsteriskVars(); } } public void writeGlobalAsteriskVars(){ try { asteriskserver.setGlobalVariable("EXT", "foobar"); } catch (ManagerCommunicationException e) { System.err.println(e.toString()); } } } > Are you modifying the AJ source directly? uhh, no wouldn't do that... I just commented all other code, so nothing else should try to access the server via AJ and create a conflict. Interestingly the asterisk CLI tells me that the variable was set, although the error occurs in AJ. thanks for your help Henrik > > > -----Original Message----- > > From: ast...@li... > > [mailto:ast...@li...] On > > Behalf Of Henrik Buchholz > > Sent: Tuesday, September 04, 2007 11:20 AM > > To: ast...@li... > > Subject: [Asterisk-java-users] Execute Action after connect > > or reconnect > > > > Hi, > > > > is there a way to execute code after asterisk-java has > > reconnected to an > > asterisk server, e.g. set global variables via > > asteriskserver.setGlobalVariable()? > > > > If I execute this after the initial login it works. I then tried to > > create an event handler for ConnectEvents to find out when > > asterisk-java > > has reconnected. When executing setGlobalVariable afterwards I always > > get: > > > > Exception in thread "Asterisk-Java ManagerConnection-0-Reconnect-0" > > java.util.ConcurrentModificationException > > at java.util.AbstractList > > Itr.checkForComodification(AbstractList.java:449) > > at java.util.AbstractList$Itr.next(AbstractList.java:420) > > at > > org.asteriskjava.manager.internal.ManagerConnectionImpl.fireEv > > ent(ManagerConnectionImpl.java:1204) > > at > > org.asteriskjava.manager.internal.ManagerConnectionImpl.doLogi > > n(ManagerConnectionImpl.java:590) > > at > > org.asteriskjava.manager.internal.ManagerConnectionImpl.reconn > > ect(ManagerConnectionImpl.java:1290) > > at > > org.asteriskjava.manager.internal.ManagerConnectionImpl.access > > $100(ManagerConnectionImpl.java:84) > > at org.asteriskjava.manager.internal.ManagerConnectionImpl > > $1.run(ManagerConnectionImpl.java:1160) > > at java.lang.Thread.run(Thread.java:595) > > > > any ideas? > > > > Henrik > > > > > > > > -------------------------------------------------------------- > > ----------- > > This SF.net email is sponsored by: Splunk Inc. > > Still grepping through log files to find problems? Stop. > > Now Search log events and configuration files using AJAX and > > a browser. > > Download your FREE copy of Splunk now >> http://get.splunk.com/ > > _______________________________________________ > > Asterisk-java-users mailing list > > Ast...@li... > > https://lists.sourceforge.net/lists/listinfo/asterisk-java-users > > > > ------------------------------------------------------------------------- > This SF.net email is sponsored by: Splunk Inc. > Still grepping through log files to find problems? Stop. > Now Search log events and configuration files using AJAX and a browser. > Download your FREE copy of Splunk now >> http://get.splunk.com/ > _______________________________________________ > Asterisk-java-users mailing list > Ast...@li... > https://lists.sourceforge.net/lists/listinfo/asterisk-java-users |
From: Martin S. <ma...@be...> - 2007-09-05 16:59:02
|
Henrik, I think you may have spotted a bug. We synchronize access to the list of event listeners from other threads, but since you are adding an event listener in the same thread as the event firing for connection, the list isn't protected by the synchronized keyword. Unfortunately, I can see that fully protecting the list could cause deadlock when you want to edit the list while iterating through it. We probably need to work on this some to get it right. What do you think, Stefan? As a workaround, can you try something like: public void writeGlobalAsteriskVars(){ final AsteriskServer a2 =3D asteriskserver; =09 Runnable r =3D new Runnable() { public void run() { try { a2.setGlobalVariable("EXT", "foobar"); } catch (ManagerCommunicationException e) { =09 System.err.println(e.toString()); }=20 } }; new Thread(r).start(); } Martin Smith, Systems Developer ma...@be... Bureau of Economic and Business Research University of Florida (352) 392-0171 Ext. 221=20 =20 > -----Original Message----- > From: ast...@li...=20 > [mailto:ast...@li...] On=20 > Behalf Of Henrik Buchholz > Sent: Wednesday, September 05, 2007 3:43 AM > To: ast...@li... > Subject: Re: [Asterisk-java-users] Execute Action after=20 > connect or reconnect >=20 > Am Dienstag, den 04.09.2007, 12:19 -0400 schrieb Martin Smith: > > Hello Henrik, > >=20 > > Could you show the list what code you are using to add the=20 > ConnectEvent > > listener, and what you try to do when you receive a new=20 > ConnectEvent? >=20 > sure. thats the connect to the server and the adding of the listener: >=20 > ManagerConnectionFactory factory =3D new > ManagerConnectionFactory(astserver, asteriskuser, asteriskpasswd); > =09 > this.managerConnection =3D factory.createManagerConnection(); >=20 > managerConnection.login(); >=20 > this.asteriskserver =3D new=20 > DefaultAsteriskServer(this.managerConnection); >=20 > managerConnection.addEventListener(new > ConnectEventListener(asteriskserver)); >=20 >=20 > and this is the listener class: >=20 > public class ConnectEventListener implements ManagerEventListener { >=20 > private AsteriskServer asteriskserver; > =09 > public ConnectEventListener(AsteriskServer conn){ > this.asteriskserver =3D conn; > } >=20 > public void onManagerEvent(ManagerEvent event) { > =09 > String eventname =3D event.getClass().getName(); > if=20 > (eventname.equals("org.asteriskjava.manager.event.ConnectEvent")){ > ConnectEvent connevent =3D (ConnectEvent) event; > writeGlobalAsteriskVars(); > } > =09 > } >=20 > public void writeGlobalAsteriskVars(){ > try { > asteriskserver.setGlobalVariable("EXT",=20 > "foobar"); > } catch (ManagerCommunicationException e) { > System.err.println(e.toString()); > }=20 > } > } >=20 >=20 > > Are you modifying the AJ source directly? >=20 > uhh, no wouldn't do that... >=20 > I just commented all other code, so nothing else should try to access > the server via AJ and create a conflict. Interestingly the=20 > asterisk CLI > tells me that the variable was set, although the error occurs in AJ. >=20 > thanks for your help >=20 > Henrik >=20 > >=20 > > > -----Original Message----- > > > From: ast...@li...=20 > > > [mailto:ast...@li...] On=20 > > > Behalf Of Henrik Buchholz > > > Sent: Tuesday, September 04, 2007 11:20 AM > > > To: ast...@li... > > > Subject: [Asterisk-java-users] Execute Action after connect=20 > > > or reconnect > > >=20 > > > Hi, > > >=20 > > > is there a way to execute code after asterisk-java has=20 > > > reconnected to an > > > asterisk server, e.g. set global variables via > > > asteriskserver.setGlobalVariable()? > > >=20 > > > If I execute this after the initial login it works. I=20 > then tried to > > > create an event handler for ConnectEvents to find out when=20 > > > asterisk-java > > > has reconnected. When executing setGlobalVariable=20 > afterwards I always > > > get: > > >=20 > > > Exception in thread "Asterisk-Java=20 > ManagerConnection-0-Reconnect-0" > > > java.util.ConcurrentModificationException=20 > > > at java.util.AbstractList > > > Itr.checkForComodification(AbstractList.java:449) > > > at java.util.AbstractList$Itr.next(AbstractList.java:420) > > > at > > > org.asteriskjava.manager.internal.ManagerConnectionImpl.fireEv > > > ent(ManagerConnectionImpl.java:1204) > > > at > > > org.asteriskjava.manager.internal.ManagerConnectionImpl.doLogi > > > n(ManagerConnectionImpl.java:590) > > > at > > > org.asteriskjava.manager.internal.ManagerConnectionImpl.reconn > > > ect(ManagerConnectionImpl.java:1290) > > > at > > > org.asteriskjava.manager.internal.ManagerConnectionImpl.access > > > $100(ManagerConnectionImpl.java:84) > > > at org.asteriskjava.manager.internal.ManagerConnectionImpl > > > $1.run(ManagerConnectionImpl.java:1160) > > > at java.lang.Thread.run(Thread.java:595) > > >=20 > > > any ideas? > > >=20 > > > Henrik > > >=20 > > >=20 > > >=20 > > > -------------------------------------------------------------- > > > ----------- > > > This SF.net email is sponsored by: Splunk Inc. > > > Still grepping through log files to find problems? Stop. > > > Now Search log events and configuration files using AJAX and=20 > > > a browser. > > > Download your FREE copy of Splunk now >> http://get.splunk.com/ > > > _______________________________________________ > > > Asterisk-java-users mailing list > > > Ast...@li... > > > https://lists.sourceforge.net/lists/listinfo/asterisk-java-users > > >=20 > >=20 > >=20 > -------------------------------------------------------------- > ----------- > > This SF.net email is sponsored by: Splunk Inc. > > Still grepping through log files to find problems? Stop. > > Now Search log events and configuration files using AJAX=20 > and a browser. > > Download your FREE copy of Splunk now >> http://get.splunk.com/ > > _______________________________________________ > > Asterisk-java-users mailing list > > Ast...@li... > > https://lists.sourceforge.net/lists/listinfo/asterisk-java-users >=20 >=20 > -------------------------------------------------------------- > ----------- > This SF.net email is sponsored by: Splunk Inc. > Still grepping through log files to find problems? Stop. > Now Search log events and configuration files using AJAX and=20 > a browser. > Download your FREE copy of Splunk now >> http://get.splunk.com/ > _______________________________________________ > Asterisk-java-users mailing list > Ast...@li... > https://lists.sourceforge.net/lists/listinfo/asterisk-java-users >=20 |
From: Stefan R. <ste...@re...> - 2007-09-05 20:33:50
Attachments:
signature.asc
|
Hi, you seem to be mixing the live API and the manager API. While this is generally possible I think it would be better to just stick with the live API. You are adding a callback to the ConnectEvent, so maybe the best solution would be to provide a way to register a few more callbacks like for connect and disconnect in the live API. The problem in this case is that a ManagerAction is send from the event dispatching thread which is not allowed. Generally the live API handles this problem by using a ManagerEventListenerProxy but by registering your own listener with the underlying connection you prevent this from working correctly. So what you have to do is to decouple receiving the event and sending the action on your own. One solution is starting a new Thread as proposed by Martin. Another solution is to use ManagerEventListenerProxy for your own listener, too. This might look like this: Instead of managerConnection.addEventListener( new ConnectEventListener(asteriskserver)); use managerConnection.addEventListener(new ManagerEventListenerProxy( new ConnectEventListener(asteriskserver))); =3DStefan |
From: Henrik B. <hen...@fr...> - 2007-09-06 10:18:02
|
Thank you both for the fast answers. The solution with the ManagerEventListenerProxy does it for me. Gretings Henrik Am Mittwoch, den 05.09.2007, 22:34 +0200 schrieb Stefan Reuter: > Hi, > > you seem to be mixing the live API and the manager API. While this is > generally possible I think it would be better to just stick with the > live API. You are adding a callback to the ConnectEvent, so maybe the > best solution would be to provide a way to register a few more callbacks > like for connect and disconnect in the live API. > > The problem in this case is that a ManagerAction is send from the event > dispatching thread which is not allowed. Generally the live API handles > this problem by using a ManagerEventListenerProxy but by registering > your own listener with the underlying connection you prevent this from > working correctly. > So what you have to do is to decouple receiving the event and sending > the action on your own. One solution is starting a new Thread as > proposed by Martin. Another solution is to use ManagerEventListenerProxy > for your own listener, too. > This might look like this: > > Instead of > > managerConnection.addEventListener( > new ConnectEventListener(asteriskserver)); > > use > > managerConnection.addEventListener(new ManagerEventListenerProxy( > new ConnectEventListener(asteriskserver))); > |