jadex.micro.examples.messagequeue.replicated.UserAgent 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.examples.messagequeue.replicated;
import jadex.bridge.IComponentStep;
import jadex.bridge.IInternalAccess;
import jadex.bridge.component.IExecutionFeature;
import jadex.commons.future.IFuture;
import jadex.commons.future.ISubscriptionIntermediateFuture;
import jadex.commons.future.IntermediateDefaultResultListener;
import jadex.micro.annotation.Agent;
import jadex.micro.annotation.AgentArgument;
import jadex.micro.annotation.AgentBody;
import jadex.micro.annotation.AgentService;
import jadex.micro.annotation.Argument;
import jadex.micro.annotation.Arguments;
import jadex.micro.annotation.Binding;
import jadex.micro.annotation.RequiredService;
import jadex.micro.annotation.RequiredServices;
import jadex.micro.examples.messagequeue.Event;
/**
*
*/
@Agent
@RequiredServices(@RequiredService(name = "mq", type = IMessageQueueReplicableService.class, binding = @Binding(scope = Binding.SCOPE_APPLICATION)))
@Arguments(@Argument(name = "topic", clazz = String.class, defaultvalue = "\"default_topic\""))
public class UserAgent
{
// -------- attributes --------
/** The agent. */
@Agent
protected IInternalAccess agent;
/** The message queue. */
@AgentService
protected IMessageQueueReplicableService mq;
/** The topic argument. */
@AgentArgument
protected String topic;
// -------- methods --------
/**
* The agent body.
*/
@AgentBody
public void body()
{
final ISubscriptionIntermediateFuture fut = mq.subscribe(topic);
fut.addResultListener(new IntermediateDefaultResultListener()
{
public void intermediateResultAvailable(Event event)
{
System.out.println("Received: "
+ agent.getComponentIdentifier() + " " + event);
}
public void exceptionOccurred(Exception exception)
{
System.out.println("Ex: " + exception);
}
});
IComponentStep step = new IComponentStep()
{
final int[] cnt = new int[1];
public IFuture execute(IInternalAccess ia)
{
if(!(cnt[0] % 2 == 0))
{
// publish also remote
mq.publish(topic, new Event("some type", cnt[0]++, agent.getComponentIdentifier()), true);
}
else
{
// just publish local
mq.publish(topic,new Event("some type", cnt[0]++, agent.getComponentIdentifier()), false);
}
if(cnt[0] < 10)
{
agent.getComponentFeature(IExecutionFeature.class).waitForDelay(1000, this);
}
else
{
fut.terminate();
}
return IFuture.DONE;
}
};
agent.getComponentFeature(IExecutionFeature.class).waitForDelay(1000, step);
}
}