Daniel Harfleet: Calling Java remote objects and handling results

« Integrating Java and Flex with a simple RPC example | Main | Passing complex parameters and results »

August 04, 2006

Calling Java remote objects and handling results

In my previous coffee break guide we got a flex app talking to some java on the server. I'm guessing you are all happy with how the java works, but a quick explanation of how we called the java and how we handled the results would be useful.

If you take a look at EchoView.mxml, there are a number of 'features' I would like to describe to you. The mxml file itself will be compiled by the flex compiler into a .swf file, which is the binary which will run in the browser ( a flash movie ). The mxml in this file is derived from an mx:Application tag, for the purposes of this discussion, think of this as the 'entry point' of the client application. You will see there is some view code, which is made up of various controls such as buttons and text inputs and some layout code, such as HBox. I will not discuss this layout code in any more detail, I'm sure you guys can figure it out, but if not, take a look at the docs.

Next is the code which allows us to hook onto the server and make our RPC call, this is declared inside the RemoteObject tag:


<mx:RemoteObject
id="echoRO"
destination="echoService"
fault="handleFault(event)"
result="handleResult(event)" />


The id is just our instance name for an instance of RemoteObject, the destination points to the remote destination which is configured in our remoting-config file:

<destination id="echoService">
<properties>
<source>fdsweb.EchoService</source>
<scope>application</scope>
</properties>
</destination>

The fault and result properties are in fact event handlers and they point to functions which will handle the events, as you can see, they also pass in the event as a parameter. The fault event occurs when something goes wrong, including exceptions in the server java code, i.e. our remote object. The result event (hopefully) will be called most of the time, this points to the function which will handle successful results from the remote objects. The flex gateway (the code in the WAR file) translates standard java parameters and return types to standard flex parameters and return types, as described here:

Java to ActionScript
ActionScript to Java


On a real project, we would use the Cairngorm microarchitecture to abstract out our remote objects into a ServiceLocator, but as we are just wanting to play, lets just go with declaring the remote object in our mxml entry point file.

You will notice that the event handling code in the MXML file is written in action script and as such is declared in an mx:Script tag. The functions handleResult and handleFault are the same functions which we point our remote object event handlers to, handleClear describes what happens when the user clicks on the clear button (note the button has a click event handler which is pointing to this function) and finally handleSend describes what happens when the user clicks on the send button, apart from doing some trivial view stuff, this function calls the remote object:


echoRO.echo(message);

any method which is exposed on the remote java object can be called by the actionscript code (subject to security), as long as it passes a compatible parameter, see the docs for type mapping. The most important thing you will need to get your head around is that you do not assign the return of the remote method from the result of the call, in other words you do not assign it in the handleSend function. Remote object calls are asynchronous, so the client continues working in parallel to the server handling the method call, the result appears as an event on the remote object, which is handled as I mentioned earlier by the 'result' or 'fault' events on the remote object, which in our case point to handleResult and handleFault functions.

It is important to remember that the request and the response are asynchronous, so if you made request-A and then request-B you are likely to receive response-A and response-B, however, there is no guarantee and you could receive response-B and then response-A; see Asynchronous Call Token for a typical way of matching requests to responses if you need to.

As you are probably starting to work out, using the current techniques above, you would be limited to only using java parameters and return types which are mapped; fortunately however, Flex allows us to create complex objects which we can pass back and forth, often these objects are known as Data Transfer Objects or Value Objects, however, ultimately they are just objects.

In the next coffee-break guide, I will show how you can pass your own custom object to from flex to java and also a custom object from java to flex.

Posted by dharfleet at August 4, 2006 05:00 PM

Comments

Hi All,

Very good tutorial. Enjoyed a lot implementing.

I am getting error "Client.Error.MessageSend". Could some one help to to debug througth this error.

Thanks,
Parag

Posted by: Parag at February 15, 2007 09:58 AM

Hi. I am getting a strange problem. I have installed Flex 2 SDK for eclipse and updated to 2.01.
ANY FDS2 project I create shows from the start the following errors:
1023 Incompatible Override
1120 Access of undefined property IFlexModule
1144 Interface method create in namespace mx.core:IFlexModuleFactory is implemented in class xxx_SystemManager
1172 Definition IFlexModule could not be found

Could you please help me ? What is this ? How do I fix these Errors ?
I googled and it was said that it has something to do with a wrong update package from Adobe, but thats my third try to download, install and reinstall.
Does that have somthing to do with the FDS2 Express Edition ?
I am using JDK1.5, JBoss4.0.4 and Eclipse 3.2.
I am really desperate for help since I need this working for a running project...
PLEASE, PLEASE HELP :)
Sincerely yours
Amir Rocker

Posted by: amir at March 19, 2007 01:30 PM

I have a webservice that can be consumed via either the WSDL or via a Java wrapper class in Flex. As a developer that is new to Flex I have been trying to work out the best way of consuming the webservice.

So far I have determined that you can consume a webservice:

a) directly in MXML via the appropriate tags, however as the WSDL needs to be almost completely mapped here it seems impractical when you have large WSDL definitions to map to.
b) via actionscript, which to be honest I have not been able to get working as of yet.
c) using remote objects with the Java wrapper I mentioned previously.

Aside from which I have just stated what would be the pros/cons of each method?

Many thanks in advance.

Posted by: BlackDhalia at May 10, 2007 05:00 PM

ok

Posted by: Almousjbuiui at May 11, 2007 06:00 PM

In FDSWeb folder ---- > you have WEB-INF folder ------> in that classes ---- > copy the folder(fdsweb) and paste into fdsweb.war---->WEB-INF--->classes

Try this it will run properly.i executed the code..it is working fine......

Thanks,
Megharaja S.H.

Posted by: Megharaja S.H. at July 26, 2007 08:48 AM

I am testing a very simple call, but i am getting the next error : the 'Data' service can only process messages of type 'DataMessage'.


Posted by: Bacca at October 26, 2007 11:03 PM

Hai very nice tutorial but i wish that u can give an example of jsp with flex sending and receiving data
thanks in advance!!!

Posted by: martin at December 19, 2007 02:57 AM

t

Posted by: j at December 24, 2007 06:50 AM

ZETACLEAR (ZETA CLEAR) http://naturalhealthsupplement.info/zetaclear/
Anti Nail Fungus Solution by Zetaclear
Zetaclear is an all natural product specifically developed to treat the underlying cause of unsightly nails - without the risks and high costs of prescription medications.

Eazol Natural Pain Relief Eazol
http://naturalhealthsupplement.info/eazol/
Natural Pain Relief Medication - Eazol

HGH Energizer HGH Energizer Pill HGH Energizer
http://naturalhealthsupplement.info/hghenergizer/
HGH Release Supplement

Joint Advance - Joint Advance
http://naturalhealthsupplement.info/jointadvance/
Joint Advance gives you a shield of protection

Melatrol Natural Sleeping Aid Melatrol
http://naturalhealthsupplement.info/melatrol/
How to Fall Asleep Easily & Naturally

Nicocure Stop Smoking Aid Nicocure
http://naturalhealthsupplement.info/nicocure/
In just 30 days or less, you can quit smoking!

Optimum Diabetics Diabetics Health Supplement Optimum Diabetics
http://naturalhealthsupplement.info/optimumdiabetics/
Optimum Diabetics is scientifically formulated to provide nutritional support for people with diabetes.

Relagen Stress, Anxiety & Weight Loss Formula Relagen
http://naturalhealthsupplement.info/relagen/
Natural Stress, Anxiety and Depression formula without the side effects of prescription drugs!

Thyromine Thyroid Health Supplement Thyromine
http://naturalhealthsupplement.info/thyromine/
Did you know that nearly 11 million of us have a hypo-active thyroid? And that Hypothyroidism is linked to obesity and high cholesterol?

Venapro Hemorrhoids Treatment Venapro
http://naturalhealthsupplement.info/venapro/
Venapro is a long-term, non-surgical approach to hemorrhoids treatment.

Avatrim Green Tea Weight Loss Pill Avatrim
http://naturalhealthsupplement.info/avatrim/
HOW TO LOSE WEIGHT AND INCREASE YOUR ENERGY NATURALLY

Dietrine Carb Blocker Pill Dietrine
http://naturalhealthsupplement.info/dietrine/
Lose Weight and block fat

Hoodia Balance Natural Appetite Suppressant Hoodia Balance
http://naturalhealthsupplement.info/hoodiabalance/
Hoodia Balance is a Fast and Effective Weight Loss Supplement

Hoodia Chaser Natural Appetite Suppressant Hoodia Chaser
http://naturalhealthsupplement.info/hoodiachaser/
Hoodia Chaser Reduces Your Appetite in Minutes

Hoodia Diet Review What You Need to Know About Hoodia Gordonii Hoodia Diet Review
http://naturalhealthsupplement.info/hoodiadietreview/
All about Hoodia Chaser, Hoodia Balance and Hoodia Gordonii Plus

Hoodia Gordonii Plus Natural Appetite Suppressant Hoodia Gordonii Plus
http://naturalhealthsupplement.info/hoodiagordoniiplus/
Hoodia Gordonii Plus Program provides a fast and effective weight loss system

Manage Weight Loss Review Eat to live. Don't Live to Eat! Colon Health Manage Weight Loss Review
http://naturalhealthsupplement.info/manageweightlossreview/
Manage Weight Loss Review is a free resource to help you make healthy food and lifestyle choices for you and your family.

Bowtrol Sensitive Digestion Formula Bowtrol
http://naturalhealthsupplement.info/bowtrol/
Bowtrol for IBS is an all natural remedy that is clinically proven effective for Irritable Bowel Syndrome.

Bowtrol Colon Cleanse Natural Colon Cleansing Formula Women Health and Beauty Bowtrol Colon Cleanse
http://naturalhealthsupplement.info/bowtrolcoloncleanse/
Bowtrol is an all natural herbal colon cleansing treatment that has changed hundreds of thousands of people's lives through our gentle and effective whole body safe and effective internal cleansing.

Breast Actives Breast Enhancement Program Breast Actives
http://naturalhealthsupplement.info/breastactives/
Enhance your Breasts Naturally

Cushy Lips Lip Plumper and Gloss Cushy Lips
http://naturalhealthsupplement.info/cushylips/
Perfect, Sexy Lips are Just a Step Away!

Menozac Natural Menopause Relief Menozac
http://naturalhealthsupplement.info/menozac/
Menozac is a botanical alternative medicine formulation which contains a blend of all natural herbal extracts developed to ease the transition and provide effective Menopause Symptoms Relief from the onset and duration of typical menopausal symptoms.

Provillus Hair Loss Treatment for Men and Women Provillus
http://naturalhealthsupplement.info/provillus/
Provillus for Men has a breakthrough formula that blocks dihydrotestosterone (DHT), hormone responsible for hair fall in men.
Provillus for Women rejuvenates the hair follicles and stimulates growth of new hair. The nutrients in Provillus provide optimal nutrition for healthy and growing hair.

Deer Antler Plus Sexual Enhancer Deer Antler Plus
http://naturalhealthsupplement.info/deerantlerplus/
Velvet Deer Antler has been used in oriental medicine for centuries to increase sexual desire and improve sexual performance.

Enlast Premature Ejaculation Formula Enlast
http://naturalhealthsupplement.info/enlast/
The male Enhancement formula

Men Health Zone
http://naturalhealthsupplement.info/menshealthzone/
Men Health Zone has been developed to help you take care of your health more easily.

Natural Gain Plus Male Enhancement Program Natural Gain Plus
http://naturalhealthsupplement.info/naturalgainplus/
Natural Gain Plus for Natural Male Enhancement

Prostacet Prostate Health Supplement Prostacet
http://naturalhealthsupplement.info/prostacet/
An all-new formula using only top of the line prostate ingredients

Virility EX Pills Male Enhancement Pills Skin Care Virility EX Pills
http://naturalhealthsupplement.info/virilityexpills/
Virility EX Natural Male Enhancement

Revitol Revitol Health & Beauty Store Revitol
http://naturalhealthsupplement.info/revitol/
Revitol Cellulite Solution Will Help You:
-Eliminate Cellulite Dimples and Inches
-Boosts Muscle tone and Sculpts your Body
-Firms and Tightens your thighs, legs and butt

Acnezine Acne Skin Care Treatment Acnezine
http://naturalhealthsupplement.info/acnezine/
NATURAL ACNE TREATMENT FOR NATURALLY CLEAR SKIN

Anti Aging Treatment Revitol Skin Care Anti Aging Treatment
http://naturalhealthsupplement.info/antiagingtreatment/
Say goodbye to Botox with Revitol Solution

Cellulite Solution Revitol Skin Care Cellulite Solution
http://naturalhealthsupplement.info/cellulitesolution/
Revitol Cellulite Solution

Hair Removal Cream Revitol Skin Care Hair Removal Cream
http://naturalhealthsupplement.info/hairremovalcream/
Hair Removal Cream advanced hair inhibitor system is a 100% pain-free effective cream.

Hair Removal Plus Hair Removal Cream Hair Removal Plus
http://naturalhealthsupplement.info/hairremovalplus/
Hair Removal Plus advanced hair inhibitor system is a 100% pain-free effective cream.

Stretch Mark Prevention Revitol Skin Care Stretch Mark Prevention
http://naturalhealthsupplement.info/stretchmarkprevention/
As part of our Revitol product line we have now developed a revolutionary new cream to be used as a preventative treatment for stretch marks before, during and after pregnancy. This Stretch Mark Prevention cream will also help to reduce the appearance of existing stretch marks!

Posted by: xXx at February 16, 2008 12:32 PM

Thank you

Posted by: rüya tabirleri at February 22, 2008 10:45 PM

Thanks!

Posted by: evden eve nakliyat at February 22, 2008 10:46 PM

Thank you ary !

Posted by: abiye at February 22, 2008 10:46 PM

Perhaps before going on, I should describe acomplia to you. Acomplia is oleaginous, smarmy, and insidious. Furthermore, it yearns to perpetuate misguided and questionable notions of other unruly vandals' intentions.

Posted by: acomplia at March 17, 2008 01:21 PM

thanks.As part of our Revitol product line we have now developed a revolutionary new cream to be used as a preventative treatment for stretch marks before, during and after pregnancy. This Stretch Mark Prevention cream will also help to reduce the appearance of existing stretch marks!

Posted by: evden eve nakliyat at June 11, 2008 04:47 PM

Post a comment




Remember Me?