Navigation:  Communication Libary > Supported Servers >

WebORB for Java

Previous pageReturn to chapter overviewNext page

WebORB for Java embeds the Red5 product which is a fully functional Java RTMP server. Any custom server-side functionality can be implemented in Java and deployed with WebORB into any Java web container. Alternatively, WebORB for Java can be executed standalone thus avoiding the container deployment step. To listen for the RTMP connections, WebORB for Java uses the port 1935.

Creating an RTMP application

Creating and registering an RTMP application is a two step process:

 

1.Create an XML file in [WEBOB-HOME-DIR]/WEB-INF/classes with the following format:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"
<beans>
   <bean id="XXXXXXX.context" class="org.red5.server.Context" autowire="byType" />
   <bean id="XXXXXXX.scope" class="org.red5.server.WebScope" init-method="register">
       <property name="server" ref="red5.server" />
       <property name="parent" ref="global.scope" />
       <property name="context" ref="XXXXXXX.context" />
       <property name="handler" ref="XXXXXXX.handler" />      
       <property name="contextPath" value="/XXXXXXX" />
       <property name="virtualHosts" value="*,localhost, localhost:1935, localhost:8080, 127.0.0.1:8080" />
   </bean>
   <bean id="XXXXXXX.handler" class="weborb.messaging.WebORBApplication" singleton="true" />    
</beans>

Make sure to put the application name everywhere where it shows "XXXXXXX". It is recommended to save the file with the name of YOURAPPNAME-web.xml.
 

2.Register the file in [WEBOB-HOME-DIR]/WEB-INF/classes/beanRefContext.xml by adding the following line in the <list> element:

<value>YOURAPPNAME-web.xml</value>

3.Restart WebORB for Java. You can verify that the application is registered in the WebORB Management Console. Open the console at http://localhost;8080, select  the Messaging Server tab. The application should appear in the list of applications. When connecting to the server, the name of the application (as specified in step 1) should be used in the connect call.

Custom Server-Side Code

A WebORB RTMP application can be extended with custom code containing the application logic. The code must be a Java class inheriting from weborb.messaging.WebORBApplication. Any public method added to the class can be invoked by the connected clients. To deploy the custom code, the compiled classes can be copied either into the WEB-INF/classes folder (the folder hierarchy must match the package structure) or in a jar file into WEB-INF/lib. The RTMP application configuration file must also reference the custom code class. See the first step in Creating an RTMP Application. The following line in the file must contain the fully qualified name of the class:

<bean id="XXXXXXX.handler" class="weborb.messaging.WebORBApplication" singleton="true" />

Where "XXXXXXX" is the name of the application.

Data Push (server-to-client calls)

WebORB for Java supports server to client invocation via the IServiceCapableConnection interface.The interface represents a connection between an RTMP client the current application. The code snippet shown below demonstrates how to invoke a method on all client connections when a client disconnects. Specifically, the code invokes the client-side "clientDisconnected" method with one argument - client's ID.

package demo;
 
import java.util.Collection;
import java.util.Set;
import org.red5.server.api.IClient;
import org.red5.server.api.IConnection;
import org.red5.server.api.IScope;
import org.red5.server.api.service.IServiceCapableConnection;
import weborb.messaging.WebORBApplication;
 
public class AppHandler extends WebORBApplication
{
 public void disconnect( IConnection conn, IScope scope )
 {
   Collection<Set<IConnection>> connections = scope.getConnections();
 
   while( connections.MoveNext() )
   {
     IConnection connection = connections.Current;
     Object[] args = new Object[] { conn.getClient().getId() };
 
     for( Set<IConnection> connectionsSet : connections )
       for( IConnection connection : connectionsSet )
         if( connection instanceof IServiceCapableConnection )
         {
           IServiceCapableConnection cnx = (IServiceCapableConnection) connection;
           cnx.invoke( "clientDisconnected", args );
         }
     }
  }
}

Remote Shared Objects

WebORB's server-side remote shared objects provide support for the following use-cases:

Obtaining a reference to a remote shared object
Adding shared object listener to receive notifications for updates, deletes and sent messages
Updating shared object property
Clearing/deleting all shared object properties
Sending a message to all shared object clients

 

Obtaining ISharedObject Reference

To obtain a reference to a remote shared object, use the getSharedObject method in the WebORBApplication subclass. The method returns an instance of the org.red5.server.api.so.ISharedObject interface. The object is a server-side representation of the shared object, it provides access to all the RSO functionality, including setting RSO properties, delivering messages to the clients, etc:

package demo;
 
import org.red5.server.api.IScope;
import weborb.messaging.WebORBApplication;
 
public class AppHandler extends WebORBApplication
{
 private ISharedObject myRSO;
 
 public void appStart( IScope app )
 {
   createSharedObject( scope, "chatRSO", false );
   myRSO = getSharedObject( scope, "chatRSO", false );
 
   return super.appStart( app );
 }
}

 

Adding Listener

In order to receive notifications about RSO property updates, as well as many other RSO-related events, a listener should be added to a remote shared object reference. RSO listeners must implement the org.red5.server.api.so.ISharedObjectListener interface. A listener can be added to an instance of ISharedObject using the addSharedObjectListener method:

package demo;
 
import org.red5.server.api.IScope;
import weborb.messaging.WebORBApplication;
 
public class AppHandler extends WebORBApplication
{
 private ISharedObject myRSO;
 
 public void appStart( IScope app )
 {
   createSharedObject( scope, "chatRSO", false );
   myRSO = getSharedObject( scope, "chatRSO", false );
   myRSO.addSharedObjectListener( new MySharedObjectListener() );
 
   return super.appStart( app );
 }
}
 
 class MySharedObjectListener : ISharedObjectListener
 {
   public void onSharedObjectConnect( ISharedObjectBase so )
     {
      // a client has connected to the shared object
     }
 
   public void onSharedObjectDisconnect( ISharedObjectBase so )
     {
      // a client has disconnected from the shared object
     }
 
   public void onSharedObjectUpdate( ISharedObjectBase so, string key, object value )
     {
      // a shared object property with the 'key' has been updated. The value is 'value'
     }
 
   public void onSharedObjectUpdate( ISharedObjectBase so, IAttributeStore values )
     {
      // shared object properties have been updated. The updated key/value pairs are in 'values'
     }
 
   public void onSharedObjectUpdate( ISharedObjectBase so, IDictionary<string, object> values )
     {
      // shared object properties have been updated. The updated key/value pairs are in 'values'
     }
 
   public void onSharedObjectDelete( ISharedObjectBase so, string key )
     {
      // a shared object property with the key 'key' has been removed
     }
 
   public void onSharedObjectClear( ISharedObjectBase so )
     {
      // all shared object properties have been cleared
     }
 
   public void onSharedObjectSend( ISharedObjectBase so, string method, IList parms )
     {
       // a client sent a message 'method' with parameters 'parms' to all the clients of the shared object identified by so
     }
  }
}

Updating Property

When a shared object property is updated, WebORB delivers a notification event to all clients connected to the shared object. To update a property, use the setAttribute method on the RSO reference. Consider the following example:

Hashtable messageObject = new Hashtable();
messageObject.put( "imText", messageText;
messageObject.put( "username", "Java Client" );
messageObject.put( "color", 0 );
messageObject.put( "isBold", false );
messageObject.put( "isItalics", false );
messageObject.put( "isUnderline", false );
myRSO.setAttribute( "UserMessage", messageObject );

The code above updates the "UserMessage" property of a remote shared object with an untyped object (represented as Hashtable). Alternatively, the code could be rewritten to use a strongly typed object:

InstantMessage messageObject = new InstantMessage();
messageObject.imText = messageText;
messageObject.username = "Java Client";
messageObject.color = 0;
messageObject.isBold = false;
messageObject.isItalics = false;
messageObject.isUnderline = false;
myRSO.setAttribute( "UserMessage", messageObject );
 
public class InstantMessage
{
 public String imText;
 public String userName;
 public int color;
 public boolean isBold;
 public boolean isItalics;
 public boolean isUnderline;
}

 

Deleting All Shared Object Properties

To delete all shared object properties use the clear method on the ISharedObject reference. See Obtaining ISharedObject Reference for details. When the method is invoked, WebORB notifies all other connected clients that the properties have been deleted.

 

Sending Messages To All RSO Clients

To delete all shared object properties use the sendMessage method on the ISharedObject reference. See Obtaining ISharedObject Reference for details. When the method is called, WebORB sends a notification event to invoke the method with the specified arguments on all clients connected to the remote shared object. See Server Data Push for additional details.