EJB3的代码:
package communicateMgmt;
import javax.ejb.Stateless;
@Stateless
public class CommunicateBean implements CommunicateBeanLocal,
CommunicateBeanRemote {
public void queryParameter() {
System.out.println("Query Parameter");
}
}
package communicateMgmt;
import javax.ejb.Remote;
@Remote
public interface CommunicateBeanRemote {
public void queryParameter();
}
JBoss启动后显示:
12:22:28,375 INFO [JBossASKernel] Created KernelDeployment for: CommunicateEJB.jar
12:22:28,375 INFO [JBossASKernel] installing bean: jboss.j2ee:jar=CommunicateEJB.jar,name=CommunicateBean,service=EJB3
12:22:28,375 INFO [JBossASKernel] with dependencies:
12:22:28,375 INFO [JBossASKernel] and demands:
12:22:28,375 INFO [JBossASKernel] jboss.ejb:service=EJBTimerService
12:22:28,375 INFO [JBossASKernel] and supplies:
12:22:28,375 INFO [JBossASKernel] jndi:CommunicateBean/remote
12:22:28,375 INFO [JBossASKernel] Class:communicateMgmt.CommunicateBeanLocal
12:22:28,375 INFO [JBossASKernel] jndi:CommunicateBean/remote-communicateMgmt.CommunicateBeanRemote
12:22:28,375 INFO [JBossASKernel] jndi:CommunicateBean/local-communicateMgmt.CommunicateBeanLocal
12:22:28,390 INFO [JBossASKernel] jndi:CommunicateBean/local
12:22:28,390 INFO [JBossASKernel] Class:communicateMgmt.CommunicateBeanRemote
12:22:28,390 INFO [JBossASKernel] Added bean(jboss.j2ee:jar=CommunicateEJB.jar,
name=CommunicateBean,service=EJB3) to KernelDeployment of: CommunicateEJB.jar
12:22:28,843 INFO [SessionSpecContainer] Starting jboss.j2ee:jar=CommunicateEJB.jar,name=CommunicateBean,service=EJB3
12:22:28,875 INFO [EJBContainer] STARTED EJB: communicateMgmt.CommunicateBean ejbName: CommunicateBean
12:22:29,046 INFO [JndiSessionRegistrarBase] Binding the following Entries in Global JNDI:
CommunicateBean/remote - EJB3.x Default Remote Business Interface
CommunicateBean/remote-communicateMgmt.CommunicateBeanRemote - EJB3.x Remote Business Interface
CommunicateBean/local - EJB3.x Default Local Business Interface
CommunicateBean/local-communicateMgmt.CommunicateBeanLocal - EJB3.x Local Business Interface
12:22:29,218 INFO [Http11Protocol] Starting Coyote HTTP/1.1 on http-127.0.0.1-8
080
12:22:29,250 INFO [AjpProtocol] Starting Coyote AJP/1.3 on ajp-127.0.0.1-8009
12:22:29,265 INFO [ServerImpl] JBoss (Microcontainer) [5.0.0.GA (build: SVNTag=
JBoss_5_0_0_GA date=200812042120)] Started in 54s:265ms
客户端代码:
System.out.println("EJB begin");
Properties prop = new Properties();
prop.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
prop.put(Context.PROVIDER_URL, "localhost:1099");
prop.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
Context ctx = new InitialContext(prop);
CommunicateBeanRemote objref = (CommunicateBeanRemote)ctx.lookup("CommunicateBean/remote");
objref.queryParameter();
执行上面客户端代码后,显示异常:
EJB begin
java.lang.ClassCastException: javax.naming.Reference cannot be cast to communicateMgmt.CommunicateBeanRemote
at service.impls.DeviceManagerImpl.addDevice(DeviceManagerImpl.java:59)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
...
------解决方案--------------------------------------------------------
Properties prop = new Properties();