博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Discuss about PortableRemoteObject.narrow()
阅读量:6818 次
发布时间:2019-06-26

本文共 3748 字,大约阅读时间需要 12 分钟。

Disucss List
 
Q:
 
When using remote references (stubs) to EJB's they must be narrowed in the following mannor:
InitialContext ctx = 
new InitialContext(); 

Object obj = ctx.lookup(SomeObject); 

SpecificObject so = (SpecificObject) PortableRemoteObject.narrow(obj, SpecificObject.
class); 

 
However, do you also have to use PortableRemoteObject.narrow() when obtaining a datasource, or some other kind of object?
Thanks in advance!
 
A1:
For database, you have to search database JNDI name and cast it to DataSource.
Datasource ds = (Datasource)ctx.lookup("myDB");
 
The narrowing is from the CORBA world and got introduced in J2EE when they decided to adopt IIOP as the protocol for doing RMI. Consequently, you have to narrow only when you are dealing with IIOP, such as for remote bean handles. 
DataSource objects are usually not accessed that way (intra-VM instead) and hence don't need narrowing.
 
Q2:
Just out of interest how are Datasources handled since to retrieve a datasource the same syntax is used ie.
InitialContext ctx = new InitialContext();
ctx.lookup("Datasource");
I'm assuming they don't use RMI? 
A2:
Anything you lookup in JNDI is handled by a class-specific ObjectFactory that (loosely speaking) converts the name into an object of the class you lookup. What is actually bound inside JNDI is not really the Object that you get on lookup, but rather some reference data containing information about what class the object belongs to, and how to construct an 'equivalent' instance on lookup. Such an equivalent Object is the real result of the lookup.
So technically, the casting is all you need (like for the DataSource): it tells both the compiler and the runtime VM that the name you lookup is expected to be of the class you cast to. The JNDI and the class-specific ObjectFactory do the rest.
For pure Java, this mechanism is sufficient in most if not all cases.
For IIOP/CORBA (and dito bean handles), the case is more complex because the underlying CORBA runtime also needs a special treatment that can't be done by casting and the reference information alone. That is why you have to do the explicit call to narrow. It's really a special case as far as JNDI is concerned.
 
A3:
generally everything you obtain from "java:" namespace does not need narrowing, since it is VM-local.
However, you might find links to remote ressources (typically under java:comp/env/), so it might be better you always narrow, unless you know you deal with VM local objects (like DataSources). I have not yet found a case where narrow hurts.
Another Q:
this code is not working for me..
can anyone help me where i have did mistake.
pubserhome is the home for session bean and pubserremote is the remote interface for session bean.
   Properties properties = new Properties();
 properties.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
properties.put(Context.PROVIDER_URL, "localhost:1099");
try{
          Context jndiContext = new InitialContext(properties);
          Object ref=jndiContext.lookup("pubserremote"); 
          pubserhome home =(pubserhome) PortableRemoteObject.narrow( ref, pubserhome.class);
          pubserremote searchbean=home.create();
in the above code i get the error as 
incompatible types..
found:publisher.src.ejb.pubserhome
required:publisher.src.ejb.pubserremote
pubserhome searchbean=home.create();
                           ^
and also in this line 
  pubserhome home =(pubserhome) PortableRemoteObject.narrow( ref, pubserhome.class);
thanks in advance 
A:
pubserhome searchbean=home.create();
the above stmt is giving an error coz home.create() returns an object of remote type.
that is why u r getting this message:
found:publisher.src.ejb.pubserhome
required:publisher.src.ejb.pubserremote 
    本文转自danni505 51CTO博客,原文链接:http://blog.51cto.com/danni505/168739,如需转载请自行联系原作者
你可能感兴趣的文章
用motion实现家庭视频监控
查看>>
帝国cms缩略图:网站不同地方生成不同的缩略图
查看>>
python Django Ajax基础
查看>>
aop point-cut表达式
查看>>
easyui的 getSelections 与 getSelected 对比区别
查看>>
后缀数组模板 UOJ#35. 后缀排序
查看>>
[转]DirectX Rendering Pipeline渲染管线图
查看>>
ImageMaigck不支持中文路径的问题
查看>>
俄罗斯方块
查看>>
ZOJ 2061 - Buy the Ticket
查看>>
27.将 VMware 服务器上的虚拟机备份到 Azure(上)
查看>>
【cocos2d-x从c++到js】22:使用非侵入方式扩展UI系统接口的举例
查看>>
Hibernate查询效率对比
查看>>
DROP TABLE 恢复【一】
查看>>
Message Flood(map)
查看>>
百度地图计算两坐标点之间距离计算
查看>>
getHibernateTemplate()
查看>>
【SPOJ】10628. Count on a tree(lca+主席树+dfs序)
查看>>
Uva10290 - {Sum+=i++} to Reach N
查看>>
本地域名解析
查看>>