有谁能告诉我如何通过java代码找到可用的代理容器?我正在使用JADE代理框架,我已经找到了如何创建新容器但找不到现有容器(以便可以在其中部署代理)。
答案 0 :(得分:5)
有两种方法可以执行此操作,具体取决于您是要通过正在进行的服务接收信息还是通过消息中的当前快照接收信息。
要获取当前可用代理容器的ID的快照,请向代理管理服务(AMS)发送请求消息并等待其回复。使用JADE Management Ontology和 QueryPlatformLocationsAction 术语,发送和接收方法将是:
private void queryAMS() throws CodecException, OntologyException {
QueryPlatformLocationsAction query = new QueryPlatformLocationsAction();
Action action = new Action(myAgent.getAID(), query);
ACLMessage message = new ACLMessage(ACLMessage.REQUEST);
message.addReceiver(myAgent.getAMS());
message.setLanguage(FIPANames.ContentLanguage.FIPA_SL);
message.setOntology(JADEManagementOntology.getInstance().getName());
myAgent.getContentManager().fillContent(message, action);
myAgent.send(message);
}
private void listenForAMSReply() throws UngroundedException, CodecException,
OntologyException {
ACLMessage receivedMessage = myAgent.blockingReceive(MessageTemplate
.MatchSender(myAgent.getAMS()));
ContentElement content = myAgent.getContentManager().extractContent(
receivedMessage);
// received message is a Result object, whose Value field is a List of
// ContainerIDs
Result result = (Result) content;
List listOfPlatforms = (List) result.getValue();
// use it
Iterator iter = listOfPlatforms.iterator();
while (iter.hasNext()) {
ContainerID next = (ContainerID) iter.next();
System.out.println(next.getID());
}
}
要将此信息作为正在进行的服务获取,并在向AMS注册时接收每个容器的ContainerID,请创建一个扩展AMSSubscriber的行为。注册AddedContainer事件的处理程序,您将能够访问新可用容器的ContainerID:
public class AMSListenerBehaviour extends AMSSubscriber {
@Override
public void installHandlers(Map handlersTable) {
handlersTable.put(AddedContainer.NAME, addedContainerHandler);
}
public final class AddedContainerHandler implements EventHandler {
@Override
public void handle(Event ev) {
AddedContainer event = (AddedContainer) ev;
ContainerID addedContainer = event.getContainer();
System.out.println(addedContainer.getID());
}
希望这有帮助,
拉斯