Accessing Stateful session bean from servlet/jsp throws ClassCast exception in jboss 4.2.2 - jboss

Whenever I try to access EJB 3 Stateful session bean deployed on JBoss 4.2.2 application server using a web client(within JBoss server) such as servlet/jsp , I get following exception
java.lang.ClassCastException: $Proxy123
If I use application client , same code works without any exception.
Or if I change bean to #Stateless , even web client works fine.
If I deploy my .war on the tomcat container externally, again web client works fine for stateful bean also.
So in short : Stateful bean EJB 3 deployed on JBoss 4.2 and accessed via web client deployed on JBoss throws classcastexception
This is my code - client side, written in a servlet
Properties properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
properties.put(Context.PROVIDER_URL, "jnp://localhost:1099");
Context c = new InitialContext(properties);
InvokePOJORemote ref = (InvokePOJORemote) c.lookup("InvokePOJOBean/remote");
out.println("<h1>Servlet ejb ref " + ref + "</h1>");
boolean sts = ref.addEmployee("Mad", (short) 30, new java.math.BigDecimal(12000));
This throws ClasscastException at the lookup statement , at time of casting. Dont know what is wrong! Pls help.

This is a bit disgusting but you need to keep the stateful ejb interface outside the war file.That is neither in the classes folder(of WEB-INF) as .class file nor in the lib folder(of WEB-INF) as .jar file.
Instead wht u can do is create a jar file of the stateful ejb interface and for servlet compilation add the jar to ur project.. say using "add external jars" in Eclipse.
That should work..

Related

How to inject EntityManager in a MDB which is configured in ejb-jar.xml

I have demo application with JPA 2.1 and weblogic 12.1.3 deployment works fine and am able to get EntityManager which is annotated on the stateless bean.
I have application where I have ejb-jar.xml, on this file we have configured all MDB and session beans, Note: application is not using annotation.
And ejb-jar.xml uses http://java.sun.com/dtd/ejb-jar_2_0.dtd(ejb 2), but our application is running on weblogic 12.1.3. On this configuration, I tried to get EntityManager with the help of annotation in MDB, but am getting null object.
Question.
1. Is it possible to annotate EntityManager, with above configuration.
2. If not possible what are alternative ways. meaning I need to move all the configuration of ejb-jar.xml to annotation.
Thanks
Daya

JBoss Eclipse - Deploying a War with Jar

I am using JBoss AS 7 with Eclipse Indigo, and i have a problem with deploying a WAR.
In fact, i have 2 projects :
First containing the "Logic part": The differents EJB (Session Bean + Entity Bean)
Second containing the "Web part": Related to the web application, which have some JSF, xhtml and backing bean stuff.
So basically, I added to my Second project classpath, the first (build path -> project -> add).
Then i try to make a basic xhtml file (form), interacting with a backing bean, interacting itself with the Session Bean of my first project.
But when i try to deploy my application, i have the following exception :
catching: org.jboss.weld.resources.spi.ResourceLoadingException: Error loading class MY_BACKINGBEAN_CLASS
Caused by: java.lang.NoClassDefFoundError: MY_SESSIONBEAN_CLASS
I don't get why this error happens, because i included in my classpath the project containing all the session bean.
I know there are a lot of thing about deploying war, but i didn't find any good solution.
Thanks.

Inject Application scoped CDI bean into stateless EJB bean

First I will describe what I am trying to accomplish here. I have a Java application that periodically reads data and calls stateless EJB on JBoss AS 7.1.1 for further operations (computing data and saving it into the DB). Then I have front end which uses JSF 2.0. In controller which is a #ApplicationScoped CDI bean and resides in JSF project I inject EJB into it. Now I need EJB for getting data from the DB. I am using #ApplicationScoped CDI bean because of this reason:
The application context is shared between all servlet requests, web service invocations, EJB remote method invocations, EJB asynchronous method invocations, EJB timeouts and message deliveries to message-driven beans that execute within the same application. The application context is destroyed when the application is shut down.
I want only one CDI bean for all the clients, because the data is independent of the user.
Now I want to update data in #ApplicationScoped CDI bean, which is defined under JSF project with the help of EJB bean, which method is executed when new data arrives. I have already successfully used #Inject in #ApplicationScoped CDI bean, where I have injected EJB from EJB project. Now I want to do the other way around. I tried to inject ApplicationScoped CDI bean from JSF project into EJB bean. But when I wrote this I got #Inject underline as warning:
#Inject
private CurrentDataController currentDataController;
The warning is:
No bean is eligible for injection to the injection point [JSR-299 §5.2.1]
When I try to publish project I get error about class not found exception for CurrentDataController.
Caused by: java.lang.ClassNotFoundException: controllers.CurrentDataController from [Module "deployment.TestEAR.ear.TestEJB.jar:main" from Service Module Loader]
It seems that the EJB project can't reference class in JSF project. Also it is looking for CurentDataController class in TestEJB.jar instead of in the TestJSF.jar. What I am missing here?
The structure of my whole project is as follow:
TestEAR
TestEJB
TestEJBClient
TestJPA
TestJSF
Now I reckon that after I will fix the error about no class definition found I will have another problem connected with the warning I have posted.
In an EAR the EJB modules (jar) don't have visibility on the Web modules (war). EJB module is UI agnostic so it's normal to have this layer decoupling.
To resolve you issue you have two options.
Move the bean that need to be injected in your EJB in one EJB module (the war would also be able to inject that bean)
Refactor your app to have only a war (yes you can have EJB in war now). Architecture would be simplier and full EJB will work under JBoss (remote EJB too)

Cannot access bean when deploying ejb module and web module separately?

I was testing an EJB 2.x application. I created 2 module separately:
EJB module: contains a simple stateless session bean
Web module: contains a single servlet page to lookup EJB module.
I was using Jboss 4.2.3.
First, I deployed the EJB module and the deployment went well.
Second, I deployed the web module and the deployment went well.
Then I used the following code to look up the EJB module:
Context c = new InitialContext();
Object o = c.lookup("HelloJNDI"); // Line 1
HelloLocalHome rv = (HelloLocalHome) o; // Line 2
HelloLocal local = rv.create();
The lookup went well (Line 1), but Line 2 produced a class cast exception.
Then I test the above code in 2 scenarios:
I packaged the EJB and the Web module into a single EAR module. Then, deployed this EAR module in JBoss 4.2.3, and the lookup code above worked like a charm.
I tried to use JBoss 5, and even deployed the EJB module and the Web module separately, the lookup code above worked great.
So, why when I deployed the 2 modules separately in JBoss 4, things did not work out? I use local JNDI lookup only because the 2 modules were deployed in the same container.
Was I missing something or this is a flaw in JBoss 4?
Try using the following code instead of your cast:
HelloLocalHome rv = (HelloLocalHome)javax.rmi.PortableRemoteObject.narrow(o, HelloLocalHome.class);
If that does now work, then you have the infamous classloading problem. (One of the reasons JBoss 5's default server uses a different classloading mechanism). Easiest is to indeed place them in a single EAR (so the Home and remote classes are loaded once). The Home interface class in your web application is loaded by a different classloader than the one being returned by JNDI.
You can also remove the interfaces from your WAR file's classes or lib dirrectory

Resources injection doesn't work when running jms examples in eclipse.

I want to run the jms tutorial files in eclipse. I have configured glassfish properly and added the jms resources. It works ok if I run it in netbeans.
I created an "application client project" in eclipse.
Here is where the resources get injected:
#Resource(mappedName = "jms/ConnectionFactory")
private static ConnectionFactory connectionFactory;
#Resource(mappedName = "jms/Queue")
private static Queue queue;
#Resource(mappedName = "jms/Topic")
private static Topic topic;
Then, when it gets here:
connection = connectionFactory.createConnection();
I get a NullPointerException. For some reason it doesn't get the resources from the server so they are null.
It works if I build the project in eclipse and then run it from a command line using appclient:
appclient Producer topic 4
It says here http://help.eclipse.org/indigo/index.jsp?topic=%2Forg.eclipse.jst.j2ee.doc.user%2Ftopics%2Fcjappcliproj.html that:
An application client module is used to contain a full-function client
Java™ application (non Web-based) that connects to and uses the Java
EE resources defined in your server. When you place the client code in
an application client module instead of a simple JAR file, the
application client benefits from the server's resources (it does not
need to re-specify the class path to Java EE and server JAR files) as
well as from easier JNDI lookup (the client container fills in the
initial context and other parameters).
So it seems that I am on the right track, but maybe I am missing some eclipse configuration.
As you've already noticed, if you run your Application in standalone mode you will not be able to access servers resources, this is by design. You need to use Glassfish appclient (or similar tools for other application servers) in order to run a Java EE Client App.
Heres another thread that may help you configure your project to run correctly: eclipse howto start a application client on java ee glassfish appl srv.