Question

I'm working with Apache River javaspaces project and I'm trying to figure out a way to write a template that reads multiple entries in the space and returns the ones I don't already have stored somewhere for example:

User writes three Entries to the space "entry1", "entry2", "entry3". Later the same user tries to read the Entries from the space, each time the space returns "entry1" because it's not being removed from the space it will always match the given template.

Writing to space:

SpaceEntry string = new SpaceEntry("User","entry1"); 
                    space.write(string, null, Lease.FOREVER);

Reading from space:

SpaceEntry template = new SpaceEntry(); 
                    SpaceEntry read = (SpaceEntry)space.readIfExists(template,null,Long.MAX_VALUE);

$read is stored in a list and the reading process repeats.

Template:

public SpaceEntry (String o, String m){
    this.Owner = o;
    this.Message = m;
}

Finally any suggestions on dynamically modifying the template with the list of values already acquired so the Entries remain in space but different ones are returned?

I thought about taking the Entries from the Space storing them in a list and then returning them to the Space when no new entries are left but this would introduce all sorts of confusion, lost data and loose multi-user compatibility.

Edit1:

Okay so after fiddling about with my code I figured out a way of doing this.

The Template has to be changed to use another parameter ID.

public SpaceEntry (int id, String o, String m){
this.ID = id;
this.Owner = o;
this.Message = m;

}

Now each time the user wants to add a message the code has to read existing Entries in the Space by incrementing the id value in the template by one until none are left and add the new message with the missing id number. The same code could be run to read all the entries in the space and return them by id number.

However this poses a threat if one of the Entries is removed how would the Code deal with a missing ID for example 1,2,,4,,_,7 any suggestions?

Edit2:

Seems like Transaction Manager might be the way to go..

Was it helpful?

Solution

Upon doing some additional research I've come across the answer to this question. In order to read multiple Entries you have to implement a transaction manager which basically reads all Entries that match the Template into a transaction, a list of sorts, and the manager only adds the entries that are not already in transaction. Once the transaction is committed the space Entries remain unchanged but you have a complete list of them.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top