在分布式应用中,单个业务应用的toplink运行在多个jvm上,同时开启了会话缓存,而这些在多个jvm上会话缓存中的domain对象就需要同步,这样可以及时拿到最新的信息而不需要每次都查询database,这大大减少了乐观锁冲突。在Oracle Application Server上Ormi端口是动态分配的,如果一个应用跑在多个jvm上,这些jvm的ormi服务端口也是不同的,所以在开发阶段无法确定这些信息,也不好采用多套toplink session的配置。庆幸的是opmn的request端口是固定的,我们可以使用opmn request端口来进行jndi的look up,然后用oc4j jms实现多jvm或者多OC4J instance之间toplink session缓存的同步, 因为经过测试OC4J in memory jms作为同步消息的传输和发现方式效率是最高的。
具体方法如下:
- 配置一个拥有3个jvm的oc4j instance
2 toplink session cache 策略的配置
<toplink:caching>
<toplink:cache-invalidation-policy xsi:type="toplink:time-to-live-cache-invalidation-policy">
<toplink:time-to-live>36000000</toplink:time-to-live>
</toplink:cache-invalidation-policy>
</toplink:caching>
3.配置JMS topic connection factory和topic
具体每个配置参数什么意思,可参见Oracle Containers for J2EE Services Guide
具体每个配置参数代表什么意思,可参见Oracle Containers for J2EE Services Guide
创建完毕后,在instance level的config目录下面打开jms.xml可看见所作的配置更改:
<topic
name="cachesyn"
location="jms/cachesyn"
>
</topic>
<!-- unified connection factories -->
<!-- queue connection factories -->
<!-- topic connection factories -->
<topic-connection-factory
location="TopicConnectionFactory"
/>
4 opmn配置
查看opmn.xml,获得request端口
<port local="6101" remote="6201" request="6004"/>
|
Opmn的url格式为:opmn:ormi://主机名:opmn request端口:OC4J Instance Name /Application Name
5.配置toplink会话缓存同步策略
6.权限设置
我们登录console的server的全局用户,并不一定有instance level的rmi等权限,所以不能实现jndi的lookup. Oc4j默认采用的是基于文件的认证和授权管理策略,用的是JAAS,通常称为:jazn,Oracle's authorization/policy provider for JAAS
打开全局的j2ee/config/ system-jazn-data.xml文件,元素<jazn-policy>有下面这一段,policy主要就是为角色和用户分配具体权限的
<grantee>
<principals>
<principal>
<realm-name>jazn.com</realm-name>
<type>role</type>
<class>oracle.security.jazn.spi.xml.XMLRealmRole</class>
<name>jazn.com/oc4j-administrators</name>
</principal>
</principals>
</grantee>
只有oc4j-administrators角色才有这些权限。而system team分配给我们的用户并不拥有该角色,在console界面上也没找到为server level的用户也不能进行oc4j-administrators角色的交易,所以也没jndi lookup的权限,会报javax.naming.AuthenticationException: Not authorized.
需要在instance level 创建一个用户,用户名和密码和server level的相同或者也可以不同,然后为该用户分配权限,查看了一下instance level的system-jazn-data.xml发现jazn.com/ascontrol_admin和jazn.com/oc4j-administrators角色有完整rmi权限,内容如下:
<permission>
<class>com.evermind.server.rmi.RMIPermission</class>
<name>login</name>
</permission>
<permission>
<class>com.evermind.server.rmi.RMIPermission</class>
<name>subject.propagation</name>
</permission>
最简单的方法就是为该用户分配jazn.com/ascontrol_admin角色,jazn.com可以省略因为jazn.com是默认域,oc4j采用的是域,用户,角色,权限的层次授权策略。
当然我们也可以在applciation level做到最小权限控制,在orion-application和applciation level的jazn-data.xml中进行设置和分配。具体方法可参见
Oracle® Containers for J2EE Security Guide
http://download-east.oracle.com/docs/cd/B25221_04/web.1013/b14429/toc.htm
7.Deploy 应用到application server
这下可以享受toplink cluster带来的高性能的乐趣了。
如果成功的话:我们可以看到oc4j_instance jvm1的toplink有如下的log:
[TopLink Finest]: 2008.12.03 06:58:30.961--ServerSession(24138346)--Thread(Thread[RMICallHandler-5,5,HTTPThreadGroup])--Retreived remote message from JMS topic: cachesyn
[TopLink Finest]: 2008.12.03 06:58:31.020--ServerSession(24138346)--Thread(Thread[RMICallHandler-5,5,HTTPThreadGroup])--Received remote command oracle.toplink.remotecommand.MergeChangeSetCommand from Service[TopLinkCommandChannel, 3673206, opmn:ormi://localhost:6004:OC4J_TTS/tts]
[TopLink Finest]: 2008.12.03 06:58:31.023--ServerSession(24138346)--Thread(Thread[RMICallHandler-5,5,HTTPThreadGroup])--Executing command oracle.toplink.remotecommand.MergeChangeSetCommand from Service[TopLinkCommandChannel, 3673206, opmn:ormi://localhost:6004:OC4J_TTS/tts]
[TopLink Finer]: 2008.12.03 06:58:31.098--ServerSession(24138346)--Thread(Thread[RMICallHandler-5,5,HTTPThreadGroup])--Received updates from Remote Server
[TopLink Finest]: 2008.12.03 06:58:31.110--ServerSession(24138346)--Thread(Thread[RMICallHandler-5,5,HTTPThreadGroup])--Merging com.ldd600.isdc.sha.frm.tts.model.Employee: [22] from remote server
Oc4j_instance jvm2的toplink有如下的log:
ndler-5,5,HTTPThreadGroup])--Retreived remote message from JMS topic: cachesyn
[TopLink Finest]: 2008.12.03 06:58:30.721--ServerSession(8762565)--Thread(Thread[RMICallHandler-5,5,HTTPThreadGroup])--Received remote command oracle.toplink.remotecommand.MergeChangeSetCommand from Service[TopLinkCommandChannel, 3673206, opmn:ormi://localhost:6004:OC4J_TTS/tts]
[TopLink Finest]: 2008.12.03 06:58:30.724--ServerSession(8762565)--Thread(Thread[RMICallHandler-5,5,HTTPThreadGroup])--Executing command oracle.toplink.remotecommand.MergeChangeSetCommand from Service[TopLinkCommandChannel, 3673206, opmn:ormi://localhost:6004:OC4J_TTS/tts]
[TopLink Finer]: 2008.12.03 06:58:30.734--ServerSession(8762565)--Thread(Thread[RMICallHandler-5,5,HTTPThreadGroup])--Received updates from Remote Server
[TopLink Finest]: 2008.12.03 06:58:30.736--ServerSession(8762565)--Thread(Thread[RMICallHandler-5,5,HTTPThreadGroup])--Merging com.ldd600.isdc.sha.frm.tts.model.Employee: [22] from remote server