Externalize your AGI Configuration

Using a dependency injection framework like the Spring Framework
to exernalize your configuration is often a great enhancement for maintainance and deployment of your application.

If you are building an AGI application using Asterisk-Java the following code snippets will show you
how do it:

Build your AGI script as before:

public class HelloAgi extends BaseAgiScript
{
    private String voicePrompt;

    public void setVoicePrompt(String voicePrompt)
    {
        this.voicePrompt = voicePrompt;
    }

    public void service(AgiRequest request, AgiChannel channel) throws AgiException
    {
        streamFile(voicePrompt);
    }
}

This is really a very simple one but note the property voicePrompt that we added.
This property will be configured using Spring at deploy-time.

Next define an ApplicationContext (usually an XML file somewhere on the classpath):

<beans xmlns="...">

	<bean id="agiServer" class="org.asteriskjava.fastagi.DefaultAgiServer"
		init-method="startup" destroy-method="shutdown">
		<property name="mappingStrategy" ref="mappingStrategy" />
	</bean>

	<bean id="mappingStrategy" class="org.asteriskjava.fastagi.SimpleMappingStrategy">
		<property name="mappings">
			<map>
				<entry key="hello.agi" value-ref="helloAgi" />
			</map>
		</property>
	</bean>

	<bean id="helloAgi" class="HelloAgi">
		<property name="voicePrompt" value="tt-monkeys" />
	</bean>

</beans>

This context configures your AGI script and defines the mapping of URL to script instance.

Finally provide a main() method somewhere to start the context:

public class Main
{
    public static void main(String[] args)
    {
        new ClassPathXmlApplicationContext("context.xml").start();
    }
}

Now you are ready to run the application.

Follow Up:

No related posts.

5 thoughts on “Externalize your AGI Configuration

  1. Hi Stefan,
    Thank you for the example for integrating with Spring. I was curious if you have tried to run this inside a container, such as tomcat? I attempted your example above from within my tomcat 6 instance, and it works fine, except that it causes tomcat to hang on shutdown. As far as I can tell, the shutdown() method described in the destroy-method never gets called.

    I was curious if you had any insight into why that is.

    Great job with asterisk-java btw, I have found it tremendously useful.