You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@river.apache.org by Andrew Meng <ml...@hotmail.com> on 2009/10/23 18:10:08 UTC

Javaspace question.

Hello,

 

For those who are using Javaspace, if you do not know the number of tasks having been written to space, how do you manage to terminate the loop(i.e, collecting result) when the Javaspace are empty? Javaspace.take() does not return null if there is no more entry in space.

 

Thanks a lot in advance!

Andrew
 		 	   		  
_________________________________________________________________
Lots of fantastic Windows 7 offers, in one convenient place. Get the perfect deal for you now.
http://go.microsoft.com/?linkid=9691633

RE: Javaspace question.

Posted by Andrew Meng <ml...@hotmail.com>.
Dominic,

 

I did look at the takeifexist() method before but I am worrying about the synchronization between task-generating thread and result-collecting thread. For example, at some point, collecting thread could terminate prematurely if space is empty while waiting for worker to write a new result back to it.

 

Thanks a lot,

Andrew
 
> From: dominic-cleal@cdo2.com
> To: river-user@incubator.apache.org
> Subject: Re: Javaspace question.
> Date: Fri, 23 Oct 2009 17:28:24 +0100
> 
> Hi Andrew,
> 
> On Friday 23 October 2009 17:10:08 Andrew Meng wrote:
> > For those who are using Javaspace, if you do not know the number of tasks
> > having been written to space, how do you manage to terminate the loop(i.e,
> > collecting result) when the Javaspace are empty? Javaspace.take() does not
> > return null if there is no more entry in space.
> 
> Take a look at the takeIfExists method, which will return a null if no 
> matching entry exists. You can simply loop while a holding variable isn't 
> null, calling takeIfExists on each loop.
> 
> Cheers,
> 
> -- 
> Dominic Cleal
> CDO2
> 88 Union Street
> London
> SE1 0NW
> Tel: +44 (0)845 456 4460
> Fax: +44 (0)845 456 4461
> www.cdo2.com www.cdovar.net
 		 	   		  
_________________________________________________________________
Lots of fantastic Windows 7 offers, in one convenient place. Get the perfect deal for you now.
http://go.microsoft.com/?linkid=9691633

Re: Javaspace question.

Posted by Gregg Wonderly <ge...@cox.net>.
Guy Korland wrote:
> The problem with takeIfExists is that it blocks if there're Entries
> taken and locked under transaction.
> 
> This one of the reason we added in GigaSpaces the count() method.
> You can read more about it in:
> http://www.gigaspaces.com/docs/JavaDoc7.0/org/openspaces/core/GigaSpace.
> html#count(java.lang.Object)

In an active distributed system, at any point in time, one clients view of the 
"system" is almost never consistent with the actual system.  Counting is almost 
never a good way to do things because it doesn't dynamically scale with system 
changes, it almost always represents a size that is not going to be the same 
next time you check.  You can call count() and see how many items are there but 
if two workers do that with one item left, and both call take(), one will block.

If you want to start a worker on a JavaSpace, and then stop that worker later, 
than you need to write entries into the space with a marker that lets the worker 
see the end of the work list.

In general, a simple way to do this is to just use a marker interface for the 
type of thing you take, and have implementations of that interface that are both 
work items, and the "end of list" item.  That way, the workers can see when they 
are done.

public interface MyWorkItem {}

public class WorkItem implements MyWorkItem, SomeOtherInterfaceTooPerhaps {
	...
}

public class MarkerItem implements MyWorkItem, EndOfListMarker {
	...
}

Then you can ask if the entry is an instance of EndOfListMarker, and take 
appropriate actions.

The more predominate issue, is that if there are multiple workers, than you need 
to use transactional take, and abort the transaction when you get the "end 
marker" so that other workers will also see it.  You can use short leases on the 
object to automatically expire it.

There are several ways to do this, and I am sure others will throw in their 
experiences.

Gregg Wonderly

RE: Javaspace question.

Posted by Guy Korland <Gu...@gigaspaces.com>.
The problem with takeIfExists is that it blocks if there're Entries
taken and locked under transaction.

This one of the reason we added in GigaSpaces the count() method.
You can read more about it in:
http://www.gigaspaces.com/docs/JavaDoc7.0/org/openspaces/core/GigaSpace.
html#count(java.lang.Object)

Guy

-----Original Message-----
From: Dominic Cleal [mailto:dominic-cleal@cdo2.com] 
Sent: Friday, October 23, 2009 6:28 PM
To: river-user@incubator.apache.org
Subject: Re: Javaspace question.

Hi Andrew,

On Friday 23 October 2009 17:10:08 Andrew Meng wrote:
> For those who are using Javaspace, if you do not know the number of
tasks
> having been written to space, how do you manage to terminate the
loop(i.e,
> collecting result) when the Javaspace are empty? Javaspace.take() does
not
> return null if there is no more entry in space.

Take a look at the takeIfExists method, which will return a null if no 
matching entry exists.  You can simply loop while a holding variable
isn't 
null, calling takeIfExists on each loop.

Cheers,

-- 
Dominic Cleal
CDO2
88 Union Street
London
SE1 0NW
Tel: +44 (0)845 456 4460
Fax: +44 (0)845 456 4461
www.cdo2.com  www.cdovar.net

Re: Javaspace question.

Posted by Dominic Cleal <do...@cdo2.com>.
Hi Andrew,

On Friday 23 October 2009 17:10:08 Andrew Meng wrote:
> For those who are using Javaspace, if you do not know the number of tasks
> having been written to space, how do you manage to terminate the loop(i.e,
> collecting result) when the Javaspace are empty? Javaspace.take() does not
> return null if there is no more entry in space.

Take a look at the takeIfExists method, which will return a null if no 
matching entry exists.  You can simply loop while a holding variable isn't 
null, calling takeIfExists on each loop.

Cheers,

-- 
Dominic Cleal
CDO2
88 Union Street
London
SE1 0NW
Tel: +44 (0)845 456 4460
Fax: +44 (0)845 456 4461
www.cdo2.com  www.cdovar.net