Tutorial on Linux + Asterisk +MySQL + Java – part 5

This part of the tutorial will talk of the Asterisk-Java’ EventListener.

Beside the cool stuff we have previously seen about the Manager API and Live API, Asterisk-java offers some EventListeners to add more dynamic control over Asterisk behaviour. Some of this listeners belongs to the Manager API, and others to the Live API.
To use the Manager API EventListener, you may:

    implement the ManagerEventListener interface
    extend the AbstractManagerEventListener class

We will use the second method in this tutorial. To implement the listener, you have to:

  1. connect to the Asterisk server (as done in the previous parts)
  2. instantiate your class and add it as listener to the connection
  3. override/implement the method required to handle specific events; keep in mind that ManagerEventListener interface define only the
    onManagerEvent(ManagerEvent event)
    method (called when an event is received) while AbstractManagerEventListener already implements this method and defines all the required methods to manage every specific event (but this methods are empty and should be overridded)
  4. In this example, we will do all the connection setup in the constructor. Here’s the full code of the test class. Please note that this is just for demonstration, so forgive the *ugly* endless loop in the main method. Moreover, I have only overridden a couple of methods related to MeetMe join and leave events, but in the org.asteriskjava.manager.event package you may see all of them.

    package net.cardosi.asterisk.ami;
    
    import org.asteriskjava.manager.AbstractManagerEventListener;
    import org.asteriskjava.manager.ManagerConnection;
    import org.asteriskjava.manager.ManagerConnectionFactory;
    import org.asteriskjava.manager.event.MeetMeJoinEvent;
    import org.asteriskjava.manager.event.MeetMeLeaveEvent;
    
    public class AMIListenerTest extends AbstractManagerEventListener {
    
    	public AMIListenerTest() {
    		// Instantiate the factory
    		ManagerConnectionFactory factory = new ManagerConnectionFactory(
    				"localhost", "admin", "secret5");
    		// Retrieve the connection from the factory
    		ManagerConnection managerConnection = factory.createManagerConnection();
    		try {
    			// login to Asterisk
    			managerConnection.login();
    			// Add this object as listener to the connection
    			managerConnection.addEventListener(this);
    		} catch (Exception e) {
    			// Manage exception
    			e.printStackTrace();
    		}
    	}
    
    	@Override
    	protected void handleEvent(MeetMeJoinEvent event) {
    		System.out.println(event);
    	}
    
    	@Override
    	protected void handleEvent(MeetMeLeaveEvent event) {
    		System.out.println(event);
    	}
    
    	public static void main(String[] args) {
    		new AMIListenerTest();
    		while (true) {
    		}
    	}
    }
    

    To use the Live API EventListener, you may:

      implement the AsteriskServerListener interface
      extend the AbstractAsteriskServerListener class

    We will use the second method in this tutorial. The step are pretty similar as in the previous example, but now we will show you how to directly instantiate a DefaultAsteriskServer. With the Live API you can only listen for a small set of Asterisk events, but each of this event will provide a LiveObject to which, in turn, you may add a PropertyChangeListener to monitor what happen to it. The only method required to implement PropertyChangeListener is

    public void propertyChange(PropertyChangeEvent propertyChangeEvent).
    Our example class will both extend AbstractAsteriskServerListener and implement PropertyChangeListener:

    package net.cardosi.asterisk.live;
    
    import java.beans.PropertyChangeEvent;
    import java.beans.PropertyChangeListener;
    import org.asteriskjava.live.AbstractAsteriskServerListener;
    import org.asteriskjava.live.AsteriskChannel;
    import org.asteriskjava.live.AsteriskQueueEntry;
    import org.asteriskjava.live.AsteriskServer;
    import org.asteriskjava.live.DefaultAsteriskServer;
    import org.asteriskjava.live.MeetMeUser;
    import org.asteriskjava.live.internal.AsteriskAgentImpl;
    
    public class LiveListenerTest extends AbstractAsteriskServerListener implements
    		PropertyChangeListener {
    
    	public LiveListenerTest() {
    		AsteriskServer asteriskServer = new DefaultAsteriskServer("localhost",   "admin", "secret5");
    		asteriskServer.addAsteriskServerListener(this);
    	}
    
    	public void onNewAsteriskChannel(AsteriskChannel channelParam) {
    		System.out.println(channelParam);
    		channelParam.addPropertyChangeListener(this);
    	}
    
    	public void onNewMeetMeUser(MeetMeUser userParam) {
    		System.out.println(userParam);
    		userParam.addPropertyChangeListener(this);
    	}
    
    	public void onNewAgent(AsteriskAgentImpl agentParam) {
    		System.out.println(agentParam);
    		agentParam.addPropertyChangeListener(this);
    	}
    
    	public void onNewQueueEntry(AsteriskQueueEntry entryParam) {
    		System.out.println(entryParam);
    		entryParam.addPropertyChangeListener(this);
    	}
    
    	public void propertyChange(PropertyChangeEvent propertyChangeEvent) {
    		System.out.println(propertyChangeEvent);
    	}
    
    	public static void main(String[] args) {
    		new LiveListenerTest();
    		while (true) {
    		}
    	}
    }
    

    “A “PropertyChange” event gets delivered whenever a bean changes a “bound” or “constrained” property. A PropertyChangeEvent object is sent as an argument to the PropertyChangeListener and VetoableChangeListener methods.” (quoted). So, whenever the state of the LiveObject change, you may be informed of what and how changed. If you try the above code, for example connecting a client to a MeetMe conference, you will see all the events and notifications that will be triggered.

    … and that’s all, folks!!!
    I hope you enjoyed this tutorial.
    Bye

    Author: gabrio12

    Java developer, amateur recorder player, what else...?

One thought on “Tutorial on Linux + Asterisk +MySQL + Java – part 5”

  1. Hello!
    Try to use the frist and second code. This show me the current channels when run, but no any new channels.
    What’s wrong?

    Like

Leave a comment