`
sbtmbj2014
  • 浏览: 26400 次
社区版块
存档分类
最新评论

What you may need to know while calling Application Module methods from Java EE components such as E

阅读更多

A couple of years back I blogged aboutCalling Application Module Methods from EJB and MDB. Recently I learned that when a non web client invokes methods defined in EJB or MDB that uses Application Module, it may results in some unexpectedClassCastException for DefLocaleContext. The error stack may look like as listed below.
Caused by: java.lang.ClassCastException: oracle.jbo.common.DefLocaleContext cannot be cast to oracle.jbo.common.DefLocaleContext at oracle.jbo.common.DefLocaleContext.getInstance(DefLocaleContext.java:84)

The reason is that the current request gets the ADFContext cached by the previous thread. As these threads may have different class loaders, casting class loaded by one loader to the otherresultin weird ClassCastException. This post contains a solution for such cases. Here you go...

1. If you are accessing EJB(such as a SessionBean) from a web UI, then configureServletADFFilterin web.xml to intercept all the requests that access EJB using ADF BC. The ServletADFFilter sets up
ServletADFContextproperly at the start of the request so that ADF BC can access the right context object during execution. If you use ADF binding, then this step is not required because the ADFBindingFilter configured in web.xml does the same job oninitializingADFContext.

<filter>

<filter-name>ServletADFFilter</filter-name>

<filter-class>oracle.adf.share.http.ServletADFFilter</filter-class>

</filter>

<filter-mapping>

<filter-name>ServletADFFilter</filter-name>

<url-pattern>/*</url-pattern>

<dispatcher>REQUEST</dispatcher>

<dispatcher>FORWARD</dispatcher>

</filter-mapping>


2. If you are invoking EJB in non web UI context, then you will not have the luxury of using Servlet filter for initializing the context. For example calling EJB methods from a desktop client or from somebatchprogram. In such cases you may need to manually initialize the ADF context. A possible solution is to define an interceptor class for the EJB session bean and add the ADFContext initialization code in the AroundInvoke method in theinterceptorclass.

An example of usinginterceptorwith EJB is here:
Define the interceptor as shown below:

publicclassADFContextInterceptor{

publicADFContextInterceptor(){

super();

}

@AroundInvoke

public Object manageADFContext(InvocationContext ctx)throws Exception {

ADFContext currentADFContext =null;

try{

System.out.println(" - manageADFContext - entry ");

currentADFContext =

ADFContext.initADFContext(null,null,null,null);

return ctx.proceed();

}finally{

System.out.println(" - manageADFContext - exit ");

ADFContext.resetADFContext(currentADFContext);

}

}

}

Now configure your Session Bean to use this interceptor as shown in the following example so that all methodinvocationswill be routed through your interceptor code.

@Stateless(name = "DemoSessionEJB",

mappedName = "EJBWithADFBCApp-EJBModel-DemoSessionEJB")

@Interceptors(value = ADFContextInterceptor.class)

publicclassDemoSessionEJBBeanimplements DemoSessionEJB,

DemoSessionEJBLocal {

//... implementation go here...

}


3. If the technology that you use do not support any interceptors around method invocation as discussed above, then you can try adding the ADFContext handling code before and after of your custom code that access AM method as listed below:

publicvoidsomeBusinessMethod()throws Exception {

ADFContext currentADFContext =null;

try{

currentADFContext =

ADFContext.initADFContext(null,null,null,null);

//Your code that access AM and doing business some

//actions go here

}finally{

ADFContext.resetADFContext(currentADFContext);

}

}


Note
In case if you see the following warning in the console while accessing application module from aclient bypassing ADF binding layer, the above solution will help you to get rid of it

WARNING: Automatically initializing a DefaultContext for getCurrent.
Caller should ensure that a DefaultContext is proper for this use.

Memory leaks and/or unexpected behaviour may occur if the automatic initialization is performed improperly.

Download


You can
download the sample workspace from here. [Runs with Oracle JDeveloper 11g R1 11.1.1.7.0 + Oracle XE].
See the EJBModel project which uses theinterceptorapproach discussed in this post in order to initialize ADFContext object.


A note of thanks

Many thanks to my colleague Ricky Frost (Oracle ADF Team) who pointed out a weird logical error in my code while resetting ADF context at the end of invocation. He mentioned that ADFContext.resetADFContext() should be called unconditionally at the end of the method. I corrected the same in my code without asking any questions as Ricky knows these things much better than me :)

分享到:
评论

相关推荐

    Calling Java Class Methods from C with JNI

    Calling Java Class Methods from C with JNI c调用java示例

    Calling R from Java

    Calling R from Java java调用R java call R

    Java邮件开发Fundamentals of the JavaMail API

    addition, you will need a development environment such as the JDK 1.1.6+ or the Java 2 Platform, Standard Edition (J2SE) 1.2.x or 1.3.x. A general familiarity with object-oriented programming ...

    Google C++ Style Guide(Google C++编程规范)高清PDF

    It is important to know that functions are not always inlined even if they are declared as such; for example, virtual and recursive functions are not normally inlined. Usually recursive functions ...

    Advanced Apple Debugging & Reverse Engineering v0.9.5

    In this chapter, you’re going to learn about methods and strategies to “hook” into Swift and C code as well as execute methods you wouldn’t normally have access to. 16. Exploring and Method ...

    微软内部资料-SQL性能优化2

    Although 2 GB of address space may seem like a large amount of memory, application such as SQL Server could leverage more memory if it were available. The boot.ini option /3GB was created for those ...

    J/Invoke 2010 jinvoke jni 天涯浪子

    J/Invoke enables Java developers to easily invoke native methods (such as the Win32 API or C-based Windows DLLs and Unix dynamic libraries) with pure Java code. Unlike error-prone JNI programming ...

    UE(官方下载)

    Did you know that you can not only change what is on UltraEdit's toolbars, you can also change the icon used, as well as create your own custom toolbars and tools? File tabs Understand how file tabs ...

    VclZip pro v3.10.1

    PLEASE TAKE A LOOK AT THE "WHAT's NEW IN THIS VERSION" LINK IN THE HELP FILE AS IT HAS CONVENIENT LINKS TO ALL OF THE NEW TOPICS. ==================== Version 3.10 Build 1 - Several bug fixes. - ...

    101 Windows Phone 7 Apps, Volume I: Developing Apps 1-50

    Everything you need to know about Silverlight Fully exploiting phone features such as the application bar, hardware/software keyboards, multi-touch, accelerometer, microphone, and more Using rich ...

    Introduction.to.Android.Application.Development(4th,2013.12) pdf

    Adding Logging Support to Your Android Application 83 Adding Some Media Support to Your Application 84 Adding Location-Based Services to Your Application 88 Debugging Your Application on Hardware 90 ...

    Calling C from IDL(2002)

    Calling C and C++ from IDL复印版,还挺清楚的。

    javacv-platform-1.3.3-src

    You may also find it useful to refer to the source code of ProCamCalib and ProCamTracker as well as examples ported from OpenCV2 Cookbook and the associated wiki pages. Please keep me informed of ...

    Go Cookbook 含代码

    Get to know tricks on treating data such as collections Handle errors and cleanly pass them along to calling functions Wrap dependencies in interfaces for ease of portability and testing Explore ...

    Java2核心技术卷I+卷2:基础知识(第8版) 代码

    Calling a C Function from a Java Program 936 Numeric Parameters and Return Values 942 String Parameters 944 Accessing Fields 950 Encoding Signatures 954 Calling Java Methods 956 Accessing Array ...

    超强大、好用的Pascal语言解释器(RemObjects Pascal Script)

    超强大、好用的Pascal语言解释器:RemObjects ... Capability to call RemObjects SDK Services from within scripts Pascal Script includes a tool to create headers for importing classes and interfaces

    Calling C Programs from IDL

    It's obvious you don't know squat about calling C programs from IDL. Who does? IDL 中引用c++程序的方法

    193_4ECH.PDF

    from Perl scripts, calling the component’s methods and using its properties. In fact, that code component will even be able to display windows, letting you use OLE automation to do what you can’t ...

    jmockit测试例子

    It indeed a bad smell, but before you refactor them, you may need to make them testable firstly. Now, jmockit comes to us. "Tests can easily be written that will mock final classes, static methods...

    Cross-platform.UI.Development.with.Xamarin.Forms.1784391190

    Next, you will learn to plan the UI using Xamarin.Forms for cross-mobile platform development, and move on to creating custom buttons, extending the UI, and connecting to social sites such as ...

Global site tag (gtag.js) - Google Analytics