jadex.micro.quickstart.NonblockingTimeUserAgent Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jadex-applications-micro Show documentation
Show all versions of jadex-applications-micro Show documentation
The Jadex micro applications package contains several example applications, benchmarks and testcases using micro agents.
package jadex.micro.quickstart;
import jadex.bridge.service.IService;
import jadex.commons.future.IIntermediateFuture;
import jadex.commons.future.ISubscriptionIntermediateFuture;
import jadex.commons.future.IntermediateDefaultResultListener;
import jadex.micro.annotation.Agent;
import jadex.micro.annotation.AgentBody;
import jadex.micro.annotation.AgentService;
import jadex.micro.annotation.Binding;
import jadex.micro.annotation.RequiredService;
import jadex.micro.annotation.RequiredServices;
/**
* Simple agent that uses globally available time services.
* Non-blocking implementation using asynchronous result-listeners.
*/
@Agent
@RequiredServices(@RequiredService(name="timeservices", type=ITimeService.class, multiple=true,
binding=@Binding(scope=Binding.SCOPE_GLOBAL)))
public class NonblockingTimeUserAgent
{
//-------- attributes --------
/** The time services are searched and set at agent startup. */
@AgentService
private IIntermediateFuture timeservices;
//-------- methods --------
/**
* This method is called after agent startup.
*/
@AgentBody
public void body()
{
// Subscribe to all found time services.
timeservices.addResultListener(new IntermediateDefaultResultListener()
{
public void intermediateResultAvailable(final ITimeService timeservice)
{
ISubscriptionIntermediateFuture subscription = timeservice.subscribe();
subscription.addResultListener(new IntermediateDefaultResultListener()
{
/**
* This method gets called for each received time submission.
*/
public void intermediateResultAvailable(String time)
{
String platform = ((IService)timeservice).getServiceIdentifier().getProviderId().getPlatformName();
System.out.println("New time received from "+platform+" at "+timeservice.getLocation()+": "+time);
}
});
}
});
}
}