Synchronizing Data in Apollo using Flex Data Services

contacts3.gif

In my previous post, I provided an example of an Apollo application using the Flex Message Service. Since then, a number of people have asked for examples of Apollo applications accessing data using the Flex Data Management services. So here is a simple Contact Management application that demonstrates this integration.

Using the Data Services to work with data in Apollo offers several benefits. First, just like for traditional browser-based Flex apps, the Data Management Services automate the synchronization of data between the client and the middle-tier. In other words, you don’t have to flag/keep track of the changes made at the client-side, and then make corresponding RPC calls to send changes to the server: all that is managed automatically.

For this application, I actually used LiveCycle Data Services (the new name for Flex Data Services) 2.5 currently in beta 2, which offer additional benefits in the context of this application:

  • Using the new SQL assembler, you don’t have to write server-side components if you don’t want/need to: you just provide a series of SQL statements indicating how data should be retrieved, and how changes should be persisted in the database. The SQL assembler makes it extremely fast and easy to create applications that don’t require sophisticated persistence logic.
  • In 2.5, the DataService API provides methods (such as dataService.saveCache() / dataService.clearCache()) to save data locally and manipulate the local data store. You can also automatically synchronize the changes you made offline with the server. I ran into a bug in LCDS beta 2 that prevented me from implementing offline caching in this sample. The bug is fixed post beta 2 and I will share an updated version of the app when the bits become publicly available.

Installation instructions:

  1. Install LiveCycle Data Services 2.5 beta here
  2. Modify the flexdemosb database file to add the contact table: replace WEB-INF\db\flexdemodb\flexdemodb.script with this version.
  3. Open WEB-INF\flex\data-management-config.xml and add the following destination.
    <destination id="sql-contact">
    
    	<adapter ref="java-dao" />
    
    	<properties>
    
    		<use-transactions>true</use-transactions>
    		<source>flex.data.assemblers.SQLAssembler</source>
    		<scope>application</scope>
    		<metadata>
    			<identity property="CONTACT_ID"/>
    		</metadata>
    
    		<network>
    			<session-timeout>20</session-timeout>
    			<paging enabled="false" pageSize="10" />
    			<throttle-inbound policy="ERROR" max-frequency="500"/>
    			<throttle-outbound policy="REPLACE" max-frequency="500"/>
    		</network>
    
    		<server>
    
    			<database>
    			    <driver-class>org.hsqldb.jdbcDriver</driver-class>
    			    <!-- Modify the URL below with the actual location of the flexdemodb database on your system -->
    			    <url>jdbc:hsqldb:file:C:/lcds/jrun4/servers/default/samples/WEB-INF/db/flexdemodb/flexdemodb</url>
    			    <username>sa</username>
    			    <password></password>
    				<login-timeout>15</login-timeout>
    			</database>
    
    			<actionscript-class>Contact</actionscript-class>
    
     			<fill>
       				<name>all</name>
       				<sql>SELECT * FROM CONTACT</sql>
     			</fill>
    
    			<get-item>
    				<sql>SELECT * FROM CONTACT WHERE CONTACT_ID = #CONTACT_ID#</sql>
    			</get-item>
    
     			<create-item>
    			  <sql>INSERT INTO CONTACT
    			  		(FIRST_NAME,LAST_NAME,EMAIL,PHONE,ADDRESS,CITY,STATE,ZIP,COUNTRY,NOTES)
    			  		VALUES (#FIRST_NAME#, #LAST_NAME#, #EMAIL#, #PHONE#, #ADDRESS#, #CITY#, #STATE#, #ZIP#, #COUNTRY#, #NOTES#)</sql>
    			  <id-query>CALL IDENTITY()</id-query> <!-- HSQLDB syntax to retrieve value of autoincremented column -->
    			</create-item>
    
     			<update-item>
    			  <sql>UPDATE CONTACT SET FIRST_NAME=#FIRST_NAME#,LAST_NAME=#LAST_NAME#,EMAIL=#EMAIL#,PHONE=#PHONE#,ADDRESS=#ADDRESS#,CITY=#CITY#,STATE=#STATE#,ZIP=#ZIP#,COUNTRY=#COUNTRY#,NOTES=#NOTES#
    			  		WHERE CONTACT_ID=#_PREV.CONTACT_ID#</sql>
    			</update-item>
    
     			<delete-item>
    			  <sql>DELETE FROM CONTACT WHERE CONTACT_ID=#CONTACT_ID#</sql>
    			</delete-item>
    
    			<count>
    				<name>all</name>
    				<sql>SELECT count(*) FROM CONTACT</sql>
    			</count>
    
    		</server>
    
    	</properties>
    
    </destination>
    
  4. Start the server
  5. Install contacts.air and run the application

NOTE: The application has been compiled assuming that an RTMP endpoint named my-rtmp is configured at rtmp://localhost:2037. If your server isn’t configured that way, you will have to recompile the application. You can also provide the endpoints at runtime. See my previous post for more information on defining a ChannelSet.

You can download the source code here.

27 Responses to Synchronizing Data in Apollo using Flex Data Services

  1. Adam Adamczyk March 31, 2007 at 1:01 pm #

    Christoper,

    You have missed channel definition in your destination “sql-contact” settings. At least one channel must be defined. JRun trow following error

    03/31 21:56:58 user MessageBrokerServlet: **** MessageBrokerServlet failed to in
    itialize due to runtime exception: flex.messaging.config.ConfigurationException:
    Destination ‘sql-contact’ must specify at least one channel.

  2. najier April 11, 2007 at 8:04 am #

    Not bad. But this is too simple. I wonder if Christopher can increase the complexity a little bit to to work with a small object graph. How about grouping the contacts, so we have another object called contact group which has a list of contacts and also factor out the address so that a contact is associated with an address (a shared object). Contacts are lazy loaded.

    This would make a decent practical real-world example and will save thousands of suffering hours by developers.

    …najier

  3. Samuel Flores April 19, 2007 at 11:45 am #

    Could you post the services-config.xml file you used for that application? I cannot compile the it and I’m not sure it’s the cause but I would like to look your configs. Thanks.

  4. Sebastien Denis April 24, 2007 at 7:11 am #

    I just started my first SQLAssembler app.
    I managed one table. But how do you suggest to manage sub-table (for instance contacts friends).

    In the same DataService (destination)? Then same AS class “Contact.as”. But how to delete reference to one friend? And how doing the binding in the data-management.xml?
    Or in another DataService (destination)?
    Or SQLAssembler is not suitable for this…

    Thanks Christophe…

  5. Brian Cragin April 25, 2007 at 4:59 pm #

    Greetings,

    I have visited your site several times and have enjoyed your comments. My company is looking to bolster our web capability (RIAs) and are looking at adding a position that is versed in flex and other relavent programming languages. What type of skillsets would be appropriate or common among flex developers and at what salary do they usually run. I’m sure the answer depends on many variables which I have not described, but we aren’t looking for third-base, only the ballpark. Any information you can provide would be valuable.

  6. Cameron Collins April 30, 2007 at 11:12 am #

    I am about to start a multi-year project that will use Flex as the presentation layer technology. However I find it difficult to find any contacts at Adobe to help build the business case for using Flex. Please let me who I need to contact.

    Thanks

  7. Nabeel Kamboh May 24, 2007 at 11:14 am #

    I was able to run the application with FDS successfully. I was also able to port the FDS code to run it on Tomcat.
    When I create a new apollo application in eclipse using the source code provided, I get an error in the service-config.xml. Turns out that channels now need to specify uri instead of url, also the default-channel needs to be included in each service not in the services block, also each service must have a messageTypes attribute.

    I made all of these changes and the app still works with the FDS. However, when I create a new Apollo application and run the apollo app using the eclipse builder, I get the following error:

    Error:[FaultEvent fault=[RPC Fault faultString="Send failed" faultCode="Client.Error.MessageSend" faultDetail="Channel.Connect.Failed error undefined url:'rtmp://ewApp.swf:2037'"] messageId=”18583AC9-C52C-9DA6-5F4C-BF42E2CC45CC” type=”fault” bubbles=false cancelable=true eventPhase=2]

    I have the FDS server running and working with the installed air file provided but I get an error when creating a project and running it in eclipse. I am connecting to the same fds provider as the app and doing the same fill operation

    Please advise.

  8. gxwzyrcldw June 21, 2007 at 10:29 pm #

    Hello! Good Site! Thanks you! dnprtvfcklg

  9. Ahmed August 14, 2007 at 5:35 am #

    Hello Christophe,

    First of all before I introduce myself I would like to compliment you on your amazing efforts in helping developers understand the true powers of Flex 2.0 and AIR. I am a big fan and found of your work on Adobe and your Blog. I have been working on my own in understanding the in-n-outs of Flex and how to create real powerful enterprise applications. I am strong follower of your work on Adobe and your own blog.

    I was trying your app the restaurant app. Finder posted on 15th March 2004. Currently on the Adobe web site. I have combined both the Java test drive with the restaurant app to create a working environment. Your work as helped me a great deal. I have been studying both of your apps, the restaurant search app (url) and the Java test drive. I am very grateful for your amazing efforts. In my learning process I found that your restaurant application is obviously written for the older version of Flex with Macromedia xmlns. I would like to repay your efforts in providing you with my update of the restaurant app to comply with Flex 2.0 in updating all the files including action scripts and functions even creating bind able custom events for the area search and review addition.

    I would be extremely honored to have my updated code reviewed by the top Flex evangelist world wide. Please find attached the following source files and the document that contains the change log on each exercise steps published on the web.

    Please let me know how I can email you my revised code? I was able to complete the restaurant app. But I did not have enough time to convert the restaurant admin app. I would like to complete it when the wife is not looking :p

  10. Robert September 4, 2007 at 10:31 am #

    can you post the contacts.air for air runtime and not for apollo,please?

  11. sathish September 19, 2007 at 12:40 am #

    superbuuuuuuuuuuuuuuuuuu

  12. davis September 20, 2007 at 9:03 pm #

    when i use SQLAssembler on mySQL, it can’t display the chinese or janpanes with utf8 encoding on Flex DataGrid. would you please do me a favor?

  13. Nathalie Roman October 2, 2007 at 7:17 am #

    Hi Christophe,

    We’ve met a few weeks ago in Belgium concerning the usage of Flex Data Management Services for our data-driven application.

    This meeting was very useful to have an in-depth view on the most important features of FDMS.

    But I still have a few questions, otherwise it would be to perfect wright ;o))

    My questions:
    First what’s documented in flex2_devguide:
    ‘The Realtime Message Protocol (RTMP) channel maintains a connection between the client
    and the server, so the client does not have to poll the server. The Action Message Format
    (AMF) channel with polling enabled polls the server for new messages.’

    What’s confusing here is that FDMS works with a push-system to synchronize data instead of a poll-system – which is mentioned in the text above – to make sure that all client-app’s are working on the same up-to-date data.

    Could you elaborate if the usage of sprecific channel-types can interfere with the synchronization-mechanism of FDMS?

    Kind regards,
    Nathalie

  14. chat August 22, 2008 at 6:43 am #

    super! :)

  15. sohbet August 22, 2008 at 6:44 am #

    Great ;)

  16. çet August 22, 2008 at 6:45 am #

    Güzel bilgiler.

  17. sohbet April 2, 2009 at 9:24 am #

    when i use SQLAssembler on mySQL, it can’t display the chinese or janpanes with utf8 encoding on Flex DataGrid. would you please do me a favor?

  18. Srikanth August 6, 2009 at 1:36 am #

    I am not able to download source code , if any one have source code please send me at srikanthnakka@gmail.com

  19. sohbet January 10, 2010 at 3:29 am #

    yah good post very nice For Admin..

  20. matbaa August 8, 2010 at 8:19 am #

    can you post the contacts.air for air runtime and not for apollo,please?

  21. kablo tv October 9, 2010 at 12:20 pm #

    very nice

  22. Norman Homme June 17, 2011 at 12:33 am #

    Hey, I tried to e-mail you pertaining to this submit but aren?t in a position to attain you. Please e-mail me when get a second. Thanks.

  23. flat rent jesmond August 11, 2011 at 1:37 am #

    hi do u know that, Adobe made a public preview release of AIR (then called Apollo) along with a software development kit (SDK) and extension for developing Apollo applications with the Flex framework, on March 19, 2007.

  24. JuniorsSexyStylishBathroomIsTh March 23, 2013 at 8:06 am #

    You have mentioned very interesting points! ps nice internet site. Juniors Sexy Stylish Bathroom Is The Specialty Swi http://www.kaodu.com/plus/view.php?aid=52666

Trackbacks/Pingbacks

  1. Synchronizing Data in AIR | Adobe AIR Tutorials - August 26, 2007

    [...] more @ http://coenraets.org/blog/2007/03/synchronizing-data-in-apollo-using-flex-data-services/ [...]

  2. Christophe Coenraets » Offline Synchronization using AIR and LiveCycle Data Services - October 26, 2007

    [...] Many people have asked me to share the code, so here it is… This is actually an update of an application I posted a few months ago (but that didn’t include the offline synchronization part at the [...]

  3. Adobe LCDS « SrikanthCreative Mind’s Weblog - October 27, 2008

    [...] Christophe Coenraets :: Synchronizing Data in Apollo using Flex Data Services [...]

Leave a Reply