Discuss about PortableRemoteObject.narrow()

简介:
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,如需转载请自行联系原作者



相关文章
|
3天前
|
人工智能 运维 安全
|
1天前
|
人工智能 异构计算
敬请锁定《C位面对面》,洞察通用计算如何在AI时代持续赋能企业创新,助力业务发展!
敬请锁定《C位面对面》,洞察通用计算如何在AI时代持续赋能企业创新,助力业务发展!
|
9天前
|
人工智能 JavaScript 测试技术
Qwen3-Coder入门教程|10分钟搞定安装配置
Qwen3-Coder 挑战赛简介:无论你是编程小白还是办公达人,都能通过本教程快速上手 Qwen-Code CLI,利用 AI 轻松实现代码编写、文档处理等任务。内容涵盖 API 配置、CLI 安装及多种实用案例,助你提升效率,体验智能编码的乐趣。
795 109
|
3天前
|
机器学习/深度学习 人工智能 自然语言处理
B站开源IndexTTS2,用极致表现力颠覆听觉体验
在语音合成技术不断演进的背景下,早期版本的IndexTTS虽然在多场景应用中展现出良好的表现,但在情感表达的细腻度与时长控制的精准性方面仍存在提升空间。为了解决这些问题,并进一步推动零样本语音合成在实际场景中的落地能力,B站语音团队对模型架构与训练策略进行了深度优化,推出了全新一代语音合成模型——IndexTTS2 。
362 9
|
2天前
|
人工智能 测试技术 API
智能体(AI Agent)搭建全攻略:从概念到实践的终极指南
在人工智能浪潮中,智能体(AI Agent)正成为变革性技术。它们具备自主决策、环境感知、任务执行等能力,广泛应用于日常任务与商业流程。本文详解智能体概念、架构及七步搭建指南,助你打造专属智能体,迎接智能自动化新时代。
|
3天前
|
机器学习/深度学习 传感器 算法
Edge Impulse:面向微型机器学习的MLOps平台——论文解读
Edge Impulse 是一个面向微型机器学习(TinyML)的云端MLOps平台,致力于解决嵌入式与边缘设备上机器学习开发的碎片化与异构性难题。它提供端到端工具链,涵盖数据采集、信号处理、模型训练、优化压缩及部署全流程,支持资源受限设备的高效AI实现。平台集成AutoML、量化压缩与跨硬件编译技术,显著提升开发效率与模型性能,广泛应用于物联网、可穿戴设备与边缘智能场景。
184 127