jadex.micro.testcases.blocking.ShutdownAgent 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.testcases.blocking;
import java.util.Map;
import jadex.bridge.IComponentIdentifier;
import jadex.bridge.IInternalAccess;
import jadex.bridge.component.IExecutionFeature;
import jadex.bridge.service.RequiredServiceInfo;
import jadex.bridge.service.annotation.Service;
import jadex.bridge.service.component.IRequiredServicesFeature;
import jadex.bridge.service.types.cms.CreationInfo;
import jadex.bridge.service.types.cms.IComponentManagementService;
import jadex.commons.SReflect;
import jadex.commons.future.DefaultTuple2ResultListener;
import jadex.commons.future.DelegationResultListener;
import jadex.commons.future.ExceptionDelegationResultListener;
import jadex.commons.future.Future;
import jadex.commons.future.IFuture;
import jadex.micro.annotation.Agent;
import jadex.micro.annotation.AgentBody;
import jadex.micro.annotation.Binding;
import jadex.micro.annotation.ComponentType;
import jadex.micro.annotation.ComponentTypes;
import jadex.micro.annotation.RequiredService;
import jadex.micro.annotation.RequiredServices;
/**
*
*/
@Agent
@Service
@ComponentTypes(@ComponentType(name="block", clazz=BlockAgent.class))
@RequiredServices(@RequiredService(name="cms", type=IComponentManagementService.class, binding=@Binding(scope=RequiredServiceInfo.SCOPE_PLATFORM)))
public class ShutdownAgent
{
//-------- attributes --------
/** The agent. */
@Agent
protected IInternalAccess agent;
/**
*
*/
@AgentBody
public IFuture body()
{
final Future ret = new Future();
IFuture fut = agent.getComponentFeature(IRequiredServicesFeature.class).getRequiredService("cms");
fut.addResultListener(new ExceptionDelegationResultListener(ret)
{
public void customResultAvailable(final IComponentManagementService cms)
{
// cms.createComponent("block", new CreationInfo(agent.getComponentIdentifier()))
cms.createComponent(BlockAgent.class.getName()+".class", new CreationInfo(agent.getComponentIdentifier()))
.addResultListener(new DefaultTuple2ResultListener>()
{
public void firstResultAvailable(final IComponentIdentifier cid)
{
// call several times a blocking method on the agent and then terminate it
agent.getComponentFeature(IRequiredServicesFeature.class).searchService(IBlockService.class, cid)
.addResultListener(new ExceptionDelegationResultListener(ret)
{
public void customResultAvailable(IBlockService bs)
{
int numBlocks = 1000;
if (SReflect.isAndroid()) {
numBlocks = 100;
}
for(int i=0; i(ret)
{
public void customResultAvailable(Void result)
{
cms.destroyComponent(cid).addResultListener(new ExceptionDelegationResultListener