2009-02-03 52 views
4

誰能告訴我如何通過java代碼找到可用的代理容器?我正在使用JADE代理框架,並且我已經想出瞭如何創建新容器,但找不到現有容器(以便可以在其中部署代理)。Jade Agent Containers

+0

這是一個老問題,但任何人搜索有一個較新的類似的問題,接受的答案是集中於獲取Containers作爲「正在進行的服務」:http://stackoverflow.com/questions/9094522/getting-a-list-of-all-jade-containers – edmundo096 2015-10-20 21:04:21

回答

5

這樣做有兩種方法,具體取決於您是希望通過正在進行的服務還是通過消息中的當前快照接收信息。

要獲取當前可用代理容器標識的快照,請向代理管理服務(AMS)發送請求消息並等待其答覆。使用JADE管理本體論和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註冊創建一個擴展AMSSubscriber的行爲。註冊的處理程序AddedContainer事件,你將能夠訪問新的可用容器的數據筒:

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()); 
} 

希望這有助於

拉斯

+0

in新的JADE有沒有其他的方式比與AMS溝通? – Purushottam 2012-02-01 10:57:29