All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jadex.micro.producerconsumer.ProducerAgent Maven / Gradle / Ivy

The newest version!
package jadex.micro.producerconsumer;

import java.util.Queue;
import java.util.function.Supplier;

import jadex.core.IComponent;
import jadex.execution.IExecutionFeature;
import jadex.future.FutureBlockingQueue;
import jadex.micro.annotation.Agent;
import jadex.model.annotation.OnStart;

@Agent
public class ProducerAgent 
{
	protected FutureBlockingQueue queue;
	protected Supplier create;
	protected long maxdelay;
	
	public ProducerAgent(FutureBlockingQueue queue, Supplier create)
	{
		this(queue, 2000, create);
	}
	
	public ProducerAgent(FutureBlockingQueue queue, long maxdelay, Supplier create)
	{
		this.queue = queue;
		this.maxdelay = maxdelay;
		this.create = create;
	}
	
	@OnStart
	protected void onStart(IComponent agent)
	{
		while(true)
		{
			long delay = (long)(Math.random()*maxdelay);
			System.out.println(agent.getId()+" waiting: "+delay);
			agent.getFeature(IExecutionFeature.class).waitForDelay(delay).get();
			
			R item = create.get();
			System.out.println(agent.getId()+" created: "+item);
			queue.enqueue(item).get();
			System.out.println(agent.getId()+" added: "+item+" "+queue.size());
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy