我正在尝试在同一主机上生成更多的Jade实例。我知道可以通过cmd行,但是我无法使用某些Java代码来做到这一点。我无法在Java中运行命令行,因为这样一来,我可能会丢失一些有关platfomr的信息。有人可以帮助我吗? 在我的代码和错误下方:
public class MainCMD_ {
static String classAgentName= "Agents.MyAgent";
static int agentNum=6;
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
String platformHost= "localhost";
String platformName="Platform_";
int platformPort= 2000;
for (int i =1; i<3;i++) {
platformName="Platform_"+i;
while(!isAvailablePort(platformPort)){
platformPort++;
}
//new jade.Boot3(new String[] {"-local-host",platformHost,"-local-port",Integer.toString(platformPort),"-name",platformName,"-gui"});
genPlatform(platformHost,platformName,platformPort);
System.out.println("Platform "+platformName+" created!");
}
}
private static void genPlatform (String platformHost, String platformName, int platformPort) {
Runtime runtime = Runtime.instance();
ProfileImpl profile = new ProfileImpl();
profile.setParameter(Profile.PLATFORM_ID, platformName);
profile.setParameter(Profile.LOCAL_HOST, platformHost);
profile.setParameter(Profile.LOCAL_PORT, Integer.toString(platformPort));
profile.setParameter(Profile.GUI, "true");
profile.setParameter(Profile.MAIN, "false");
//profile.setParameter(Profile.CONTAINER_NAME, "MainContainer");
//System.out.println(profile.getProperties());
ContainerController cc = runtime.createMainContainer(profile);
AgentController ac;
for(int i = 1; i<agentNum;i++) {
try {
ac=cc.createNewAgent("MyAgent"+i, classAgentName, null);
ac.start();
} catch (StaleProxyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private static boolean isAvailablePort(int port) {
//metodo per verificare che la porta sia disponibile
ServerSocket ss = null;
DatagramSocket ds = null;
try {
ss = new ServerSocket(port);
ss.setReuseAddress(true);
ds = new DatagramSocket(port);
ds.setReuseAddress(true);
return true;
} catch (IOException e) {
} finally {
if (ds != null) {
ds.close();
}
if (ss != null) {
try {
ss.close();
} catch (IOException e) {
}
}
}
return false;
}
}
控制台
set 15, 2018 5:56:06 PM jade.core.Runtime beginContainer
INFORMAZIONI: ----------------------------------
This is JADE 4.4.0 - revision 6778 of 21-12-2015 12:24:43
downloaded in Open Source, under LGPL restrictions,
at http://jade.tilab.com/
----------------------------------------
set 15, 2018 5:56:06 PM jade.imtp.leap.LEAPIMTPManager initialize
INFORMAZIONI: Listening for intra-platform commands on address:
- jicp://192.168.1.100:2000
set 15, 2018 5:56:06 PM jade.core.AgentContainerImpl init
AVVERTENZA: Automatic main-detection mechanism initialization failed
(Error setting up multicast socket - Caused by: Can't assign requested
address). Mechanism disabled!
set 15, 2018 5:56:06 PM jade.core.BaseService init
INFORMAZIONI: Service jade.core.management.AgentManagement initialized
set 15, 2018 5:56:07 PM jade.core.BaseService init
INFORMAZIONI: Service jade.core.messaging.Messaging initialized
set 15, 2018 5:56:07 PM jade.core.BaseService init
INFORMAZIONI: Service jade.core.resource.ResourceManagement initialized
set 15, 2018 5:56:07 PM jade.core.BaseService init
INFORMAZIONI: Service jade.core.mobility.AgentMobility initialized
set 15, 2018 5:56:07 PM jade.core.BaseService init
INFORMAZIONI: Service jade.core.event.Notification initialized
set 15, 2018 5:56:07 PM jade.mtp.http.HTTPServer <init>
INFORMAZIONI: HTTP-MTP Using XML parser .
com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser
set 15, 2018 5:56:07 PM jade.core.messaging.MessagingService boot
INFORMAZIONI: MTP addresses:
http://mbp-di-mauro:7778/acc
set 15, 2018 5:56:07 PM jade.core.AgentContainerImpl joinPlatform
INFORMAZIONI: --------------------------------------
如您所见,在这里启动Platform 1
Agent container Main-Container@192.168.1.100 is ready.
--------------------------------------------
Hello, I'm agent
I'm going to ...
Hello, I'm agent
I'm going to ...
Hello, I'm agent
I'm going to ...
Hello, I'm agent
I'm going to ...
Platform Platform_1 created!
Hello, I'm agent
I'm going to ...
set 15, 2018 5:56:07 PM jade.core.Runtime beginContainer
INFORMAZIONI: ----------------------------------
This is JADE 4.4.0 - revision 6778 of 21-12-2015 12:24:43
downloaded in Open Source, under LGPL restrictions,
at http://jade.tilab.com/
----------------------------------------
set 15, 2018 5:56:07 PM jade.imtp.leap.LEAPIMTPManager initialize
INFORMAZIONI: Listening for intra-platform commands on address:
- jicp://192.168.1.100:2000
- jicp://192.168.1.100:2001
set 15, 2018 5:56:07 PM jade.core.AgentContainerImpl init
AVVERTENZA: Automatic main-detection mechanism initialization failed
(Error setting up multicast socket - Caused by: Can't assign requested
address). Mechanism disabled!
set 15, 2018 5:56:07 PM jade.core.AgentContainerImpl joinPlatform
GRAVE: Some problem occurred while joining agent platform.
jade.core.ProfileException: Can't get a proxy to the Platform Manager -
这里是错误
Caused by: Wrong platform name Platform_1. It should be Platform_2
at jade.core.ProfileImpl.createPlatformManager(ProfileImpl.java:529)
at jade.core.ProfileImpl.getPlatformManager(ProfileImpl.java:442)
at jade.core.ProfileImpl.getServiceManager(ProfileImpl.java:456)
at jade.core.AgentContainerImpl.init(AgentContainerImpl.java:347)
at
jade.core.AgentContainerImpl.joinPlatform(AgentContainerImpl.java:494)
at jade.core.Runtime.createMainContainer(Runtime.java:159)
at MainCMD_.genPlatform(MainCMD_.java:55)
at MainCMD_.main(MainCMD_.java:33)
Nested Exception:
jade.core.IMTPException: Wrong platform name Platform_1. It should be
Platform_2
at jade.imtp.leap.CommandDispatcher.
setPlatformManager(CommandDispatcher.java:231)
at jade.imtp.leap.CommandDispatcher.
registerSkeleton(CommandDispatcher.java:731)
at jade.imtp.leap.LEAPIMTPManager.exportPlatformManager
(LEAPIMTPManager.java:198)
at jade.core.ProfileImpl.createPlatformManager(ProfileImpl.java:518)
at jade.core.ProfileImpl.getPlatformManager(ProfileImpl.java:442)
at jade.core.ProfileImpl.getServiceManager(ProfileImpl.java:456)
at jade.core.AgentContainerImpl.init(AgentContainerImpl.java:347)
at
jade.core.AgentContainerImpl.joinPlatform(AgentContainerImpl.java:494)
at jade.core.Runtime.createMainContainer(Runtime.java:159)
at MainCMD_.genPlatform(MainCMD_.java:55)
at MainCMD_.main(MainCMD_.java:33)
Exception in thread "main" java.lang.NullPointerException
at MainCMD_.genPlatform(MainCMD_.java:61)
at MainCMD_.main(MainCMD_.java:33)
答案 0 :(得分:0)
关于您的最后一个问题:是的,有可能。
假设您使用Eclipse,则可以有两个(或更多)Run Configurations。每个配置的参数部分都应不同(对应于main方法中的args数组)。使用这些参数,您应该能够区分平台及其配置。
正如Runtime.instance();
访问指示的那样,通过代码进行的JADE平台处理由Singleton实现。因此,一个JVM中将不能拥有多个JADE平台。使用上述方法,每个运行配置将产生一个JVM,并且您确实可以发送ACL消息,而不是在平台上移动消息(如果要在同一平台的代理之间发送消息,JADE不会序列化/反序列化消息)
希望这会有所帮助!