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

jadex.micro.quickstart.NonblockingTimeUserAgent 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.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);
					}
				});				
			}
		});
	}	
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy