上篇文章講述了jacorb 的配置,本章講敘其應用,隨后的幾篇文章會重點講述corba的開發
JACORB開發以及corba的請求方式:
JacORB的應用開發一般分為以下五步:
1.寫IDL接口定義
2.編譯IDL接口定義生成Java類
3.實現步驟2中生成的接口
4.寫服務器啟動類,并注冊到ORB
5.寫客戶端去獲取服務對象引用
本例子是采用JACORB里面的一個GRID的例子,從這里例子我們能看出CORBA開發的一些基本步驟,猶豫本例子采用的都是JAVA開發的,由于也受到JACORB的框架限制,并不能很好看出跨語言平臺以及如何做到分布式的開發,但是本例 有2種基本的方式能夠展現出來corba兩中 調用方式:
第一種方式:根據server生成的IDL以及IOR文件,交給客戶端,客戶端根據這IOR文件來定位server,然后根據IDL接口生成自己的調用代碼(本例中是JAVA,當然也可以自己用別的語言來實現)
第二種方式:根據Naming Service 的方式來定位server,有點類似于JNDI的方式,有個Naming Server ,server端把一些服務注冊上去,client從naming server來找這些服務.
以上是2種最基本的方式來實現 CORBA的這2種調用方式,第一種方式雖然麻煩一點,但是也是很有效的。本例子用JAVA實現的server和Client 充分的展示了這2種調用方式
1.寫IDL接口定義
全部Corba結果是從一個接口開始的,因為Corba與語言無關,所以它依靠一種接口定義語言來表達如何向實現接口的服務發出請求以及得到應答.
這是Jacorb里面Grid的例子,IDL的詳細語法可以去OMG找些相關的資料來看,比較簡單
grid.idl
// IDL defintion of a 2-D grid:
module demo
{
module grid
{
interface MyServer
{
typedef fixed <5,2> fixedT;
readonly attribute short height; // height of the grid
readonly attribute short width; // width of the grid
// set the element [n,m] of the grid, to value:
void set(in short n, in short m, in fixedT value);
// return element [n,m] of the grid:
fixedT get(in short n, in short m);
exception MyException
{
string why;
};
short opWithException() raises( MyException );
};
};
};
我們用這樣一個與任何語言無關的IDL文件將來生成一些與語言相關一些文件,這些文件里面定義了 包名,類名,方法名.
2.編譯IDL接口定義生成Java類
根據下圖就可以看出來生成的文件哪些是server的那些client的
3.實現步驟2中生成的接口
package demo.grid;
/**
* A very simple implementation of a 2-D grid
*/
import demo.grid.MyServerPackage.MyException;
public class gridImpl extends MyServerPOA
{
protected short height = 31;
protected short width = 14;
protected java.math.BigDecimal[][] mygrid;
public gridImpl()
{
mygrid = new java.math.BigDecimal[height][width];
for( short h = 0; h < height; h++ )
{
for( short w = 0; w < width; w++ )
{
mygrid[h][w] = new java.math.BigDecimal("0.21");
}
}
}
public java.math.BigDecimal get(short n, short m)
{
if( ( n <= height ) && ( m <= width ) )
return mygrid[n][m];
11
else
return new java.math.BigDecimal("0.01");
}
public short height()
{
return height;
}
public void set(short n, short m, java.math.BigDecimal value)
{
if( ( n <= height ) && ( m <= width ) )
mygrid[n][m] = value;
}
public short width()
{
return width;
}
public short opWithException()
throws demo.grid.MyServerPackage.MyException
{
throw new demo.grid.MyServerPackage.MyException("This is only
a test exception,
}
}
4.寫服務器啟動類,并注冊到ORB
MyServer接口來訪問它。以下是這個類的源代碼:
package demo.grid;
import java.io.*;
import org.omg.CosNaming.*;
public class Server
{
////// public static void main( String[] args )
{
org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args, null);
try
{
org.omg.PortableServer.POA poa =
org.omg.PortableServer.POAHelper.narrow(
orb.resolve_initial_references("RootPOA"));
poa.the_POAManager().activate();
org.omg.CORBA.Object o = poa.servant_to_reference(new
gridImpl());
if( args.length == 1 )
{
// write the object reference to args[0]
PrintWriter ps = new PrintWriter(
new FileOutputStream(
new File( args[0] )));
ps.println( orb.object_to_string( o ) );
ps.close();
}
else
{
// register with the naming service
NamingContextExt nc =
NamingContextExtHelper.narrow(
orb.resolve_initial_references("NameService"));
nc.bind( nc.to_name("grid.example"), o);
12
}
}
catch ( Exception e )
{
e.printStackTrace();
}
orb.run();
}
}
5.寫客戶端去獲取服務對象引用
package demo.grid;
import org.omg.CosNaming.*;
public class Client
{
public static void main(String args[])
{
try
{
MyServer grid;
org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args,null);
if(args.length==1 )
{
// args[0] is an IOR-string
grid =
MyServerHelper.narrow(orb.string_to_object(args[0]));
}
else
{
NamingContextExt nc =
NamingContextExtHelper.narrow(
orb.resolve_initial_references("NameService"));
grid = MyServerHelper.narrow(
nc.resolve(nc.to_name("grid.example")));
}
short x = grid.height();
System.out.println("Height = " + x);
short y = grid.width();
System.out.println("Width = " + y);
x -= 1;
y -= 1;
13
System.out.println("Old value at (" + x + "," + y +"): " +
grid.get( x,y));
System.out.println("Setting (" + x + "," + y +") to 470.11");
grid.set( x, y, new java.math.BigDecimal("470.11"));
System.out.println("New value at (" + x + "," + y +"): " +
grid.get( x,y));
try
{
grid.opWithException();
}
catch (jacorb.demo.grid.MyServerPackage.MyException ex)
{
System.out.println("MyException, reason: " + ex.why);
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
JACORB的調用方式分為 2種方式
(一)IOR定位調用方式
首先我們展示的是IOR定位調用方式,需要server把生成的IOR文件交給client
首先運行server 把生成的ior文件保存到一個文件中:
jaco demo.grid.Server d://test/NS_Ref
打開d://test/NS_Ref可以看到如下內容:
IOR:000000000000001B49444C3A64656D6F2F677269642F4D795365727665723A312E300000000000020000000000000070000102000000000E6C6F6F636B7977616E672D30310012980000001C5374616E64617264496D706C4E616D652F00151F2C4C294C3C011139000000020000000000000008000000004A414300000000010000001C00000000000100010000000105010001000101090000000105010001000000010000002C0000000000000001000000010000001C00000000000100010000000105010001000101090000000105010001
|
其實這個內容我們可以用JACORB提供command可以反解析的
Dior –f d://test/NS_Ref
------IOR components-----
TypeId : IDL:demo/grid/MyServer:1.0
TAG_INTERNET_IOP Profiles:
Profile Id: IIOP Version : 1.2
Host : loockywang-01
Port : 4760
Object key (URL): StandardImplName/%00%15%1F,L)L%3C%01%119
Object key (hex): 0x53 74 61 6E 64 61 72 64 49 6D 70 6C 4E 61 6D 65 2
F 00 15 1F 2C 4C 29 4C 3C 01 11 39
-- Found 2 Tagged Components--
#0: TAG_ORB_TYPE
Type: 1245790976 (JacORB)
#1: TAG_CODE_SETS
ForChar native code set Id: ISO 8859-1
Char Conversion Code Sets: UTF-8
ForWChar native code set Id: UTF-16
WChar Conversion Code Sets: UTF-8
Components in MULTIPLE_COMPONENTS profile: 1
#0: TAG_CODE_SETS
ForChar native code set Id: ISO 8859-1
Char Conversion Code Sets: UTF-8
ForWChar native code set Id: UTF-16
WChar Conversion Code Sets: UTF-8
|
從這段內容里面我們可以看到HOST PORT等信息也就是說client有辦法定位到server的位置以及服務了
我IOR的內容 作為參數來運行 client程序
C:\Documents and Settings\jpwang>jaco demo.grid.Client IOR:000000000000001B49444
C3A64656D6F2F677269642F4D795365727665723A312E300000000000020000000000000070000102000000000E6C6F6F636B7977616E672D303100135C0000001B5374616E64617264496D706C4E616D652F00151F2D0C241517473600000000020000000000000008000000004A414300000000010000001C00000000000100010000000105010001000101090000000105010001000000010000002C00000
00000000001000000010000001C00000000000100010000000105010001000101090000000105010001
|
得到如下運行結果
Height = 31
Width = 14
Old value at (30,13): 0.21
Setting (30,13) to 470.11
New value at (30,13): 470.11
MyException, reason: This is only a test exception, no harm done :-)
done.
|
(二)Naming Service的調用方式
1:啟動ns
Naming service的調用一定要啟動naming server,在這里JACORB提供一個command ns
只要配置文件寫的都對,只需要啟動NS就可以了
>ns 回車
2:啟動server
jaco demo.grid.Server
3:監控你的namingserver 通過命令nmg

或者通過命令lsns 得到如下結果.
C:\Documents and Settings\jpwang>lsns
[ configuration jacorb loaded from file D:\work\nj\JACORB\etc\jacorb.properties
]
grid.example
|
4:運行client
C:\Documents and Settings\jpwang>jaco demo.grid.Client
得到如下結果
Height = 31
Width = 14
Old value at (30,13): 0.21
Setting (30,13) to 470.11
New value at (30,13): 470.11
MyException, reason: This is only a test exception, no harm done :-)
done.
|
posted on 2007-11-22 10:41
小小程序程序員混口飯吃 閱讀(8315)
評論(2) 編輯 收藏 所屬分類:
java