Flex accesses a remote Java object through remoteobject

Source: Internet
Author: User
ArticleDirectory
    • Access remote Java objects
    • 19.2.1 server: implement the pojo Java class
    • 19.2.2 server: deploy the pojo Java class
    • 19.2.3 server side: Configure remote-config.xml
    • 19.2.4 client: instantiate the remoteobject Component Object
    • 19.2.5 client: Call remote Java object Method
    • 19.2.6 client: handling responses and exceptions
    • 19.2.7 use <mx: Operation> to process multiple methods
Access remote Java objects

To access remote Java objects, perform the following steps on the server:

1. Create a livecycle Data Service web application;

2. Implement the pojo Java class;

3. Deploy the pojo Java class in the livecycle Data Service web application;

4. Configure the remote-config.xml file, declare the desination node, point to the pojo Java class.

In client applications, You need:

1. instantiate the remoteobject object and set attributes such as destination;

2. Call methods of remote Java objects and pass parameters;

3. register the result and fault event listeners of the remoteobject object to implement the listener method to handle returned responses or call exceptions.

We have introduced how to create a livecycle Data Service web application in section 17.5.1 "Create a server-side livecycle data service application" (see page 17th. Therefore, we will gradually access remote Java objects from the implementation of the pojo Java class.

19.2.1 server: implement the pojo Java class using the pojo class as a remote Java object

Of course, we will not explain how to write pojo Java classes in a flex book. We need to explain here the Java class that can be used as a remote Java object.

Only the so-called pojo class can be used as a remote Java object. Pojo (plain old Java object) is short for simple Java objects. It usually contains a series of get and set methods. Generally, pojo Java classes are pure Java objects and do not implement any Java object models, protocols, or frameworks. For example, they cannot implement EJB standards. Strictly speaking, the pojo class cannot inherit other parent classes and cannot implement interfaces. Livecycle data service requires that, as a remote Java object of the livecycle data service remote service, its constructor cannot accept any parameters, that is, it must be a zero parameter constructor. In this way, livecycle data service can construct pojo class instances.

However, the pojo class can still serve as a window for backend Service Logic services, such as using the pojo class to access JNDI and obtain the EJB service, so as to further integrate the backend service logic.

The public methods of the pojo Java class provide remote services. Note that livecycle data service retains some method names, so these method names cannot be used in the pojo Java class. We call these methods "Reserved methods ". The retention method is defined by the remoteobject class or its parent class.

Retention methods include:

· Addheader ();

· Addproperty ();

· Clearusernamepassword ();

· Deleteheader ();

· Hasownproperty ();

· Ispropertyenumerable ();

· Isprototypeof ();

· Registerclass ();

· Setusernamepassword ();

· Tolocalestring ();

· Tostring ();

· Unwatch ();

· Valueof ();

· Watch ().

In addition, the pojo Java class as a remote object cannot use "_" as the start Letter of the remote service method name.

19.2.2 server: deploy the pojo Java class

When we use the httpservice component and WebService component to access the remote HTTP service and Web Service through the httpproxyservice service, the remote service is not necessarily deployed on the livecycle Data Service server.

Remote Java objects are called in different situations. The Java class that provides remote services must be deployed in the same livecycle Data Service web application as the flex application.

The only purpose of deploying the pojo Java class is to find the corresponding Java class when the remoteobject component of the client sends an access request to the livecycle data service.

Therefore, we need to deploy the pojo Java class to the livecycle data service class path. We can directly copy the pojo Java class file (. to the WEB-INF \ Classes directory of the Web application according to the class package structure, or package the pojo Java class as a jar package (. JAR file), deployed in the WEB-INF \ lib directory.

19.2.3 server side: Configure remote-config.xml original remote-config.xml files

The LCDs-root/webapps/LCDs directory contains an empty remote-config.xml file. Let's take a look at the original remote-config.xml file first.

See Figure 19-2. The figure shows the original remote-config. xml file and comments the main node.

Figure 19-2 remote-config.xml with no target configured

Configure remote service targets

In the <service> </service> tab, add the <destination> </destination> label to declare the new remote object target.

The followingCodeDisplays the remote-config.xml file that contains a destination definition:

<? 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 = "shoppingcart">

<Properties>

<Source> com. Mark. pojo. shoppingcart </source>

<Scope> session </scope>

</Properties>

</Destination>

</Service>

The source attribute of the destination node describes the target of the remote object service, that is, the remote Java object. When setting the source attribute, you must use the full path Java class name to point to the Java class deployed in the livecycle data service class path.

The optional values of the scdrop attribute of the desitnation node are application, session, and request, and the remote object instance scope is specified.

If the scope is request, the server creates a remote Java object instance for each request. The request is the default scope.

If the scope is Session, the server creates a shared remote Java object instance for each session. Generally, each browser instance connected to the server forms a session. For example, if we start Firefox to access the flex application connected to the livecycle data service, the launched Firefox is a browser instance. In this Firefox browser, if you open other tabs and access the same flex application, these applications are in the same session and share the same remote object instance.

If the scope is application, the server creates a remote Java object instance for each flex application.

Restrict Remote Java Object Access

In some cases, remote Java objects define many public methods, but we may not want these methods to be open to flex applications. In this case, you can use the include-methods attribute to define the list of methods that can be accessed. If the flex application attempts to access methods outside the list, an exception will occur and a fault event will be called on the client.

The include-methods attribute is located in <Properties> </Properties> of the destination node. The following settings only allow the flex client to access the purchaseitem and deleteitem methods of the shoppingcart class.

<Destination ID = "shoppingcart">

<Properties>

<Source> com. Mark. pojo. shoppingcart </source>

<Scope> session </scope>

<Include-methods>

<Method name = "purchaseitem"/>

<Method name = "deleteitem"/>

</Include-methods>

</Properties>

</Destination>

If you only want to restrict remote access to several public methods, it is more convenient to use the <exclude-methods> </exclude-methods> attribute. The following configuration sets the flex client to access other public methods except the purchaseitem method in the shoppingcart class.

<Destination ID = "shoppingcart">

<Properties>

<Source> com. Mark. pojo. shoppingcart </source>

<Scope> session </scope>

<Exclude-methods>

<Method name = "purchaseitem"/>

</Exclude-methods>

</Properties>

</Destination>

19.2.4 client: instantiate the remoteobject Component Object

The mxml tag syntax for creating a remoteobject component object is as follows:

<Mx: remoteobject id = "remoteobj" Destination = "shoppingcart"/>

The instance name of the remoteobject component is specified in the ID attribute. It will be used when you call this service object and process remote service response. The destination attribute specifies the target of the remote service to be connected. The value must match the target ID attribute configured in the remote-config. xml file on the server.

We can also use the ActionScript code to create a remoteobject object:

VaR remoteobj: remoteobject = new remoteobject ();

Remoteobj. Destination = "shoppingcart ";

19.2.5 client: Call remote Java object Method

The remoteobject component is used to access public methods of remote Java objects. The simplest way to call a remote Java object method is to call the method name directly:

Remoteobj. methodname (parm1, parm2 ,...);

In the mxml tag, we can directly use this method to call remote services in user events. For example, the following code calls remoteobj. Purchase ('TV') When you click "shopping '). (Assuming that the remote Java object we configured has the public method purchase (string product) open )).

<Mx: remoteobject id = "remoteobj" Destination = "shoppingcart"/>

<Mx: button label = "shopping" Click = "remoteobj. Purchase ('TV')"/>

In this chapter, we will introduce how to implement and deploy pojo Java classes on the server side. Remote Java objects should avoid retaining method names. However, if you have to do this for unavoidable reasons, for example, your remote Java object developer is a paranoid "Disconnect" method name, the getoperation method of the remoteobject class can ease your embarrassment.

Public Function getoperation (Name: string): Operation

For example, if the public method tostring () is enabled for remote Java objects, we can use the following code to create an operation object to call this method:

Public var remoteobj: remoteobject = new remoteobject ();

Remoteobj. Destination = "shoppingcart ";

// Create an operation object that represents the tostring Method

Public var metadata: Operation = remoteobj. getoperation ("tostring ");

// Call this method

Response. Send ();

19.2.6 client: handling responses and exceptions

Remoteobject calls a remote Java object asynchronously. After the call, the flex application does not stop performing other operations to wait for a response, but allows users to continue interaction and perform other operations. When a response is returned or an exception is thrown, the system schedules the result event or fault event to notify the application to execute the event listener method. That is to say, we listen on the result and fault events of the remoteobject component to process responses and exceptions. This method is exactly the same as processing remote HTTP service and Web Service responses.

When remoteobject successfully calls a remote method, the system schedules the result event. Developers can use the MX. rpc. Events. resultevent object generated by the result event to access the service response data.

As shown in the following figure, we add a result event and a fault event listener for the remoteobject object instance, and declare the listener method.

......

<Mx: SCRIPT>

<! [CDATA [

Import MX. rpc. Events. faultevent;

Import MX. rpc. Events. resultevent;

// Remoteobject result event listener Method

Private function rorespondhandler (Event: resultevent): void {

// Process remote service response

}

// Remoteobject object fault event listener Method

Private function roerrorhandler (Event: faultevent): void {

// Handle remote service call exceptions

}

]>

</MX: SCRIPT>

<! -- Instantiate a remote object component -->

<Mx: remoteobject id = "remoteobj" Destination = "shoppingcart"

Result = "rorespondhandler (event)" fault = "roerrorhandler (event)"/>

<! -- Call the remote object component -->

<Mx: button label = "shopping" Click = "remoteobj. Purchase ()"/>

......

We can use the resultevent. Result attribute or the operation. lastresult attribute of the remote service component to obtain the returned data.

Similar to processing web service return objects, for complex types of returned data, such as arrays or custom objects, flex encapsulates array-type data as MX by default. collection. arraycollection, which encapsulates the custom object type as MX. utils. objectproxy.

If the remote object fails to be called, the system will trigger the fault event. You can define a listener method for the event and use the MX. rpc. Events. faultevent object generated by the fault event to handle the exception. For details, see section 18.1.6 "Exception Handling" in Chapter 18th (see page 425th ).

19.2.7 use <mx: Operation> to process multiple methods

Similar to Web Services, multiple operations can be provided, and multiple public methods can be opened for remote Java objects to be called by the remoteobject component. We can use the operation class or the <mx: method> label to call multiple remote methods for a remoteobject component.

This method can also provide more options for handling responses of different methods. We can define the proprietary result and fault event listeners for each method, instead of defining a uniform listener on the remoteobject component object.

Suppose we have defined the Java class shoppingcart and opened methods such as clearcart (), purchaseitem (string itemname, int quantity), and deleteitem (string itemname.

Use MX. rpc. remoting. Operation class

The MX. rpc. remoting. Operation class is used to specify remote object operations for the remoteobject class. Operation is a single method on the service. You can call a function of the same name on the service, that is, remoteobject. operationname () method, or access the operation as an attribute on the service and call the send () method to call operation, such as remoteobject. operation. send ().

Use the <mx: method> label

You can also use the <mx: method> label to define multiple methods for the remoteobject component.

The <mx: method> label must be embedded in the <mx: remoteobject> </MX: remoteobject> label. The name attribute specifies the name of the matched remote Java object method. Developers can register the result and fault event listener methods for each method label to handle the response or call exceptions of different methods of the remote Java object respectively.

In the <mx: method> </MX: method> label pair, you can embed the <mx: arguments> label to specify the parameter passed to the remote method. Flex does not require defining the <mx: arguments> tag with the same name as the parameter name of the remote Java object method. However, when you call a remote method with multiple parameters, you must ensure that the parameter sequence specified by the <mx: arguments> tag matches the parameter sequence in the remote method.

<! -- Instantiate a remote object component -->

<Mx: remoteobject id = "remoteobj" Destination = "shoppingcart"

Fault = "roerrorhandler (event)">

<Mx: method name = "clearcart" result = "clearroresulthandler (event);"/>

<Mx: method name = "deleteitem"

Result = "deleteroresulthandler (event);">

<Mx: arguments>

<Item >{ comboitem. selecteditem. Data} </item>

</MX: arguments>

</MX: method>

<! -- The order of parameters must match the purchaseitem (string itemname, int quantity) Method -->

<Mx: method name = "purchaseitem"

Result = "purchaseroresulthandler (event);">

<Mx: arguments>

<Item >{ comboitem. selecteditem. Data} </item>

<Qty> {txtquantity. Text} </qty>

</MX: arguments>

</MX: method>

</MX: remoteobject>

Since we have already defined the number of parameters in the <mx: method> label, we only need to use the send () method without parameters to call. For example:

<! -- Call the remote Java object purchaseitem Method -->

<Mx: button label = "shopping" Click = "remoteobj. purchaseitem. Send ()"/>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.