Use jacorb to create a fixed ior

Source: Internet
Author: User

You can use the Naming Service or the interoperable object reference (IOR) method to obtain a CORBA object.

Ior stores almost all protocol information between ORB (Object Request Broker, Object Request proxy). It is used to establish communication between the client and the target object, and provides a standardized object reference format for ORB Interoperability. Each ior specifies one or more supported protocols. For each protocol, the IOR includes the proprietary information of that Protocol. For IIOP, each ior includes a host name, TCP/IP Port number, and an object key. The key identifies the target object based on the host name and port combination. An ior consists of three parts: warehouse ID, destination information, and object key.

Ior encodes all orb information in a string. The application can use this ior string to obtain the CORBA object to be operated. An ior example is as follows:

IOR: 2017100000000001749444c3a48656c6c6f0000702f48656c6f3a312e3000000000000000000000000005c0

Bytes

Bytes

The Dior command of jacorb can be used to parse the contents of the IOR string. The contents parsed by the IOR are as follows:

Type ID: IDL: helloapp/Hello: 1.0

Tag_internet_iop profiles:

Profile ID: 0

IIOP version: 1.2

HOST: 10.140.1.97

Port: 10501

Object key (URL): standardimplname/persistentpoa/myhelloimpl

Object key (HEX): 0x53 74 61 6e 64 61 72 64 49 6D 70 6C 4E 61 6D 6

5 2f 50 65 72 73 69 73 74 65 6e 74 50 4f 41 2f 4D 79 48 65 6C 6C 6f 49 6D 70 6c

 

-- Found 1 tagged components --

#0: tag_orb_type

Type: 1245790976 (jacorb)

To create a persistent ior, the host, port, and object key in the IOR must be fixed, and the object key must be composed of implname/poaname/objectid.

The naming service method has been briefly introduced in the previous blog "Java build a simple CORBA application". This article mainly introduces the IOR method. The ior structure supports two kinds of IOR: the ior that changes each time. By default, CORBA uses the IOR that changes every time. That is, after the server-side CORBA program is started, because the port number used by the process is not fixed, the IOR content changes with the port number every time, relatively simple. Unlike the IOR that changes every time, in many cases, the Application Scenario requires the IOR to remain unchanged, that is, the process Number of the application on the CORBA server is fixed, in this way, the persistent PoA generation policy must be used.

Thanks very much for the small example given in this article of http://blog.csdn.net/njchenyi/archive/2008/10/16/3086559.aspx, but because the example is not very clear, in addition, the source code is not complete, so in the practical work is difficult, next, I will use an experiment program I made myself as an example to explain the implementation of a fixed ior.

1. Write a simplest IDL file named hello. IDL. The Code is as follows:

View plain
  1. Module helloapp {
  2. Interface Hello {
  3. String sayhello ();
  4. Oneway void Shutdown ();
  5. };
  6. };

2. Compile this IDL file into a corresponding Java file. This is done through the idlj tool that comes with JDK. This tool is in the same directory as javac. The command format is as follows:
Idlj hello. IDL

We will find that the new directory helloapp contains six Java source files, which are described as follows:

Hellopoa PoA refers to the Portable Object Adapter (lightweight Object Adapter ). This abstract class is a stream-based server-side skeleton that provides basic server-side CORBA functions.

_ Hellosub client's Stub class, providing the client with the CORBA function.

Hello, this is the hello interface of Java version. It provides the standard functions of CORBA objects.

Hellohelper is a helper class that is used to write or read objects to the CORBA stream.

Helloholder is a final class that holds a public Hello instance variable. It is used to operate the parameters of the input and output streams of CORBA.

The hellooperations class is the interface we expected. It only contains the method we defined and does not contain anything about CORBA.

3. Provide a server-side object. Note that this implementation class inherits hellopoa:

View plain
  1. Package com. CORBA;
  2. Import org. Omg. CORBA. Orb;
  3. Import helloapp. hellopoa;
  4. Public class helloimpl extends hellopoa {
  5. Private Orb orb;
  6. Public void setorb (Orb orb ){
  7. This. Orb = ORB;
  8. }
  9. Public String sayhello (){
  10. Return "/n Hello world! /N ";
  11. }
  12. Public void Shutdown (){
  13. Orb. Shutdown (false );
  14. }
  15. }

4. server programs:

Because jacorb is used as the implementation scheme of CORBA, You need to download the jacorb2.3.zip file, decompress it, and unzip the slf4j-api-1.5.6.jar, slf4j-jdk14-1.5.6.jar and jacorb of its lib directory. jar is added to the classpath of the program. The server-side program code is as follows:

View plain
  1. Package com. CORBA;
  2. Import java. util. properties;
  3. Import org. Omg. CORBA. Orb;
  4. Import org. Omg. portableserver. idassignmentpolicyvalue;
  5. Import org. Omg. portableserver. lifespanpolicyvalue;
  6. Import org. Omg. portableserver. PoA;
  7. Import org. Omg. portableserver. poahelper;
  8. Import org. Omg. portableserver. servantretentionpolicyvalue;
  9. Public class helloserver {
  10. // Persistent PoA
  11. Private Static PoA persistentpoa = NULL;
  12. Public static void main (string [] ARGs ){
  13. // Generate an Object Request proxy (ORB) and initialize
  14. Properties props = new properties ();
  15. // Use jacorb's CORBA Implementation Scheme
  16. Props. Put ("org. Omg. CORBA. orbclass", "org. jacorb. Orb. Orb ");
  17. Props. Put ("org. Omg. CORBA. orbsingletonclass", "org. jacorb. Orb. orbsingleton ");
  18. // When persistence ior is used, you must specify the implementation name of persistence PoA. The default value is "standardimplname ",
  19. // You can specify
  20. Props. Put ("jacorb. implname", "standardimplname ");
  21. // Here, the fixed port is specified for the client port of the CORBA server, which is the key to the fixed ior of the CORBA server.
  22. Props. Put ("oaport", "12500 ");
  23. // If you need to specify the IP address of the server, you need to use the following method. The above method is used by default. // you only need to specify the port. The IP address is the IP address of the machine where the server program is located, note that either of the two methods can be selected
  24. // Props. Put ("oaaddress", "IIOP: // 10.140.1.97: 10501 ");
  25. Try {
  26. // Create an orb instance
  27. Orb orb = orb. INIT (ARGs, props );
  28. // Get a rootpoa reference
  29. POA rootpoa = poahelper. Narrow (ORB. resolve_initial_references ("rootpoa "));
  30. // Specify the policy for creating persistent PoA. To use persistent PoA, you must specify the following three policies:
  31. Org. Omg. CORBA. Policy [] policies = new org. Omg. CORBA. Policy [3];
  32. // The Life Cycle of Poa is persistent.
  33. Policies [0] = rootpoa. create_lifespan_policy (lifespanpolicyvalue. Persistent );
  34. // The identifier of the CORBA object is specified by the user.
  35. Policies [1] = rootpoa. create_id_assignment_policy (idassignmentpolicyvalue. user_id );
  36. // Every time a request is received, Poa expects the application to provide the target object identifier
  37. // Search for the index of the servo Program
  38. Policies [2] = rootpoa. create_servant_retention_policy (servantretentionpolicyvalue. Retain );
  39. // Create a persistent PoA
  40. Persistentpoa = rootpoa. create_poa ("persistentpoa", rootpoa. the_poamanager (), policies );
  41. // Clear the policy
  42. Policies [0]. Destroy ();
  43. Categories [1]. Destroy ();
  44. Categories [2]. Destroy ();
  45. Policies = NULL;
  46. // Create a servo program and register it on ORB
  47. Helloimpl = new helloimpl ();
  48. Helloimpl. setorb (ORB );
  49. // Create the servo program identifier because idassignmentpolicyvalue. user_id is used
  50. // Policy, all must specify the servo program id
  51. Byte [] servantid = "myhelloimpl". getbytes ();
  52. // Associate the servo program identifier with the server-side CORBA object and activate it
  53. Persistentpoa. activate_object_with_id (servantid, helloimpl );
  54. // Activate poamanager
  55. Rootpoa. the_poamanager (). Activate ();
  56. // Obtain the CORBA object through persistent PoA
  57. Org. Omg. CORBA. Object ref = persistentpoa. servant_to_reference (helloimpl );
  58. // Print the ior of the CORBA object
  59. System. Out. println ("CORBA ior is:" + orb. object_to_string (REF ));
  60. System. Out. println ("helloserver ready and waiting ...");
  61. // Start the thread service and wait for the call
  62. Orb. Run ();
  63. } Catch (exception e ){
  64. System. Out. println ("helloserver occur error:" + E );
  65. E. printstacktrace (system. Out );
  66. }
  67. System. Out. println ("helloserver exiting ...");
  68. }
  69. }

This small application can directly run the server program without a client program. After running, it records the IOR string output, ends the server program, and runs several more times, comparing the generated ior string, you will find that the IOR is generated the same every time. If no port or policy is specified, the generated ior is different each time, this is because different ports are used each time.

5. Client Program:

In order to make the application look more complete, and to demonstrate the simple process for the client to call the server, the following is a simple client program:

View plain
  1. Package com. CORBA;
  2. Import org. Omg. CORBA. Orb;
  3. Import helloapp. Hello;
  4. Import helloapp. hellohelper;
  5. Public class helloclient {
  6. Static hello;
  7. Public static void main (string [] ARGs ){
  8. Try {
  9. // Create an orb instance
  10. Orb orb = orb. INIT (ARGs, null );
  11. // Obtain the target object directly through the IOR. the IOR is generated by the server (the first time you implement it, You Need To // copy the IOR generated by the server ), because it is a fixed ior, it can be hard-coded and written here.
  12. Hello = hellohelper. Narrow (ORB. string_to_object ("IOR: Rule "));
  13. // Call the interface object Method
  14. System. Out. println ("Get Hello object from CORBA server:" + Hello );
  15. System. Out. println (hello. sayhello ());
  16. // Disable the CORBA service
  17. Hello. Shutdown ();
  18. } Catch (exception e ){
  19. System. Out. println ("helloclient occur error:" + E );
  20. E. printstacktrace (system. Out );
  21. }
  22. }
  23. }

Start the server program, then start the client program, and the client program prints "Hello World! After that, the CORBA service will be closed, and the server program will end up running. Because of the fixed ior, the server program and client program can run normally repeatedly without any changes.

 

Note: When testing a persistent ior, you cannot simply compare whether the generated ior string is the same, as long as the host, port, and object key in the IOR are the same, in addition to the above content, the IOR also has some filling fields, so it is possible that the persistence ior string is different each time. The best test method is no matter how many times the service is restarted, the same ior can be used to successfully call the server's servo program.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.