Re: [Asterisk-java-users] Concurrent accesstoManagerConnectioninstance
Brought to you by:
srt
From: Martin S. <ma...@be...> - 2008-04-18 20:00:52
|
Hi Maciek, ManagerConnection will some concurrency attention -- it will block back and forth if you send and receieve an event on the same thread. Have you looked at using org.asteriskjava.manager.ManagerEventListenerProxy? It listens for events and dispatches them asynchronously in another thread with a single-threaded executor. You could easily probably easily modify it to dispatch to your threadpool of handlers with a multithreaded executor. Alternatively, why not use the Live API and then make a thread for a handler, and have each handler add a listener for events to be fired on the agent -- and then fire once on startup for everything that pre-exists your execution of the program: import java.util.Collection; import org.asteriskjava.live.*; import org.asteriskjava.live.internal.AsteriskAgentImpl; public class TestCase implements AsteriskServerListener { static final String HOST = ""; static final String USERNAME = ""; static final String PASSWORD = ""; public AsteriskServer asteriskServer; public TestCase() throws ManagerCommunicationException { asteriskServer = new DefaultAsteriskServer(HOST, USERNAME, PASSWORD); asteriskServer.addAsteriskServerListener(this); } public void onNewAsteriskChannel(AsteriskChannel channel) { System.out.println("New channel:\n" + channel); } public void onNewMeetMeUser(MeetMeUser user) { System.out.println("New meet me user:\n" + user); } public void onNewQueueEntry(AsteriskQueueEntry entry) { System.out.println("New queue entry:\n" + entry); } public void onNewAgent(AsteriskAgentImpl agent) { System.out.println("New agent:\n" + agent); } // dispatch your handler with agent object public static void main(String[] args) throws Exception { TestCase tc = new TestCase(); Collection<AsteriskAgent> agents = tc.asteriskServer.getAgents(); for(AsteriskAgent agent : agents) tc.onNewAgent((AsteriskAgentImpl)agent); while(true) { Thread.yield(); } } } The live API is much sweeter :) Martin Smith, Systems Developer ma...@be... Bureau of Economic and Business Research University of Florida (352) 392-0171 Ext. 221 ________________________________ From: ast...@li... [mailto:ast...@li...] On Behalf Of Maciek Tokarski Sent: Friday, April 18, 2008 2:11 PM To: ast...@li... Subject: Re: [Asterisk-java-users] Concurrent accesstoManagerConnectioninstance Hi, Examples of code: Main class: //////////// public class LiveMonitoring ManagerEventListener { (...) ManagerConnection connection; (...) Public void LiveMonitoring() { ManagerConnectionFactory manConnFact = new ManagerConnectionFactory("dialer.telebot.pl", "dialer", "dialer"); this.connection = manConnFact.createManagerConnection(); this.connection.addEventListener(this); try { this.connection.login(); } catch (Exception ex) { JOptionPane.showMessageDialog(null, "Connection error.. exiting"); System.exit(0); } } Public void event() { ///this event can be called several times SateliteClass SClass= new SateliteClass(this.connection); } } ////////Satelite class public class SateliteClass implements Runable { ManagerConnection connection; Public void SateliteClass(ManagerConnection) { This.run(); this.connection=connection; } Public synchronized void someMethod() { AgentsAction agentsAction = new AgentsAction(); try { ResponseEvents events = connection.sendEventGeneratingAction(agentsAction, 5000); /////[1] Collection<ResponseEvent> agents = events.getEvents(); Iterator<ResponseEvent> itr = agents.iterator(); while (itr.hasNext()) { ResponseEvent ev1 = itr.next(); if (ev1 instanceof AgentsEvent) { AgentsEvent ev = (AgentsEvent) ev1; if (ev.getAgent().equals(agentNo)) { //processing agent events } } } } catch (IllegalStateException ex) { ex.printStackTrace(); } catch (IllegalArgumentException ex) { ex.printStackTrace(); } catch (EventTimeoutException ex) { ex.printStackTrace(); } catch (IOException ex) { ex.printStackTrace(); } } } } I've located reason of hanging up application- it was Timeout while waiting for response for AgenstAction. Does it have any relation with multithread architecture and concurrent access to ManagerConnectio9 object? Maybe I'm doing something wrong... Regards, Maciek ________________________________ From: ast...@li... [mailto:ast...@li...] On Behalf Of Martin Smith Sent: Wednesday, April 16, 2008 7:22 PM To: ast...@li... Subject: Re: [Asterisk-java-users] Concurrent access toManagerConnectioninstance We might need an example of your code to explain why it doesn't work. Also, the Live API does pretty much what you're describing, including the threading problems. Martin Smith, Systems Developer ma...@be... Bureau of Economic and Business Research University of Florida (352) 392-0171 Ext. 221 ________________________________ From: ast...@li... [mailto:ast...@li...] On Behalf Of Maciek Tokarski Sent: Wednesday, April 16, 2008 11:38 AM To: ast...@li... Subject: [Asterisk-java-users] Concurrent access to ManagerConnectioninstance Hey all, I'm trying to build multithread app. Architecture is as follows: -one main Thread, that does creation of ManagerConnection and login stuff -several "satellite" threads that should have access to previously initialized ManagerConnection object (they have to send some events to Asterisk box) ManagerConnection object is passed to "satellite" threads in constructor and reference to it is assigned to satellite-class member. When I'm trying to send event (I'm doing it in synchronized way) by eg. invoking sendEvent method- application totally hangs up (main thread and child threads). What is the reason and what can be solution. Regards, Maciek |