Creating Factory classes in BlazeDS

What is factory mechanism

BlazeDS provides a factory mechanism that lets you plug in your own component creation and maintenance system to BlazeDS so it integrates with systems like EJB and Spring, which store components in their own namespace.

A factory is responsible for creating the instance that should be used for a request to a destination. When a destination gets a request, destination’s adapter will invoke the configured factory for the destination. When the factory is invoked it should return the instance that should be used by the adapter to process the request.

You can see where exactly a factory is used by the Remoting service at this URL https://sujitreddyg.wordpress.com/2009/01/20/how-remoting-service-in-blazeds-works/

Let’s get into details 🙂

Creating a factory

Creating a factory and mapping it to a destination is 3 step process

Create factory class

You just have to create a class which will implement the “flex.messaging.FlexFactory” interface.

Methods to be implemented:

Initialize() – this method will be invoked when the factory class is instantiated. This happens only once.

createFactoryInstance() – this method is invoked for each reference of the factory in a destination. You will get properties declared in the destination as arguments to this method. Every factory class implementing FlexFactory can have a factory instance class extending “flex.messaging.FactoryInstance”. If you have a instance class for your factory create an instance of it and return the same in this method.

Since there is only one instance of the factory class, you can use this factory instance to create instances specific to a destination.

lookUp() – this method is invoked each time there is a request to a destination. If you have a factory instance created and have the lookUp() method overridden in that class, then this will not be invoked. You will receive the instance of the factory instance class returned in the createFactoryInstance() method as argument to this method. You should return the instance of the object that should be used by the adapter.

You can find a sample implementation below.

Create factory instance class

You have to create a class which extends the “flex.messaging.FactoryInstance” class. You should override the constructor. You can override the lookUp() method if you want to do destination based logic in this method. If you don’t override this method, then the lookUp() method in the factory class will be invoked. You should return the instance of the object that should be used by the adapter.

You can find a sample implementation below.

Declaring factory in services-config.xml

To declare a factory class, you should add a tag in the services-config.xml file as shown below. Add the “factories” node under the “service-config” node.

The declaration below is for the sample factory class.

<factories>

<factory id=”MyCustomFactory” class=”com.adobe.factories.MyFactory” />

</factories>

Mapping factory to a destination

In order to set a factory for a destination, you should declare that in the configuration file. In this sample we will change the factory of a Remoting destination. Append the node below in the remoting-config.xml under “service” node.

You can see that we have custom properties in the destination below we will be retrieving those in our factory instance.

<destination id=”CustomFactoryDestination”>

<properties>

<factory>MyCustomFactory</factory>

<source>This is the source</source>

<scope>request</scope>

<mypassedproperty>Sujit Reddy G</mypassedproperty>

</properties>

</destination>

Deploying and testing the factory

Explanation for the samples

We created a factory instance which will get the properties from the configuration file of the destination and then create an instance of “FactoryTesting” class. When we invoke getMe() method on the destination, the method is actually invoked on the instance returned by the factory instance class.

Download the following files

MyFactory.java

MyFactoryInstance.java

FactoryTesting.java

FactoryDestinationTesting.mxml

1.       Copy the .class files of MyFactory.java, MyFactoryInstance.java and FactoryTesting.java files in appropriate folders based on the packages.

2.       Add factory declaration in service-config.xml and declare destination in remoting-config.xml as mentioned above

3.       Create a Flex project pointing to the BlazeDS/LCDS where we have the classes deployed and factory configured.

4.       Use the FactoryDestinationTesting.mxml to invoke and see that the object instance created in the factory instance created is on which the method is invoked.

That’s it 🙂 Adobe rocks 🙂

Leave a comment