The first example of CORBA

zhaozj2021-02-16  178

The first example of CORBA, Visibroker implementation

1. Write the interface bank.idl

Module Bank {

Interface account {¢

Float balance ();

}

Interface AccountManager {

Account Open (in string name);

}

}

2, compile Bank.IDL with idl2java command

Command: idl2java bank.idl

Command: javac -classpath c: /bdp/lib/vbjorb.jar * .java

Generate a Bank folder and related java files, then compile these files, C: / BDP is the installation path of Visibroker.

3, realize server.java

Import Org.omg.PortablesRver. *;

Import bank. *;

Import java.util. *;

Public class server {

Public static void main (String [] args) {

Try {

Properties Properties = New Properties ();

Properties.SetProperty ("Vbroker.Addr", "LocalHost");

Properties.SetProperty ("Vbroker.Agent.port", "14000");

Properties.Put ("Org.omg.Corba.orbclass", "com.inprise.vbroker.orb.orb");

Properties.put ("Org.omg.Corba.orbsingletonclass", "com.inprise.vbroker.orb.orbsingleton");

Properties.Put ("javax.rmi.corba.stubclass", "com.inprise.vbroker.rmi.corba.stubimpl");

Properties.Put ("javax.rmi.corba.utilclass", "com.inprise.vbroker.rmi.corba.utilimpl");

Properties.put ("javax.rmi.corba.portableremoteObjectclass", "com.inprise.vbroker.rmi.corba.portableremoteObjectImpl"); ?????

Org.omg.corba.orb orb = org.omg.corba.orb.init (ARGS, Properties);

Poa rootpoa = poahelper.narrow (orb.resolve_initial_reference));

Org.omg.corba.policy [] policies = {

Rootpoa.create_lifespan_policy (LifeSpanPolicyValue.Persister)

}

Poa mypoa = rootpoa.create_poa ("Bank_Agent_poa", Rootpoa.The_Poamanager (),

POLICIES);

AccountManagerImpl ManagerSerVant = New AccountManagerImpl ();

Byte [] ManagerId = "BankManager" .GetBytes ();

Rootpoa.the_poamanager (). Activate (); system.out.println (Mypoa.Servant_to_Reference (ManagerServant)

"is ready.");

Orb.run ();

}

Catch (Exception E) {

E.PrintStackTrace ();

}

}

}

4, realize client.java

Import bank. *;

Import java.util. *;

Public class client {

Public static void main (String [] args) {

Properties Properties = New Properties ();

Properties.SetProperty ("Vbroker.Addr", "LocalHost");

Properties.SetProperty ("Vbroker.Agent.port", "14000");

Org.omg.corba.orb orb = org.omg.corba.orb.init (ARGS, Properties);

Byte [] ManagerId = "BankManager" .GetBytes ();

Bank.accountManager Manager =

Bank.accountManagerHelper.bind (ORB, "/ Bank_Agent_poa", ManagerID);

String name = args.length> 0? Args [0]: "Tom";

Bank.account account = manager.open (name);

Float Balance = Account.balance ();

System.out.println ("The Balance in" Name "'S Account IS $" Balance);

}

}

5, realize AccountImpl

Import bank. *;

Public Class AccountImpl Extends Bank.accountpoa {

Public AccountIMPL (Float Balance) {

_balance = balance;

}

Public float balance () {

Return_balance;

}

Private float _balance;

}

6. Implement AccountManagerImpl

Import Org.omg.PortablesRver. *;

Import bank. *;

Import java.util. *;

Public class accountmanagerImpl Extends Bank.accountManagerpoa {

Public synchronized bank.account open (string name) {

Bank.account Account = (Bank.account) _Accounts.get (name);

IF (Account == NULL) {

Float balance = math.abs (_random.nextint ())% 100000 /

100F

;

AccountImpl AccountServant = New AccountImpl (Balance);

Try {

Account = Bank.accountHelper.narrow (_default_poa (). servant_to_reference);} catch (Exception E) {

E.PrintStackTrace ();

}

System.out.println ("Created" Name "'S Account:" Account);

_Accounts.put (name, account);

}

Return Accent;

}

Private Dictionary _accounts = new hashtable ();

Private random _random = new random ();

}

7, compile server and client

Compile all files with javac * .java command to generate a Class.

Command: javac -classpath c: /bdp/lib/vbjorb.jar ;. * .java

8, run SmartAgent

Command: OSAgent

9, run the server

Command: VBJ Server or Start VBJ Server, Start is used to open a new window

10, run the client

Command: VBJ Client

Run results: The Balance In Tom's Account IS $ 992.8, get it.

转载请注明原文地址:https://www.9cbs.com/read-8829.html

New Post(0)