Use POA programming
Creating and using the POA method will differ depending on the type of the program to develop programs. A typical POA program has the following steps:
1. Get POA root elements
First get the POA root element, which is managed by ORB, using the initial object name "rootpoa" to get it and supplies the program. The method of operation is as follows:
ORB ORB = Orb.init (Argv, Null);
Poa rootpoa = poahelper.narrow (orb.resolve_initial_reference));
2. Define the PoA policy
As mentioned earlier, POA is an object adapter that can be used between multiple ORB products without rewriting the code. It is also designed for permanent objects (which can continue to exist even after storing their servers). Developers need to be able to control parameters such as identity, status, storage, and lifecycle of objects. These are related to the content of the POA policy, the content of the POA policy is related to the thread, life period, object uniqueness, activation, etc.
There are seven strategies that can be set for developers. They are:
N thread policy
To select a suitable thread processing technology for a program, you need to consider many factors, such as the number of objects to be used in the program, the operating system supports multi-threaded extent, expected system load, etc. BOA does not support multithreading, but this problem is solved in POA, which specifies the thread model to be used by the thread policy. you can use:
n orb_ctrl_model: This model allows multiple requests to be handled simultaneously with multi-threads, and the ORB is responsible for allocating requests to threads (system default settings).
n SINGLE_THREAD_MODEL: This model does not use threads, all requests will be processed (but in J2SE1.4, this model is not implemented).
Create this policy as follows:
Policy p [] = new policy [7];
p [0] = rootpoa.createt_thread_policy (threadpolicyvalue.orb_ctrl_model);
In this example we have generated an array containing all seven policy contents. In the first element we created a thread policy. It is important to note that the thread policies we create are still the system default. This is just an example of how to create a strategy.
n LifeSpan Policy
POA supports two types of CORBA objects: CORBA uses a permanent object by default, and another new object is a temporary object. The temporary object leaves the process of creating its, can no longer exist again; it can be employed when some cases that require temporary objects (such as program callbacks). In contrast, the permanent object can continue to survive even if you leave the process of creating it. For example, if the client sends a request to a target object that is not started (or not run), the ORB will start this service process (if needed) at the same time. This activation process is transparent to the customer.
Life strategies can be used to specify object types that will create POA. You can create an object:
n Transient: This object leaves the POA instance that creates them, can no longer continue (default setting).
n Persistent: This object can continue to exist even if you leave the process of creating their.
The following code explains how to create a permanent life strategy:
P [1] = rootpoa.create_lifesis_policy (lifeSpanpolicyValue.Persistent);
Just as you can see from the two examples of the above, you can define a policy using a function similar to the CREATE_NAMEOF_POLICY (PolicNameValue.Value) format. N Object ID Uniqueness Policy
This policy is used to specify whether the server is only a unique object identifier. The value of this policy can be:
n UNIQUE_ID: The server only supports a clear object ID (default setting).
n multiple_id: One server can support one or more object IDs.
The following code shows how to make one or more object IDs to one or more objects:
P [2] = rootpoa.create_id_uniqueason_policy (
IDunique.multiple_id);
N id assignment policy
This policy is used to specify the object ID is generated by the application or ORB. The options are:
n User_id: You can only specify a unique ID by the application to the object.
n system_id: Assign a unique ID (default setting) by POA. Note that if the life policy is set to Persistent, the object ID assigned in all instance objects of the same POA must be unique.
The following code shows how to create an ID assignment policy that specifies the object ID generated by the application:
P [3] = rootpoa.create_id_assignment_policy (iDassignmentPolicyValue.user_ID);
N Servant Retention Policy
This policy specifies whether you want to let the created POA retain the active server in an object map. The options are:
n retain: Specifies the POA to retain the active object in the mapping (default setting).
n Non_retain: Do not retain the activity object.
The following code shows how to use the server maintenance policy to specify that POA is not in an object map:
p [4] = rootpoa.create_servant_retention_policy (
ServantRextionPolicyValue.non_retain;
n Request Processing Policy
This policy is used to specify how POA handles client requests. The options are as follows:
n Use_active_object_map_only: If the object ID is not found in the active object map, then Object_not_exist (default setting) will be thrown. But pay attention to, you must use this setting you must set the value of Servant Retention Policy to Retain.
n Use_default_servant: If the object ID is not found in the active object map, or the server maintenance policy current setting is Non_Retain, the request will be assigned to the default server.
n Use_servant_manager: If the object ID is not found in the active object map, or the server maintenance policy is currently set to Non_Retain, then the server manager is trying to determine or activate a server, otherwise the exception is thrown.
The following code shows how to create a request processing policy specifies assigning the request to the default server:
p [5] = create_request_processing_policy
RequestProcessingPolicyValue.Use_default_servant);
n Implicit Activation Policy This policy specifies whether the server in POA is allowed to be automatically activated. The options are as follows (no default settings):
n Implicit_Activation: Automatically activate the server. But pay attention to this requires settings System_ID and Retain policies.
N no_implicit_activation: The server will not start since start.
Root POA is always using the following strategy:
N thread policy: ORB_CTRL_MODEL.
MINY: TRANSIENT.
n Object id uniqueness: unique_id.
N id assignment policy: system_id.
N Servant Retention Policy: retain.
N Request Processing Policy: USE_ACTIVE_OBJECT_MAP_ONLY.
n Implicit Activation Policy: Implicit_ActiVation.
3. New POA
Create a new POA and apply the strategy you just defined. Use the Create_poa function on the existing POA to create a new POA, which will serve as the child POA's child node. Creating a new PoA you need to define the following:
1. Name this POA, the name must be unique in all POA elements of the same parent node.
2.poA Manager will be associated with the new POA. If the setting parameter is null, you will not create a new POA manager.
3. The new POA must be associated with a defined policy array.
The following program shows how to build POA:
Poa Poa = Rootpoa.create_poa ("Childpoa", Null, Policy;
4. Start Poamanager
A PoAManager can be associated with one or more PoA objects. It is responsible for controlling the processing method of POA. Poamanager can be one of the following:
n Holding: Related POA Receive requests and discharge in queue form.
n Active: Related POA Start Processing Request.
N inactive: Related POA Refuse to receive a new request and stop processing that has not been executed.
N Discarding: Related POA Discard New Request.
When a Poamanager object is created, the default is the Holding state. This means it does not start automatically. Developers are required to activate as follows:
Poa.the_poamanager (). Activate ();
If there is no step, PoAmanager will always keep the Holding state, then all requests for the server will be suspended in a queue.
5. Start the server
If you set the USE_DEFAULT_SERVANT policy, the server program requests POA to launch an unknown object, then a separate server will call a separate server regardless of what the object ID is. Server programs are registered with SET_SERVANT.
In addition, if the RetAin policy, the server, and the object ID associated with it will be stored in the current object map corresponding to POA. To activate this object, you can use one of the following three methods:
1. The server program can activate the independent object directly using the Activate_Object or Activate_Object_With_ID method.
2. The server program enables the POA to activate the object when receiving the request by allowing the POA to call a user-set server management. Can be associated with the SET_SERVANT_MANAGER function registration.
3. If the Implicit_ActiVation policy is also set, the POA will automatically activate the object when the server program wants to get a reference to the unconnected server. However, if the Non_Retain policy is set, POA will use the default server or server manager to find an activated server. But note that the server is only active within the request time in POA. POA does not associate the customer object to store the active object mapping.
6. Create an object reference
When you create an object reference in the server, you must associate it with the client. Object references information containing ORBs such as object identifiers to identify and locate servers and POA related to the object. Object references can be created in the following three ways:
1. Directly activate the server and associate it with the object reference. The following code demonstrates how to map a active server to an associated object reference: through the servant_to_reference function:
Org.omg.corba.object obj = orb.resolve_initial_references ("Nameservice");
NamingContexText Rootctx = NamingContextextHelper.narrow (OBJ);
NameComponent NC [] = rootctx.to_name ("PersistentMathServer);
Rootctx.rebind (nc, poa.servant_to_reference);
2. The server program directly generates a reference. The following code demonstrates how to do:
Context ctx = new initialContext ();
CTX.Rebind ("MathServer", poa.create_reference_with_id (ID, TIE._ALL_INTERFACES (POA, ID) [0]));
3.
Specifies the server program to automatically activate the server. This is only used
IMPLICIT_ACTIVATION
Strategy, in
Poa
The occurrence will occur when you created.