Advertise here

 
 
 
Total number of hits on the site :656   Sunday, 20 Apr 2014
 
 

Advertisement

Adds by Siddhesh
Sidd Advertising
Puts your thought directly in customers brain.
Siddhesh Travels
Append wheels to your legs..get ready !
Siddhesh Rock's

MoQuotes




visitors by country counter
Country visit tracker


Like via Facebook

2) Flex- RemoteObject PDF Print E-mail
Written by Administrator   
Monday, 11 April 2011 08:29

Using Flex we can call POJO's residing at server side. Mode of communication here will be objects rather then an xml.
This type of communication is bit difficult to configure but easy to use and maintain.

I will suggest you to go through flex tutorial first and undersatnd things as much as you can before we proceed
http://www.adobe.com/devnet/flex/testdrive/articles/1_build_a_flex_app.html.
Download sample app http://wwwimages.adobe.com/www.adobe.com/content/dam/Adobe/en/devnet/flex/testdrive/assets/testdrive_setup_JAVA.zip
This contains jar files and configuration files which we require.

Lets go.

Create a dynamic web project. Copy all jar files from the above sample app to web-inf/lib forlder fo your new project.
Copy folder flex along with its content files to web-inf directory.


Copy following content to web.xml of your app within <web-app> tag
      <!-- Http Flex Session attribute and binding listener support -->
    <listener>
        <listener-class>flex.messaging.HttpFlexSession</listener-class>
    </listener>

    <!-- MessageBroker Servlet -->
    <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>
        <servlet-name>RDSDispatchServlet</servlet-name>
        <servlet-class>flex.rds.server.servlet.FrontEndServlet</servlet-class>
        <init-param>
            <param-name>useAppserverSecurity</param-name>
            <param-value>false</param-value>
        </init-param>        
        <load-on-startup>10</load-on-startup>
    </servlet>

    <servlet-mapping id="RDS_DISPATCH_MAPPING">
        <servlet-name>RDSDispatchServlet</servlet-name>
        <url-pattern>/CFIDE/main/ide.cfm</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>MessageBrokerServlet</servlet-name>
        <url-pattern>/messagebroker/*</url-pattern>
    </servlet-mapping>
    
Open remoting-config.xml file. You will see 'destination"s declare in following format

    <destination id="userService">    
        <properties>        
            <source>net.sever.pojo.UserManager</source>        
            <scope>application</scope>    
        </properties>    
    </destination>
    
    This says, Hey I have a service called userService which is pointing to POJO UserManager available ion package
    "net.sever.pojo" and my scope is application.

    
And yes we now need to create a class called "UserManager" in package net.sever.pojo. with following content

public String validateUser(String userName, String password)
    {
        if("Siddhesh".equals(userName))
            return "valid";
        else
            return "invalid";
    }
    
See, We will be calling this method of UserManager remotely from Flex app passing it user name and password.
if user name is "siddhesh" we are returning "valid".


At cliect side, Create a new Flex project and in your MXML file declare a new service as follows

<s:RemoteObject id="userService"
                        destination="userService"
                        source="net.scdl.assessmentserver.objects.UserManager"
                        endpoint="http://1<ip>:<tomcat port>/<app-name>//messagebroker/amf">
                        <s:method name="doLogin" result="validateUser(event)"
                              fault="mx.controls.Alert.show(event.fault.faultString)"/>
</s:RemoteObject>

NOTE : "destination" should match "ID" of service declared in remoting-config.xml file at server side.

We need to call userService.send() in initApp() method similar to what we did in previous HTTPService tutorial
And Now you can call our method like follows


userService.doLogin(loginUserIdInputBox.text,loginPasswordInputBox.text);

After call to this service control will go to method validateUser
In validateUser we get data as follows


public function validateUser(event:ResultEvent):void
{
var isUserValid:String = event.result as String;
}

Here isUserValid will contain string "valid" if we pass username as "siddhesh".

How about getting objects from Server ?  SIMPLEEEEEEEEEEE...

in UserManager write method as follows

 public Object GetCustom()
    {
        return new CustomObject();
    }

Create class called CustomObject in same package as that of UserManager

public class CustomObject
{
    public String name;
    
    CustomObject()
    {
        this.name= "siddhesh";
    }
}

At Flex side we will call the method getObject as follows

<s:RemoteObject id="userService"
                        destination="userService"
                        source="net.scdl.assessmentserver.objects.UserManager"
                        endpoint="http://1<ip>:<tomcat port>/<app-name>//messagebroker/amf">
                        <s:method name="doLogin" result="validateUser(event)"
                              fault="mx.controls.Alert.show(event.fault.faultString)"/>
                        <s:method name="getObject" result="validateObject(event)"
                              fault="mx.controls.Alert.show(event.fault.faultString)"/>
</s:RemoteObject>

So in method validateObject we will get name of Object as follows

public function alidateObject(event:ResultEvent):void
{
    var obj:Object = event.result as Object;
    var name:String = obj.name;
}

And here we get name of our remote POJO as obj.name; Similarly you can retrieve a list,int etc.


So what is this "endpoint="http://1<ip>:<tomcat port>/<app-name>//messagebroker/amf">"

OK. so open file  services-config.xml. You will find following info under channel tag
    <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"/>
        </channel-definition>

and in file Remoting-config we mention our defualt channel as follows
    <default-channels>
        <channel ref="my-amf"/>
    </default-channels>  

So "my-amf" acts like an end point for all requests coing from Flex app. Thats it.
services-config.xml contains many configuration parameters for  endpoints and server's.
You can read more about gateway and endpoint on net.

Congratulations. You learn t how to integrate Flex and Java. N-joyyyyy.. MIND IT.

Next Section will look ate how to upload file using Flex and get the uploaded file at Java server side. STAY TUNED !!!

COMMENTS

avatar jeff koppo
0
 
 
AH. I learn t Flex remoting by breaking my head for 2 days. Why you dint published this article before..This is Awsome. I am sharing the link on my blog. Thanks .appreciate your efforts Sidesh
DATETIMEFORMAT
POST_REPLY
NAME *
EMAIL_VERIFICATIONS_REPLIES
CODE   
Thanks for your valuable Comment.
SUBMIT_COMMENT
Cancel
NAME *
EMAIL_VERIFICATIONS_REPLIES
CODE   
Thanks for your valuable Comment.
ChronoComments by Joomla Solutions
SUBMIT_COMMENT
Last Updated on Monday, 11 April 2011 08:45
 
 
Advertise here