tags:

views:

182

answers:

1

Hi everyone,

I have the following application in flex. I want to call two different remote objects in parallel.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical">
 <mx:RemoteObject id="service1" destination="test1" />
    <mx:RemoteObject id="service2" destination="test2" />
    <mx:Button label="service1" click="{service1.method(1)}" />
    <mx:Button label="service2" click="{service2.method(2)}" />
    <mx:Button label="service1 AND service2" click="{service1.method(1);service2.method(2)}" />
</mx:Application>

Each remote object is wired to a different Java implementation called TestCase1 and TestCase2. So I would think I could call the two objects in parallel and execute them parallel to each other.

public class TestCase1 {

 public void method(int n) {
        System.out.println("method(" + n + ") starts");
        try {
            Thread.sleep(8000);
        } catch(InterruptedException e) {}
        System.out.println("method(" + n + ") ends");
    }

}

public class TestCase2 {

 public void method(int n) {
        System.out.println("method(" + n + ") starts");
        try {
            Thread.sleep(8000);
        } catch(InterruptedException e) {}
        System.out.println("method(" + n + ") ends");
    }

}

Now what the methods do is to print sth. when they are called, then wait 8 secs and print sth. after that.

When clicking each button seperatly, it works, both methods are started in parallel. However, calling both method the same time, leaves one service to wait with its call while until the other is executing.

How can I avoid that?

Thx Philipp

enter code here
A: 

RemoteObject requests get queued until the next execution frame and then sent via the same HTTP request, ie. multiple operations over the same request. So when you call: service1.method(1);service2.method(2)
Both operations are going over the same HTTP request. The first one causes the thread handling that request to sleep thus causing the second operation to not be called until the thread resumes.

The only way I know of to avoid this is to make sure that each operation goes on it's own HTTP request and thus gets it's own thread. But remember that you only get 2 concurrent HTTP requests in most browsers. So if you are making more than two requests at a time then you will still be blocked and not parallel beyond 2 concurrent operations.

An easy way to get requests to happen on the next frame is to use callLater like:

service1.method(1);  
callLater(function():void { service2.method(2) });

I believe this will cause the first operation to happen on the next frame and the second operation will happen on the following frame. You might need to wrap another callLater around the callLater to make sure it gets on the frame after the first operation.

James Ward
James,thx for the explanation. I have implemented a little queue that takes the service calls and pipes them, then calling them each after a certain time has gone (100 milli seconds). That works as well. Now as for parallel calls.does that mean I wouldnt be able to send out severall calls to an mx:HttpService as well, without blocking?thinking ofservice1.send(params);service2.send(params);service3.send(params);ThxPhilipp
Philipp Tiedt
I'm pretty sure that HTTPService works the same way as RemoteObject in this respect.
James Ward