首页 > 解决方案 > 使用 XML 的 CORBA 消息传递——如何

问题描述

我想使用 Java 客户端和 jacorb 与现有的 ORB 交互。idl 非常简单,只指定一个 XmlDocument:

// xml.idl
module messaging
{
        interface Xml
        {
                typedef sequence <octet>  XmlDocument;
                void process_request ( inout XmlDocument document );
        };
};

我想使用 sysValidateRequest.xsd 验证某些对象:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:pt="http://www.example.com/sys4/services" targetNamespace="http://www.example.com/sys4/services" elementFormDefault="qualified" attributeFormDefault="unqualified">
            <xs:include schemaLocation="./sys_types.xsd"/>
            <xs:element name="sysValidateRequest">
                        <xs:annotation>
                                    <xs:documentation>validate sysObject</xs:documentation>
                        </xs:annotation>
                        <xs:complexType>
                                    <xs:sequence>
                                                <xs:element name="sysIdentifier" type="pt:sysIdentifierType" maxOccurs="unbounded">
                                                            <xs:annotation>
                                                                     <xs:documentation>sys object identifiers</xs:documentation>
                                                            </xs:annotation>
                                                </xs:element>
                                    </xs:sequence>
                                    <xs:attribute name="sid" type="pt:sysSessionIDType" use="required">
                                                <xs:annotation>
                                                            <xs:documentation>The session identifier which is unique to the session and the user who is logged in.</xs:documentation>
                                                </xs:annotation>
                                    </xs:attribute>
                                    <xs:attribute name="tid" type="pt:sysTransactionIdType" use="optional">
                                                <xs:annotation>
                                                            <xs:documentation>If an transaction id is sent with an request it will be sent back in an response.</xs:documentation>
                                                </xs:annotation>
                                    </xs:attribute>
                        </xs:complexType>
            </xs:element>
</xs:schema>

和 sysValidateRespone.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:pt="http://www.example.com/sys4/services" targetNamespace="http://www.example.com/sys4/services" elementFormDefault="qualified" attributeFormDefault="unqualified">
            <xs:include schemaLocation="./sys_types.xsd"/>
            <xs:element name="sysGetKeysResponse">
                        <xs:annotation>
                                    <xs:documentation>info for key(s) for object in given registration</xs:documentation>
                        </xs:annotation>
                        <xs:complexType>
                                    <xs:choice>
                                                <xs:element name="context" type="pt:keySetWithKindType" maxOccurs="unbounded">
                                                            <xs:annotation>
                                                                        <xs:documentation>the given context of the retrieved keys</xs:documentation>
                                                            </xs:annotation>
                                                </xs:element>
                                                <xs:element name="sysException" type="pt:exceptionType">
                                                            <xs:annotation>
                                                                        <xs:documentation>On error, the exception structure with detailed error information is returned.</xs:documentation>
                                                            </xs:annotation>
                                                </xs:element>
                                    </xs:choice>
                                    <xs:attribute name="tid" type="pt:sysTransactionIdType" use="optional">
                                                <xs:annotation>
                                                            <xs:documentation>If an transaction id is sent with an request it will be sent back in an response.</xs:documentation>
                                                </xs:annotation>
                                    </xs:attribute>
                        </xs:complexType>
            </xs:element>
</xs:schema>

我使用生成的java类

idlj  -fall  xml.idl

并获得了以下课程

- XmlPackage
-- XmlDocumentHolder.java
-- XmlDocumentHelper.java
- XmlHelper.java
- _XmlStub.java
- XmlPOA.java
- XmlOperations.java
- Xml.java
- XmlHolder.java

我遵循了几个教程,现在我拥有的是:

import messaging.Xml;
import messaging.XmlHelper;
import messaging.XmlPackage.XmlDocumentHolder;
import org.omg.CORBA.ORBPackage.InvalidName;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.omg.CORBA.ORB;

@SpringBootApplication
// @Service
public class sysImportApplication {

            //@Autowired
            //private ApplicationContext ctx;

            public static void main(String[] args) {
                        SpringApplication.run(sysImportApplication.class, args);

                        // Initialize the ORB
                        ORB orb = ORB.init(args, null);
                        org.omg.CORBA.Object ref = null;

                        // The object name passed in on the command line
                        String name = "corbaloc:iiop:sysHost01:50000/StandardImplName/sys/sys";

                        System.out.println("Attempting to lookup " + name);
                        ref = orb.string_to_object(name);

                        // System.out.println(orb.list_initial_services());
                        Xml xml = XmlHelper.narrow(ref);

                        // how to send XmlDocument now?
                        // and get response?

                        // XmlDocumentHolder h = new XmlDocumentHolder();
                        // xml.process_request(h);
            }
}

我需要帮助我如何发送 xml 文档...

在教程中,帮助类通常返回一个远程对象的实例,例如

Account a = AccountHelper.narrow(ref)

我可以在其上调用方法,例如

a.setAmount(100)

这里返回了一个 xml:

Xml xml = Account

它只有方法:

xml.process_request(XmlDocumentHolder holder)

和 XmlDocumentHolder 只接受字节数组...

任何帮助表示赞赏!

标签: javaxmlcorbaidljacorb

解决方案


你只得到了这个函数process_request(),因为它是你在 IDL 文件中定义的唯一一个,它得到一个holder对象是因为你将参数设置为inout参数。

如果您想为您的对象指定一些新方法,您需要首先在您的 idl 文件中定义它们:并且要holder在参数中不获取对象,您需要使其仅inout参数。例如:

module messaging
{
        interface Xml
        {
                typedef sequence <octet>  XmlDocument;
                void process_request ( in XmlDocument document ); // this method to process your object and make changes to it
                XmlDocument get_result();// this method to get your new xmldocument

// this methods are like the getters and setters and they are to easy to work with  
        };
};

ps:只是一个建议来增强你的代码使用下面的方法来初始化你的球体

        Properties props = new Properties();
        props.put("org.omg.CORBA.ORBInitialHost","localhost");
        props.put("org.omg.CORBA.ORBInitialPort",
                "4321");
        ORB orb = ORB.init((String[]) null, props);

这种方式可以帮助您避免在执行之前添加 args 参数。

您还需要为您的服务器创建一个特殊的项目,以便您可以定义您的方法以及它们处理对象的方式。


推荐阅读