Pages

Friday, July 11, 2008

Using EJB in Flex with blazeds

This blog explains how you can use ejb in Adobe Flex with blazeds. First thing I did is creating a application with the jsf, ejb template.

In the model project we can create a new entity bean. I use the CMP entity bean from tables.


For the EJB version I use Enterprise JavaBeans 2.1 option. The Oracle table I will use in this entity bean is the employee table of the HR schema.

Now we can create a Session Bean
If we look at the properties of the session bean we see the retrieveAllEmployees method. We will use this to display all the employees in flex.

We are ready to add the blazeds libraries to the viewcontroller project. Because we want to use EJB, we have to download an extra library. Here is the link of the ejb library which is made by Ryan J. Norris. Add also this library to the viewcontroller project.
Because we use ejb we have to add the embedded oc4j client and j2ee library to viewcontroller project.


Now we can configure blazeds. We have to create two files (remoting-config.xml and services-config.xml ) in WEB-INF/flex/ folder
Here is the remoting-config.xml file. In this file we define a destination with the name of the session bean in the model project.


<?xml version="1.0" encoding="UTF-8"?>
<service id="remoting-service" class="flex.messaging.services.RemotingService">
<adapters>
<adapter-definition id="java-object"
class="flex.messaging.services.remoting.adapters.JavaAdapter"
default="true"/>
</adapters>
<default-channels>
<channel ref="my-amf"/>
</default-channels>
<destination id="EmployeeEJB">
<properties>
<factory>ejb</factory>
<source>SessionEJB</source>
</properties>
</destination>
</service>

Here is the services-config.xml. In this file we define the ejb factory in my case I have to use com.adobe.ac.ejb.EJBFactory for ejb3 we have to use com.adobe.ac.ejb.EJB3Factory as factory class.

<?xml version="1.0" encoding="UTF-8"?>
<services-config>
<services>
<service-include file-path="remoting-config.xml"/>
<default-channels>
<channel ref="my-amf"/>
</default-channels>
</services>
<factories>
<factory id="ejb" class="com.adobe.ac.ejb.EJBFactory" />
</factories>
<channels>
<channel-definition id="my-amf"
class="mx.messaging.channels.AMFChannel">
<endpoint url="http://{server.name}:{server.port}/{context.root}/messagebroker/amf"
class="flex.messaging.endpoints.AMFEndpoint"/>
<properties>
<polling-enabled>false</polling-enabled>
</properties>
</channel-definition>
</channels>

<logging>
<target class="flex.messaging.log.ConsoleTarget" level="Error">
<properties>
<prefix>[Flex] </prefix>
<includeDate>false</includeDate>
<includeTime>false</includeTime>
<includeLevel>false</includeLevel>
<includeCategory>false</includeCategory>
</properties>
</target>
</logging>
</services-config>

Add the blazeds servlet to the web.xml

<servlet>
<servlet-name>MessageBrokerServlet</servlet-name>
<servlet-class>flex.messaging.MessageBrokerServlet</servlet-class>
<init-param>
<param-name>services.configuration.file</param-name>
<param-value>/WEB-INF/flex/services-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>MessageBrokerServlet</servlet-name>
<url-pattern>/messagebroker/*</url-pattern>
</servlet-mapping>

<listener>
<listener-class>flex.messaging.HttpFlexSession</listener-class>
</listener>

The last step in jdeveloper is to add -Doc4j.jmx.security.proxy.off=true to the run options of the viewcontroller project.

Let's open Flex builder 3 and create a new project.

We have to select j2ee server technology.

The second step is to add the webapp url's.
The mxml is very simple. First we add a remoteobject, then we are calling theretrieveAllEmployees method when the application is loaded and at last we display the result in a datagrid.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
width="1155" height="206.21213">
<mx:applicationComplete>
srv.retrieveAllEmployees()
</mx:applicationComplete>

<mx:RemoteObject id="srv" showBusyCursor="true"
destination="EmployeeEJB"/>
<mx:DataGrid width="1104.6212"
dataProvider="{srv.retrieveAllEmployees.lastResult}"
height="140.98485"/>

</mx:Application>

Here the result. That's all.

10 comments:

  1. Hi! This guide seems to be very helpful and simple, but i can't understand which software/plugin you are using on the first screenshot. Is that a plugin for Eclipse? Thanks!

    ReplyDelete
  2. Hi, The picture is a wizard in JDeveloper.

    maybe Oracle will make more plugin for eclipse now they own BEA

    thanks

    ReplyDelete
  3. JDeveloper works only with Oracle DB? Could i use it with a SQL Server DB?

    Thanks!

    ReplyDelete
  4. Hi jdev can work perfectly with any database. just use the right jdbc driver.

    only adf bc can not work 100% , but you use ejb.

    thanks

    ReplyDelete
  5. I have doubt in blazeDS.

    i am trying to connect the EJB and Flex with BlazeDS.

    i am going to write the EJB using Rational application developer and
    Flex using Flex Builder.

    the database i am going to use is DB2,

    is it possible to connect.

    please share your experience

    thanks
    areef

    ReplyDelete
  6. is it possible to plug-in the BlazeDS in Rational Application Development.

    thanks
    areef

    ReplyDelete
  7. Hi areef,

    Blazeds should work in every j2ee container. The only thing you should do is to add a servlet to the web.xml and add some folder with config files and lib to the web-inf folder ,

    And the ejb should work also.

    thanks

    ReplyDelete
  8. hi biemond.. why don't you give us the full source code of projects. when we use other IDEs except for JDeveloper, it becomes difficult to imagine the whole part especially for newbees.
    thanks

    ReplyDelete
  9. If you need support for stateful session beans have a look at http://dev.3m5-extra.net/blog/2010/06/04/ejb3factory-for-blazeds/

    ReplyDelete
  10. Hi Dan,

    Good work. Maybe you can also take a look at Remote Development Services in FB4. RDS does not work in FB4 with EJB. Maybe you can make it work.

    thanks
    http://biemond.blogspot.com/2010/05/using-remote-development-services-in.html

    ReplyDelete