The Application Adapter - rajdeeprath/red5-development-series GitHub Wiki

Now that we are all set up with the basics and know about the basic components of a red5 application, its time to get our hands dirty with some true red5 coding. An obvious question at this time would be – “where do I start ?”. If you followed the previous three approaches for creating a red5 application , you will notice that there is a main class called Application.java which extends Red5’s MultiThreadedApplicationAdapter class. This class is the entry point and a hook for your application.

You will also remember that this class is registered and referenced in the red5-web.xml file as a web.handler bean. This could be any custom class of yours as long as it extends MultiThreadedApplicationAdapter.

<bean id="web.handler" class="org.red5.core.Application" />

This class handles all of your application resource requests, such as Connections, SharedObjects, Streams etc. I would re-iterate that this class I like a hook, which lets you tap into different events happening in your application, alongside creating your own logic / behaviour.

Like any other java class, an inheritance brings along with it a set of parent methods / events which can be overridden in the inheriting class. Same is for the red5 application class. you can tap into various handlers, methods, properties etc which have originally been defined by the parent class – MultiThreadedApplicationAdapter.

If we go back into eclipse and open the application class file – Application.java (as setup before), we can see 4 generated handler methods.

@Override
public boolean appConnect(IConnection arg0, Object[] arg1) {
// TODO Auto-generated method stub
return super.appConnect(arg0, arg1);
}

@Override
public void appDisconnect(IConnection arg0) {
// TODO Auto-generated method stub
super.appDisconnect(arg0);
}

@Override
public boolean appStart(IScope arg0) {
// TODO Auto-generated method stub
return super.appStart(arg0);
}

@Override
public void appStop(IScope arg0) {
// TODO Auto-generated method stub
super.appStop(arg0);
}

EXPLANATION:

  • appConnect : Handles any connection attempts to the application. The first argument ‘IConnection’ object identifies the client connection and properties associated with it. The second parameter is an array of optional parameters passed in by the connecting client such as authentication details etc:

  • appDisconnect : Handles client disconnects from the application. This would happen when a client disconnects gracefully / forcefully. This method can be useful to ‘do something’ when a client disconnects from the application.

  • appStart : Handles the application startup. The ‘IScope’ parameters refer to its scope object in the context of the entire Red5 server. Each application is a scope, unique in the system. We will see more about scopes later. This is the root scope of your application. Every resource of your application is enveloped inside it. In other words, resources such as streams, connection, shared objects for this application can all be accessed through this ‘IScope’ object.

  • appStop : Handles application stop. However, be advised that this method should not be used as per its semantics. Java web servers have a complex system for shutdown mechanisms. Do not try to execute critical database commands in this method – your database connection might already have been destroyed before this executes.


Similarly to see what other handlers are available for use, you can do the following:

  • In eclipse, right click in the code area of the Application class, Select -> Source -> Override/ Implement Methods

Eclipse implement overridden methods

  • You can then select the methods that you wish to override / implement in your application class and click on “Ok”.

Eclipse method selection

  • Your application class will now be populated with the new methods. Check out the details of what each method does and how you can use them, by looking at the official documentation:

http://red5.org/javadoc/red5-server/org/red5/server/adapter/MultiThreadedApplicationAdapter.html

We shall take a look at methods and handlers of the Application class as we progress through this series.