问题
I am using JBoss4.2.2 and java6.
The deployed ear's name is apa.ear
In a servlet I have the following code line:
placeBid = (PlaceBid) context.lookup("apa/"
+ PlaceBid.class.getSimpleName() + "/remote");
I have a generated jboss-app.xml like this:
<jboss-app>
<loader-repository>apa:app=ejb3</loader-repository>
</jboss-app>
When trying to get the PlaceBid via the context I get this exception
java.lang.ClassCastException: $Proxy99 cannot be cast to se.nextit.actionbazaar.buslogic.PlaceBid
The PlaceBid interface looks like this:
@Remote
public interface PlaceBid {
Long addBid(String userId, Long itemId, Double bidPrice);
}
When I run the example coming with EJB3 in action it works. EJB3 in action sample code comes with ant building. I want to use Maven so I have rearranged the code some.
However, I don't understan what I am doing wrong here. I have some thoughts about the jboss-app.xml file. I am not sure of how its content should look like.
Grateful for any help.
Best wishes Lasse
回答1:
As a first step, try the following:
Object obj = context.lookup("apa/" + PlaceBid.class.getSimpleName() + "/remote");
System.out.println("Object = " + obj.getClass().getName());
System.out.println("Interfaces = " + Arrays.toString(obj.getClass().getInterfaces()));
It will tell you what the actual stub concrete class is and what interfaces it implements. This may then give you enough of a hint to work out what's going wrong.
Also presumably your bean is defined as:
@Stateless
public class MyPlaceBidBean implements PlaceBid {
...
}
i.e. it implements the PlaceBid
interface?
回答2:
If context is of type javax.naming.Context (or InitialContext), then the problem is that you have a missing call to PortableRemoteObject.narrow:
placeBid = (PlaceBid) PortableRemoteObject.narrow(context.lookup("apa/"
+ PlaceBid.class.getSimpleName() + "/remote", PlaceBid.class);
This is required by the EJB specification for remote interfaces. If you use a reference instead (@EJB or ejb-ref), the container will handle the narrow for you.
来源:https://stackoverflow.com/questions/2576510/java-lang-classcastexception-proxy99-cannot-be-cast