jadex.micro.tutorial.ChatServiceD1 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.tutorial;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import jadex.bridge.IInternalAccess;
import jadex.bridge.service.annotation.Service;
import jadex.bridge.service.annotation.ServiceComponent;
import jadex.bridge.service.annotation.ServiceStart;
import jadex.bridge.service.component.IRequiredServicesFeature;
import jadex.bridge.service.types.clock.IClockService;
import jadex.commons.future.ExceptionDelegationResultListener;
import jadex.commons.future.Future;
import jadex.commons.future.IFuture;
/**
* Chat service implementation.
*/
@Service
public class ChatServiceD1 implements IChatService
{
//-------- attributes --------
/** The agent. */
@ServiceComponent
protected IInternalAccess agent;
/** The required services feature **/
@ServiceComponent
private IRequiredServicesFeature requiredServicesFeature;
/** The clock service. */
protected IClockService clock;
/** The time format. */
protected DateFormat format;
//-------- attributes --------
/**
* Init the service.
*/
@ServiceStart
public IFuture startService()
{
final Future ret = new Future();
this.format = new SimpleDateFormat("hh:mm:ss");
IFuture fut = requiredServicesFeature.getRequiredService("clockservice");
fut.addResultListener(new ExceptionDelegationResultListener(ret)
{
public void customResultAvailable(IClockService result)
{
clock = result;
ret.setResult(null);
}
});
return ret;
}
/**
* Receives a chat message.
* @param sender The sender's name.
* @param text The message text.
*/
public void message(final String sender, final String text)
{
System.out.println(agent.getComponentIdentifier().getLocalName()+" received at "
+format.format(new Date(clock.getTime()))+" from: "+sender+" message: "+text);
}
}