This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Distributed Java and the fly likes Reliablity in JavaSpaces Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Distributed Java
Bookmark "Reliablity in JavaSpaces" Watch "Reliablity in JavaSpaces" New topic
Author

Reliablity in JavaSpaces

Pal Amutha
Greenhorn

Joined: Jan 28, 2008
Posts: 13
I tried small JavaSpace(read/write) programs using jini2.1 . occasionally those programs running properly , but sometimes same program gives error.

Can anybody tell me , Is JavaSpace suitable for reliable application ?.


The write method contain the (EntryObject , transaction, lease) parameter.
Transaction set to null.
lease set to Lease.FOREVER.

The read method contain the (EntryObject -- template, transaction,lease) parameter
Transaction set to null
lease set to Long.MAX_VALUE

once i run the write and read program , its running properly.
But same program gives error in second time when running read- program.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
You have neglected to say what error was thrown. Perhaps your programing has conceptual mistakes.

I found JavaSpaces to be reliable once set up correctly.

Bill
Pal Amutha
Greenhorn

Joined: Jan 28, 2008
Posts: 13
Thankyou for your kind reply sir.


First time when i run the program to read ArrayMetaData entry object it was working.
When i try second time also its working . third time it gives error , fourth time its working .


ArrayMetaData array=(ArrayMetaData)space.read(tmpl,null,60*1000);

I mentioned write method lease time is 10mini and i performed all read operation within 10 mini.



the error is like that below :


java.lang.NullPointerException
at aclient.readtuple(aclient.java:50)
at sread.main(sread.java:32)


readtuple is method of class aclient which perform read operation .

Can you tell me the solution for occasional failure .
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
The obvious next debugging step would require that you look at the code for readtuple at line 50.

So far there is no evidence that JavaSpaces has anything to do with the NullPointerException. Much more likely there is a logic flaw in your client program.

Bill
Pal Amutha
Greenhorn

Joined: Jan 28, 2008
Posts: 13
Line 50 is read Operation. Even though we are running same program with out changing the code, it is causing read failure... occasionally
Can you tell any possible reason for this....?

1. Network problem....
2. Is it because transaction in READ operation =null?
3. I have n't used RMI security features. Will that affect the read?

Please help...? ('');

William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
I continue to bet that your program has a conceptual error having nothing to do with JavaSpaces.

Since you have shown none of the code leading up to the line throwing the NPE, the limit of help I can suggest is:

You should code defensively, checking all the references involved leading up to line 50 for appropriate values. The space variable is what I would check first.

Bill
Pal Amutha
Greenhorn

Joined: Jan 28, 2008
Posts: 13
Thank you for your reply.

I mentioned the entire program below for your kind reference

aclient.java



import net.jini.space.JavaSpace;
import net.jini.core.lease.Lease;

public class aclient {

public boolean writetuple(String tupleid, int size,String tuplefield[])
{
try
{
Lookup finder=new Lookup(JavaSpace.class);
JavaSpace space=(JavaSpace)finder.getService();
System.out.println("before write");

System.out.println("after transaction");
ArrayMetaData Adata=new ArrayMetaData(tupleid,size);
System.out.println(" Array instance");
space.write(Adata,null,10*60000);
System.out.println("write 1");

for(int j=0;j<size; j++)
{
ArrayElement e=new ArrayElement(tupleid,new Integer(j),tuplefield[j]);
space.write(e, null, 10*60*1000);
System.out.println("write "+j);
}


}catch(Exception ex){ex.printStackTrace(); return false;}
return true;
}


public boolean readtuple(String tupleid)
{
try
{
Lookup finder= new Lookup(JavaSpace.class);
JavaSpace space=(JavaSpace)finder.getService();
ArrayMetaData tmpl=new ArrayMetaData(tupleid);
System.out.println("after lookup");
ArrayMetaData array=(ArrayMetaData)space.read(tmpl,null,2*60*1000);


int si=array.getsize();
System.out.println("before reading");
for(int i=0;i<si;i++)
{
ArrayElement tmpls=new ArrayElement(tupleid,i);
ArrayElement arr=(ArrayElement)space.read(tmpls,null,2*60*1000);
System.out.println(" "+arr.toString());
}
}catch(Exception ex){ex.printStackTrace();return false; }
return true;
}

}


sread.java


import java.util.Scanner;

public class sread {

public static void main(String args[])
{
try{
Scanner scanner;
String tupleid;
int size;
Boolean tw,tr;
scanner=new Scanner(System.in);
System.out.print("Enter the tupleid to be read:");
tupleid=scanner.next();
aclient tuple1=new aclient();


tr=tuple1.readtuple(tupleid);
if(tr==false)
System.out.println("Read Failure");

}catch(Exception ex) {}

}


}


swrite.java

import java.util.Scanner;

public class swrite {

public static void main(String args[])
{
try{
Scanner scanner;
String tupleid;
int size;
Boolean tw,tr;
scanner=new Scanner(System.in);
System.out.print("Enter the tupleid to be write:");
tupleid=scanner.next();


System.out.print("Enter number of elements:");
size=scanner.nextInt();
String tuplefield[] =new String[size];
System.out.println("Enter tuple elements:");
for(int i=0;i<size;i++)
{
tuplefield[i]=scanner.next();
}
aclient tuple1=new aclient();
tw= tuple1.writetuple(tupleid,size,tuplefield);
if(tw==false)
System.out.println("Write failure");



}catch(Exception ex) {}

}


}


ArrayElement.java


import java.io.Serializable;
import net.jini.core.entry.*;
public class ArrayElement implements Entry{
public String name;
public Integer index;
public Serializable data;

public ArrayElement()
{

}
public ArrayElement(String n,Integer i)
{
name=n;
index=i;
}

public ArrayElement(String name,Integer i,Serializable dat)
{
this(name,i);
data =dat;
}
public String toString()
{

String s="\nName "+name+" Index "+index+" Data "+data;
return s;
}
}


ArrayMetaData.java


import net.jini.core.entry.*;
public class ArrayMetaData implements Entry{

public String name;
public Integer size;

public ArrayMetaData()
{
}

public ArrayMetaData(String n)
{
name=n;
}
public ArrayMetaData(String n,Integer s)
{
name=n;
size =new Integer(s);
}

public int getsize()
{
return size.intValue();
}
}


Lookup.java



import java.io.IOException;

import java.rmi.RemoteException;

import net.jini.core.lookup.ServiceRegistrar;
import net.jini.core.lookup.ServiceTemplate;

import net.jini.discovery.LookupDiscovery;
import net.jini.discovery.DiscoveryListener;
import net.jini.discovery.DiscoveryEvent;

/**
A class which supports a simple JINI multicast lookup. It doesn't register
with any ServiceRegistrars it simply interrogates each one that's
discovered for a ServiceItem associated with the passed interface class.
i.e. The service needs to already have registered because we won't notice
new arrivals. [ServiceRegistrar is the interface implemented by JINI
lookup services].

@todo Be more dynamic in our lookups - see above

@author Dan Creswell (dan@dancres.org)
@version 1.00, 7/9/2003
*/
class Lookup implements DiscoveryListener {
private ServiceTemplate theTemplate;
private LookupDiscovery theDiscoverer;

private Object theProxy;

/**
@param aServiceInterface the class of the type of service you are
looking for. Class is usually an interface class.
*/
Lookup(Class aServiceInterface) {
Class[] myServiceTypes = new Class[] {aServiceInterface};
theTemplate = new ServiceTemplate(null, myServiceTypes, null);
}

/**
Having created a Lookup (which means it now knows what type of service
you require), invoke this method to attempt to locate a service
of that type. The result should be cast to the interface of the
service you originally specified to the constructor.

@return proxy for the service type you requested - could be an rmi
stub or an intelligent proxy.
*/
Object getService() {
synchronized(this) {
if (theDiscoverer == null) {

try {
theDiscoverer =
new LookupDiscovery(LookupDiscovery.ALL_GROUPS);
theDiscoverer.addDiscoveryListener(this);
} catch (IOException anIOE) {
System.err.println("Failed to init lookup");
anIOE.printStackTrace(System.err);
}
}
}

return waitForProxy();
}

/**
Location of a service causes the creation of some threads. Call this
method to shut those threads down either before exiting or after a
proxy has been returned from getService().
*/
void terminate() {
synchronized(this) {
if (theDiscoverer != null)
theDiscoverer.terminate();
}
}

/**
Caller of getService ends up here, blocked until we find a proxy.

@return the newly downloaded proxy
*/
private Object waitForProxy() {
synchronized(this) {
while (theProxy == null) {

try {
System.out.println("Above wait");
wait();

} catch (InterruptedException anIE) {
}
}
System.out.println("After wait");
return theProxy;
}
}

/**
Invoked to inform a blocked client waiting in waitForProxy that
one is now available.

@param aProxy the newly downloaded proxy
*/
private void signalGotProxy(Object aProxy) {
synchronized(this) {
if (theProxy == null) {
theProxy = aProxy;
notify();
}
}
}

/**
Everytime a new ServiceRegistrar is found, we will be called back on
this interface with a reference to it. We then ask it for a service
instance of the type specified in our constructor.
*/
public void discovered(DiscoveryEvent anEvent) {
synchronized(this) {
if (theProxy != null)
return;
}

ServiceRegistrar[] myRegs = anEvent.getRegistrars();

for (int i = 0; i >< myRegs.length; i++) {
ServiceRegistrar myReg = myRegs[i];

Object myProxy = null;

try {
myProxy = myReg.lookup(theTemplate);

if (myProxy != null) {
signalGotProxy(myProxy);
break;
}
} catch (RemoteException anRE) {
System.err.println("ServiceRegistrar barfed");
anRE.printStackTrace(System.err);
}
}
}

/**
When a ServiceRegistrar "disappears" due to network partition etc.
we will be advised via a call to this method - as we only care about
new ServiceRegistrars, we do nothing here.
*/
public void discarded(DiscoveryEvent anEvent) {
}
}

Note :

In write method in wrtietuple() --- i mentioned lease time is ten mini
Read method in readtuple()--- I mentioned lease time is two mini.

Sometime i could not get output with in ten mini.


William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
It appears that you always assume the space reference will be non-null and that you have paid no attention to my suggestion to code defensively.

If the following line throws an NPE



dont you think there is a possibility that the "space" reference is null?

Bill
 
wood burning stoves
 
subject: Reliablity in JavaSpaces
 
Similar Threads
Can't get the result of factorial method
Issue with console
javaspace transaction error
Mahalo(TransactionManager) Service in Jini
Hibernate Query