Re: Time Service for resource Utilization in Jini
Gregg Wonderly <[email protected]> Tue, 28 Oct 2008 11:01:58 -0500
| Newsgroups | gmane.comp.java.sun.jini |
|---|---|
| Message-ID | <[email protected]> |
Bishnu Gautam wrote:
> Hello Jini Friends
>
> I am planning to develop Service cloud application based in Jini. In my
> application, client will be able to utilize a service from the cloud
> developed in Jini. This cloud will be a persistent store of Jini
> services. However, I need an idea about how a service provider will
> understand the time duration of the service usage by the client. My
> point is once the service is utilized by the client, the notification
> should go to the service provider and also while the client stop to use
> the service so that the service provider will be able to calculate the
> time duration used by the client.
> I would be very thankful to Jini Community if any of you answer me about it.
> Thank you very much for your help in this regards
In Java, the predominately easy way to do these types of things is by plugging
in a delegate implementation of the service interface at the service level.
What you would do, is develop a java.lang.reflect.InvocationHandler, which would
do the appropriate "logging" of needed information based on class via the
invoke() method you would implement. Each client would then need a separate
service endpoint so that their usage would be seen as unique.
What I typically do, is have the core, registered service just have a factory
interface that I call to get a service instance specific to my client. I pass
in authentication information to that factory method.
public class RemoteIdentity {
String name;
Object credentials;
}
public interface RemoteConnection<T extends Remote> extends Remote {
public T getAccess( RemoteIdentity id ) throws RemoteException;
}
The java.lang.reflect.Proxy class provides the factory method to create a new
Proxy implementing a specified set of Interfaces. So, you migh see getAccess()
implemented as something like:
public MyService getAccess( RemoteIdentity id )
throws RemoteException, LoginException {
tryLogin( id ); // throws LoginException if failed
MyService realService = ... get reference to local service ...
MyAcctHandler h = new MyAcctHandler( id, realService );
MyService delegateProxy = Proxy.newProxyInstance(
MyService.class.getClassLoader(),
new Class[] { MyService.class, ... },
h );
// now we have a MyService proxy that we want the user to
// make calls through, so export that.
Exporter exp = (Exporter)config.getEntry( ... ); // get exporter
MyService pxy = (MyService)exp.export( delegateProxy );
return pxy;
}
Because you are using one proxy per client, you need to use Exporter
facilities for retaining live references and otherwise managing the lifecycle of
these proxy instances with DGC or some such.
The MyAcctHandler class is the secret weapon here because you can code into it,
how it manages the accounting data you need it to keep about client usage. You
can track the total time spent in the server for example with something like
public class MyAcctHandler implements java.lang.reflect.InvocationHandler {
private final MyService svc;
private final RemoteIdentity id;
private Logger log = Logger.getLogger( getClass().getName() );
public MyAcctHandler( RemoteIdentity id, MyService svc ) {
this.svc = svc;
this.id = id;
}
public Object invoke(Object proxy, Method method, Object[] args) {
long now = System.currentTimeMillis();
Object res = method.invoke( mysvc, args );
long elapsed = System.currentTimeMillis() - now;
try {
logUsage( id, elapsed, method );
} catch( Exception ex ) {
log.log( Level.SEVERE, ex.toString(), ex );
}
return res;
}
private void logUsage( RemoteIdentity id, long elapsed,
Method method ) {
... do something here to record the event ...
}
}
There are further examples of this type of service access and some code at
http://pastion.dev.java.net.
Gregg Wonderly
--------------------------------------------------------------------------
Getting Started: http://www.jini.org/wiki/Category:Getting_Started
Community Web Site: http://jini.org
jini-users Archive: http://archives.java.sun.com/archives/jini-users.html
Unsubscribing: email "signoff JINI-USERS" to [email protected]