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

jadex.micro.examples.messagequeue.MessageQueueAgent Maven / Gradle / Ivy

Go to download

The Jadex micro applications package contains several example applications, benchmarks and testcases using micro agents.

There is a newer version: 4.0.267
Show newest version
package jadex.micro.examples.messagequeue;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import jadex.bridge.IInternalAccess;
import jadex.bridge.SFuture;
import jadex.bridge.service.annotation.Service;
import jadex.commons.future.IFuture;
import jadex.commons.future.ISubscriptionIntermediateFuture;
import jadex.commons.future.SubscriptionIntermediateFuture;
import jadex.micro.annotation.Agent;
import jadex.micro.annotation.AgentCreated;
import jadex.micro.annotation.Implementation;
import jadex.micro.annotation.ProvidedService;
import jadex.micro.annotation.ProvidedServices;

/**
 *  This agent represents the central message queue. It offers a 
 *  message queue service that is used by the clients.
 */
@Agent
@Service
@ProvidedServices(@ProvidedService(type=IMessageQueueService.class, implementation=@Implementation(expression="$pojoagent")))
public class MessageQueueAgent implements IMessageQueueService
{
	//-------- attributes --------
	
	/** The agent. */
	@Agent
	protected IInternalAccess agent;
	
	/** The map of subscribers. */
	protected Map>> subscribers;
	
	//-------- methods --------

	/**
	 *  Called on agent creation.
	 */
	@AgentCreated
	public void agentCreated()
	{
		this.subscribers = new HashMap>>();
	}
	
	/**
	 *  Subscribe to a specific topic. New events that fit to the topic
	 *  are forwarded to all subscribers as intermediate results.
	 *  A subscribe can unsubscribe by terminating the future.
	 *  @param topic The topic.
	 *  @return The events.
	 */
	public ISubscriptionIntermediateFuture subscribe(String topic)
	{
//		SubscriptionIntermediateFuture ret = new SubscriptionIntermediateFuture();
		final SubscriptionIntermediateFuture	ret	= (SubscriptionIntermediateFuture)SFuture.getNoTimeoutFuture(SubscriptionIntermediateFuture.class, agent);

		List> subs = subscribers.get(topic);
		if(subs==null)
		{
			subs = new ArrayList>();
			subscribers.put(topic, subs);
		}
		subs.add(ret);
		
		return ret;
	}
	
	/**
	 *  Publish a new event to the queue.
	 *  @param topic The topic.
	 *  @param event The event to publish.
	 */
	public IFuture publish(String topic, Event event)
	{
//		System.out.println("pub: "+topic+" "+event);
		List> subs = subscribers.get(topic);
		if(subs!=null)
		{
			for(Iterator> it = subs.iterator(); it.hasNext(); )
			{
				SubscriptionIntermediateFuture sub = it.next();
				if(!sub.addIntermediateResultIfUndone(event))
				{
					System.out.println("Removed: "+sub);
					it.remove();
				}
			}
			if(subs.isEmpty())
				subscribers.remove(topic);
		}
		
		return IFuture.DONE;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy