对于一项大学作业,我们必须创建一种管理洪水传感器并在水位过高时发出警报的方法。所以我的想法是创建一个 LMS(本地监控站),它将保存传感器服务器的数组列表,然后从那里管理它们。
所以我创建了一个测试类来测试添加它们的实现;
public class testClass
{
static public void main(String[] args)
{
ArrayList<FloodSensorServer> sensors = new ArrayList<FloodSensorServer>();
sensors.add(new FloodSensorServer("Sensor 1"));
sensors.add(new FloodSensorServer("Sensor 2"));
sensors.add(new FloodSensorServer("Sensor 3"));
sensors.add(new FloodSensorServer("Sensor 4"));
}
}
从逻辑上讲,我认为这可能有效,直到我运行它并看到它运行第一个 .add 然后停止。然后我意识到它因为这行代码而停止了;
orb.run();
public class FloorSensorServer
{
Sensor counter;
String sensorName;
public FloorSensorServer(String sensorName)
{
this.sensorName = sensorName;
createServer();
}
public void createServer()
{
try {
// Initialize the ORB
System.out.println("Creating sensor: " + sensorName);
String[] newArgs = {"-ORBInitialPort", "1050"};
ORB orb = ORB.init(newArgs, null);
// get reference to rootpoa & activate the POAManager
POA rootpoa = POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
rootpoa.the_POAManager().activate();
// Create the Count servant object
FloorSensorImpl sensor = new FloorSensorImpl(sensorName);
// get object reference from the servant
org.omg.CORBA.Object ref = rootpoa.servant_to_reference(sensor);
Sensor cref = SensorHelper.narrow(ref);
// Get a reference to the Naming service
org.omg.CORBA.Object nameServiceObj =
orb.resolve_initial_references ("NameService");
if (nameServiceObj == null) {
System.out.println("nameServiceObj = null");
return;
}
// Use NamingContextExt which is part of the Interoperable
// Naming Service (INS) specification.
NamingContextExt nameService = NamingContextExtHelper.narrow(nameServiceObj);
if (nameService == null) {
System.out.println("nameService = null");
return;
}
NameComponent[] countName = nameService.to_name(sensorName);
nameService.rebind(countName, cref);
// wait for invocations from clients
orb.run();
} catch(Exception e) {
System.err.println(e);
}
}
}
问题:
遇到这样的情况该如何处理?还是我的处理方式完全错误?
最佳答案
您对所有这些都具有相同的 ORB 初始端口。要创建多个 CORBA 服务器,您必须为每个服务器提供自己的端口...
也就是说,在
String[] newArgs = {"-ORBInitialPort", "1050"};
端口“1050”
不能是一个神奇的常量,而需要是一个参数或本地变量,为每个创建的服务器更改为新值。
关于java - 从一类 Corba 创建多个服务器时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16108340/