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. |