问题从一个类Corba创建许多服务器

时间:2013-04-19 15:38:03

标签: java corba

对于单一任务,我们必须创建一种管理洪水传感器的方法,并在水位变高时发出警报。所以我的想法是创建一个LMS(本地监控站),它将保存传感器服务器的arrayList,然后从那里管理它们。

所以我创建了一个测试类来测试添加它们的实现;

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

问题:

如何处理这样的情况?或者我是以完全错误的方式解决这个问题?

1 个答案:

答案 0 :(得分:1)

您拥有相同的ORB初始端口。要创建多个CORBA服务器,您必须为每个服务器提供自己的端口...

String[] newArgs = {"-ORBInitialPort", "1050"}; 

端口"1050"不能是一个神奇的常量,但需要是一个参数或局部变量,它会更改为每个创建的服务器的新值。