How to access an EJB? - java

I am trying to develop a Basic EJB3 application on JBOSS 4.2 in Eclipse
I have created an EJB project in eclipse.
The following are my remote and local interfaces.
package com.test;
import javax.ejb.Local;
#Local
public interface HelloWorldLocal
{
public String getGreeting();
}
package com.test;
import javax.ejb.Remote;
#Remote
public interface HelloWorldRemote
{
public String getGreeting();
}
and my ejb implementation is
package com.test;
import javax.ejb.Stateless;
#Stateless
public class HelloWorld implements HelloWorldRemote, HelloWorldLocal {
public HelloWorld() {
// TODO Auto-generated constructor stub
}
public String getGreeting() {
// TODO Auto-generated method stub
return "First EJB People";
}
}
I have deployed this as an exploded JAR in JBoss and it runs fine.
My first question is:
What else do I have to add to this exploded jar ?
Secondly I created a stand alone client and added the above jar to its classpath
The client code is as follows
package com.testejb;
import java.io.FileInputStream;
import java.util.Properties;
import javax.naming.InitialContext;
public class TestBean {
/**
* #param args
*/
public static void main(String[] args)
{
// TODO Auto-generated method stub
HelloWorldRemote getMess = null;
try {
Properties props = new Properties();
Properties props = new Properties();
props.setProperty("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory");
props.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming");
props.setProperty("java.naming.provider.url", "localhost:1099");
InitialContext ic = new InitialContext(props);
//
getMess = (HelloWorldRemote) ic.lookup("HelloWorldRemote/remote");
System.out.println(getMess.getGreeting());
} catch (Exception e)
{
// TODO: handle exception
e.printStackTrace();
}
}
}
The name of the jar is FirstEJB.
I have tried the look up as FirstEJB/HelloWorldRemote/remote.
But when I run the program I get the error
javax.naming.NameNotFoundException: HelloWorldRemote not bound
If I type the lookup as HelloWorld/remote i get the error
Caused by: java.io.InvalidClassException: org.jboss.ejb3.remoting.BaseRemoteProxy; local class incompatible: stream classdesc serialVersionUID = 1126421850898582900, local class serialVersionUID = -2711693270411201590

What else do I have to add to this exploded jar ?
Nothing, it's usable.
I have tried the look up as FirstEJB/HelloWorldRemote/remote
With JBoss, the JNDI name will be:
<myEarName>/<EJB-Name>/remote
Where the EJB-Name defaults to the Bean name if not specified. So in your case, without using an EAR packaging, the JNDI name should be:
HelloWorld/remote
This should be logged in the server logs at deployment time by the way.
If I type the lookup as HelloWorld/remote I get the error (...)
The JNDI name used for the lookup is correct, this error is another problem that looks very similar to EJBTHREE-1118. Could you try with JBoss 4.2.3.GA?

Related

NameAlreadyBoundException using JNDI

I created a session bean with this code:
package ejb2;
import javax.annotation.Resource;
import javax.ejb.SessionContext;
import javax.ejb.Stateless;
#Stateless(name = "TestEJB", mappedName = "EJB2-Project1-TestEJB")
public class TestEJBBean implements TestEJB, TestEJBLocal {
#Resource
SessionContext sessionContext;
public TestEJBBean() {
}
public String getHello(String who_welcome) {
return "Hello " + who_welcome;
}
}
As you can see, it's almost a default code (except getHello method). Besides this bean I have a client:
package ejb2;
import java.util.Hashtable;
import javax.naming.CommunicationException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class TestEJBClient {
public static void main(String[] args) {
try {
final Context context = getInitialContext();
TestEJB testEJB = (TestEJB) context.lookup("EJB2-Project1-TestEJB#ejb2.TestEJB");
System.out.println(testEJB.getHello("Student"));
} catch (CommunicationException ex) {
System.out.println(ex.getClass().getName());
System.out.println(ex.getRootCause().getLocalizedMessage());
System.out.println("\n*** A CommunicationException was raised. This typically\n*** occurs when the target WebLogic server is not running.\n");
} catch (Exception ex) {
ex.printStackTrace();
}
}
private static Context getInitialContext() throws NamingException {
Hashtable env = new Hashtable();
// WebLogic Server 10.x/12.x connection details
env.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
env.put(Context.PROVIDER_URL, "t3://localhost:7101");
return new InitialContext(env);
}
}
First time it worked like a charm. But then I created another bean:
package ejb2;
import javax.annotation.Resource;
import javax.ejb.Stateless;
import javax.ejb.SessionContext;
import javax.naming.Context;
import javax.naming.InitialContext;
#Stateless(name = "ClientEJB", mappedName = "EJB2-Project1-ClientEJB")
public class ClientEJBBean implements ClientEJB, ClientEJBLocal {
#Resource
SessionContext sessionContext;
TestEJB testEJB;
public ClientEJBBean() {
try {
final Context context = new InitialContext();
testEJB = (TestEJB) context.lookup("EJB2-Project1-TestEJB#ejb2.TestEJB");
} catch (Exception ex) {
ex.printStackTrace();
}
}
public String getHelloFromBean(String who) {
return testEJB.getHello(who);
}
}
And now beans aren't working. I get an error like this:
weblogic.application.ModuleException: Unable to bind Business Interface to the JNDI name: EJB2Project1WebApp_warClientEJB_Home, throw exception javax.naming.NameAlreadyBoundException: [EJB:011224]Unable to bind the interface ejb2.ClientEJB to ClientEJB. Another EJB has already bound an interface to that name.; remaining name 'EJB2-Project1-ClientEJB#ejb2'. NestedException Message is :[EJB:011224]Unable to bind the interface ejb2.ClientEJB to ClientEJB. Another EJB has already bound an interface to that name.
What's the problem with these codes?
As far as i can see you try to deploy two stateless EJBs with the same JNDI name
Try to undeploy the current application , check the JNDI tree from Admin Console
and make sure the tree does not have the JNDI name you see as duplicate.

How to create EJB 3.2 Stateless Bean Project and Running it on Wildfly 8?

I'm trying to create EJB 3.2 Stateless Bean Project but still no avail until now. I hope anyone can help me.
Here is the EJB Project structure on my eclipse:
SLBean.java
package com.example.ejbtest;
import javax.ejb.Stateless;
#Stateless
public class SLBean implements SLBeanRemote {
public SLBean() {
}
#Override
public String sayHello() {
return "Hello World !!!";
}
}
SLBeanRemote.java
package com.example.ejbtest;
import javax.ejb.Remote;
#Remote
public interface SLBeanRemote {
public String sayHello();
}
jboss-ejb-client.properties
endpoint.name=client-endpoint
remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED=false
remote.connections=default
remote.connection.default.host=localhost
remote.connection.default.port=4447
remote.connection.default.username=user1
remote.connection.default.password=password
remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS=false
Test.java
package com.example.ejbtest;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class Test {
public static void main(String[] args) {
Context context;
try {
Properties properties = new Properties();
properties.put(Context.URL_PKG_PREFIXES,"org.jboss.ejb.client.naming");
properties.put("jboss.naming.client.ejb.context", true);
context = new InitialContext(properties);
String appName = "";
String moduleName = "EJBTest";
String distinctName = "";
String beanName = SLBean.class.getSimpleName();
String viewClassName = SLBeanRemote.class.getName();
String ejbString = "ejb:" + appName + "/" + moduleName + "/"
+ distinctName + "/" + beanName + "!" + viewClassName;
System.out.println(ejbString);
SLBeanRemote remote = (SLBeanRemote) context.lookup(ejbString);
System.out.println(remote.sayHello());
} catch (NamingException e) {
e.printStackTrace();
}
}
}
So after I run the EJBTest project on Wildfly Server through Eclipse, I run the Test.java as client program to connect to the remote stateless bean.
But what I got was error like this:
Exception in thread "main" java.lang.IllegalStateException:
EJBCLIENT000025: No EJB receiver available for handling [appName:,
moduleName:EJBTest, distinctName:] combination for invocation context
org.jboss.ejb.client.EJBClientInvocationContext#28d72e3f at
org.jboss.ejb.client.EJBClientContext.requireEJBReceiver(EJBClientContext.java:749)
at
org.jboss.ejb.client.ReceiverInterceptor.handleInvocation(ReceiverInterceptor.java:116)
at
org.jboss.ejb.client.EJBClientInvocationContext.sendRequest(EJBClientInvocationContext.java:186)
Anyone can help?
Ok, I found the answer. I had made two mistakes:
I didn't put the jboss-ejb-client.properties file in correct place, i.e. the root of source folder (ejbModule).
According to this article, the remote port for Wildfly 8 has been incorporated to port 8080, so I just need to change the remote.connection.default.port to 8080 in jboss-ejb-client.properties.

How to run Application Client on the GlassFish Server 4 from command line

I just started to learn EJB and I wrote a simple bean and client application. Below is a code:
Interface:
package ejb;
import javax.ejb.Remote;
#Remote
public interface MySessionRemote {
String getResult();
}
Bean:
import javax.ejb.EJB;
import javax.ejb.Stateless;
#Stateless
public class MySession implements MySessionRemote {
// #EJB
//private static MySessionRemote mySession1;
#Override
public String getResult() {
return "Bean";
}
}
Client application:
package entappclient;
import javax.naming.*;
import ejb.MySessionRemote;
import java.util.Properties;
import javax.ejb.EJB;
public class Main {
// #EJB private static MySessionRemote mySession;
public static void main(String[] args) {
// TODO code application logic here
new Main().Save();
}
public Context getContext() throws javax.naming.NamingException {
Properties props = new Properties();
props.setProperty("java.naming.factory.initial",
"com.sun.enterprise.naming.SerialInitContextFactory");
props.setProperty("java.naming.factory.url.pkgs",
"com.sun.enterprise.naming");
props.setProperty("java.naming.factory.state",
"com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl");
props.setProperty("org.omg.CORBA.ORBInitialHost", "localhost");
return new InitialContext(props);
}
public void Save () {
//System.out.println(mySession1.getResult());
try {
Context jndiContext = getContext();
System.out.println("0");
MySessionRemote ref = ySessionRemote)jndiContext.lookup("java:global/EntAppEJB- ejb/MySession!ejb.MySessionRemote");
System.out.println("1");
System.out.println(ref.getResult());
} catch (Exception e ) {
System.out.println("Save"+e);
}
}
}
When I run it under NetBeans 8 everything works fine. The result is:
0
1
Bean
But when I try to run this from command line using : appclient - client path/EntAppClient.jar, it does not work. The result is:
0
Savejavax.naming.NamingException: Lookup failed for 'java:global/EntAppEJB-ejb/M
ySession!ejb.MySessionRemote' in SerialContext[myEnv={java.naming.factory.initia
l=com.sun.enterprise.naming.SerialInitContextFactory, org.omg.CORBA.ORBInitialHo
st=localhost, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.J
NDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.naming} [Ro
ot exception is javax.naming.NamingException: ejb ref resolution error for remot
e business interfaceejb.MySessionRemote [Root exception is java.lang.ClassNotFou
ndException: ejb.MySessionRemote]]
Can anybody help me to find out why I can run this under NetBeans but it does not work from command line.
Best regards
Marcin

IPOJO - The factory associated with the component type is invalid (not started or missing handlers)

I am trying to learn how to use Felix iPOJO API to create components dynamically.
I have a simple bundle with the following files:
1- HelloService.java (Contains a Service Interface).
/*
* #author zaid almahmoud
*
*/
package helloipojo.service;
public interface HelloService
{
public void sayHello();
}
2- Its implementation HelloServiceImpl.java:
package helloipojo;
import helloipojo.service.HelloService;
public class HelloServiceImpl implements HelloService{
#Override
public void sayHello() {
System.out.println("Hello iPojo!");
}
}
3- Activator.java :
package helloipojo;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
public class Activator implements BundleActivator {
public void start(BundleContext context) throws Exception {
System.out.println("Bundle Started!");
}
public void stop(BundleContext context) throws Exception {
context = null;
System.out.println("Bundle Stopped!");
}
}
4- MANIFEST.MF :
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: HelloIPojo
Bundle-SymbolicName: HelloIPojo
Bundle-Version: 1.0.0.qualifier
Bundle-Activator: helloipojo.Activator
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Import-Package: org.osgi.framework
In my application, I start Felix framework and deploy the following bundles:
iPOJO (core)
iPOJO Composite
iPOJO API
According to this source.
Next, I install my bundle, and instantiate the component. Below is my class:
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.felix.framework.Felix;
import org.apache.felix.framework.util.FelixConstants;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;
import org.osgi.framework.Constants;
import org.osgi.framework.ServiceRegistration;
import org.apache.felix.ipojo.ComponentInstance;
import org.apache.felix.ipojo.ConfigurationException;
import org.apache.felix.ipojo.MissingHandlerException;
import org.apache.felix.ipojo.UnacceptableConfiguration;
import org.apache.felix.ipojo.api.ComponentType;
import org.apache.felix.ipojo.api.PrimitiveComponentType;
import org.apache.felix.ipojo.api.Service;
public class HostApplication
{
private HostActivator m_activator = null;
private Felix m_felix = null;
public HostApplication()
{
// Create a configuration property map.
Map config = new HashMap();
config.put(Constants.FRAMEWORK_STORAGE_CLEAN, Constants.FRAMEWORK_STORAGE_CLEAN_ONFIRSTINIT);
// Create host activator;
m_activator = new HostActivator();
List list = new ArrayList();
list.add(m_activator);
config.put(FelixConstants.SYSTEMBUNDLE_ACTIVATORS_PROP, list);
try
{
// Now create an instance of the framework with
// our configuration properties.
m_felix = new Felix(config);
// Now start Felix instance.
m_felix.start();
}
catch (Exception ex)
{
System.err.println("Could not create framework: " + ex);
ex.printStackTrace();
}
// Register the application's context as an OSGi service!
BundleContext bundleContext1 = m_felix.getBundleContext();
System.out.println("6");
try {
//starting ipojo required bundles
Bundle coreBundle = bundleContext1.installBundle("file:C:\\Users\\zaid.almahmoud\\Dropbox\\EBTIC\\ADERE\\feasibility-codes\\ipojo\\ipojo-distribution-1.11.0\\bundle\\org.apache.felix.ipojo-1.6.2.jar");
coreBundle.start();
if(coreBundle.getState()== coreBundle.ACTIVE)
System.out.println("Core Bundle is Active!");
Bundle apiBundle = bundleContext1.installBundle("file:C:\\Users\\zaid.almahmoud\\Dropbox\\EBTIC\\ADERE\\feasibility-codes\\ipojo\\ipojo-distribution-1.11.0\\bundle\\org.apache.felix.ipojo.api-1.6.0.jar");
apiBundle.start();
if(apiBundle.getState()== apiBundle.ACTIVE)
System.out.println("API Bundle is Active!");
Bundle compositeBundle = bundleContext1.installBundle("file:C:\\Users\\zaid.almahmoud\\Dropbox\\EBTIC\\ADERE\\feasibility-codes\\ipojo\\ipojo-distribution-1.11.0\\bundle\\org.apache.felix.ipojo.composite-1.6.0.jar");
compositeBundle.start();
if(compositeBundle.getState()== compositeBundle.ACTIVE)
System.out.println("Composite Bundle is Active!");
//HERE INSTALLING AND STARTING MY BUNDLE!!
Bundle b = bundleContext1.installBundle("file:C:\\Users\\zaid.almahmoud\\Desktop\\plugins\\HelloIPojo_1.0.0.201401211340.jar");
b.start();
try {
ComponentType type = new PrimitiveComponentType()
.setBundleContext(b.getBundleContext())
.setComponentTypeName("hello.type")
.setClassName("helloipojo.HelloServiceImpl")
.setImmediate(true);
type.start();
ComponentInstance instance = type.createInstance();
}
catch (UnacceptableConfiguration
| MissingHandlerException | ConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} // Create the instance
System.out.println("done starting bundles!");
} catch (BundleException e) {
e.printStackTrace();
System.out.println("Not done!");
}
//shutdownApplication();
}
public Bundle[] getInstalledBundles()
{
// Use the system bundle activator to gain external
// access to the set of installed bundles.
return m_activator.getBundles();
}
public void shutdownApplication()
{
// Shut down the felix framework when stopping the
// host application.
try {
m_felix.stop();
} catch (BundleException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
m_felix.waitForStop(0);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
When I run my application, it shows the following output (with the error at the end):
6
Core Bundle is Active!
API Bundle is Active!
Composite Bundle is Active!
Bundle Started!
Exception in thread "main" java.lang.IllegalStateException: The factory associated with the component type is invalid (not started or missing handlers)
at org.apache.felix.ipojo.api.ComponentType.ensureFactory(ComponentType.java:189)
at org.apache.felix.ipojo.api.ComponentType.ensureAndGetFactory(ComponentType.java:177)
at org.apache.felix.ipojo.api.ComponentType.createInstance(ComponentType.java:79)
at HostApplication.<init>(HostApplication.java:109)
at Embedder.main(Embedder.java:11)
Where did I go wrong? Thanks.
Update 1
I could see that I am missing 2 handlers. I knew that by adding the following two lines:
System.out.println(type.getFactory().getRequiredHandlers());
System.out.println(type.getFactory().getMissingHandlers());
The output of the above two lines is:
[org.apache.felix.ipojo:architecture, org.apache.felix.ipojo:callback]
[org.apache.felix.ipojo:architecture, org.apache.felix.ipojo:callback]
I also tried:
type.getFactory().createComponentInstance(new Properties());
then I got:
org.apache.felix.ipojo.MissingHandlerException: Missing handlers : org.apache.felix.ipojo:architecture org.apache.felix.ipojo:callback
I don't know why these handlers are missing. I tried to add them, but could not figure out the right syntax. Any help? Thanks.
Update 2
According to Clement in his answer, my bundle should import: org.apache.felix.ipojo and org.apache.felix.ipojo.architecture
I did that, and now I am getting the following error:
java.lang.ClassCastException: org.apache.felix.ipojo.HandlerManagerFactory cannot be cast to org.apache.felix.ipojo.HandlerFactory
I am getting the error at this line: type.start();
Please help. Thanks!
The issue comes from the asynchronous start of iPOJO. When you create your instance, not everything is available.
I’ve several question: Why are you using the iPOJO API to declare your type and instance ? Can’t you just use the annotations ? In that case it will just create everything smoothly.
If you really need / want to use the API, don’t create the instance like you do, but expose an instance declaration: http://felix.apache.org/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-userguide/ipojo-advanced-topics/ipojo-factory-service.html#deleting-instances. The declaration waits until the factory is valid to create the instance.

class cast exception in narrow a jndi reffrence in ejb

I am trying to write a simple stateless sesssion bean but I have problem with narrow reference I give in lookup time.
I got
class cast exeption
I use
eclipse IDE
my bean class
package codes;
import java.rmi.RemoteException;
import javax.ejb.EJBException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
public class SinaBean implements SessionBean {
/**
*
*/
private static final long serialVersionUID = 1L;
public String getHello()
{
return "hello";
}
public void ejbCreate(){
}
#Override
public void ejbActivate() throws EJBException, RemoteException {
// TODO Auto-generated method stub
}
#Override
public void ejbPassivate() throws EJBException, RemoteException {
// TODO Auto-generated method stub
}
#Override
public void ejbRemove() throws EJBException, RemoteException {
// TODO Auto-generated method stub
}
#Override
public void setSessionContext(SessionContext arg0) throws EJBException,
RemoteException {
// TODO Auto-generated method stub
}
}
my home interface
package codes;
import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.ejb.EJBHome;
public interface SinaHome extends EJBHome {
public SinaObject create() throws RemoteException,CreateException;
}
my component
package codes;
import java.rmi.RemoteException;
import javax.ejb.EJBObject;
public interface SinaObject extends EJBObject {
String getHello() throws RemoteException;
}
my client
package codes;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
public class Client {
/**
* #param args
*/
public static void main(String[] args) {
Context con=null;
try {
Properties p=new Properties();
p.setProperty(Context.INITIAL_CONTEXT_FACTORY,
"org.jnp.interfaces.NamingContextFactory");
p.setProperty(Context.PROVIDER_URL, "localhost:1099");
p.setProperty(Context.URL_PKG_PREFIXES,
"org.jboss.namingrg.jnp.interfaces");
con = new InitialContext(p);
Object o=con.lookup("SinaBean");
System.out.println(o);/***/untill know it works.it sysout :
//org.jnp.interfaces.NamingContext#e83912***
#SuppressWarnings("unused")
SinaHome sh=(SinaHome)PortableRemoteObject.narrow(o, SinaHome.class);//***exeption is here!!***
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
my xml file
<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd">
<display-name>Ejb </display-name>
<enterprise-beans>
<session>
<display-name>SinaBean</display-name>
<ejb-name>SinaBean</ejb-name>
<home>codes.SinaHome</home>
<remote>codes.SinaObject</remote>
<ejb-class>codes.SinaBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Bean</transaction-type>
<security-identity>
<description></description>
<use-caller-identity></use-caller-identity>
</security-identity>
</session>
</enterprise-beans>
</ejb-jar>
the exception I receive
java.lang.ClassCastException
at com.sun.corba.se.impl.javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:229)
at javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:137)
at codes.Client.main(Client.java:27)
Caused by: java.lang.ClassCastException: org.jnp.interfaces.NamingContext cannot be cast to org.omg.CORBA.Object
at com.sun.corba.se.impl.javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:212)
... 2 more
I will be extremely grateful for your advices.
First of all, your xml file indicates you are using EJB3 (which is actually the only version even worth considering), but your bean is an EJB2 bean.
If your server indeed runs EJB3, remove the XML file and remove the needless EJB home and component implementations. Add the #Stateless annotation and a remote interface. Make sure it looks something like this:
#Remote
public interface SinaRemote {
String getHello();
}
#Stateless
public class SinaBean implements SinaRemote {
public String getHello() {
return "hello";
}
}
Then correct your JNDI properties. The following is wrong:
p.setProperty(Context.URL_PKG_PREFIXES, "org.jboss.namingrg.jnp.interfaces");
it should be:
p.setProperty(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
(the variants people use here are almost endless and it basically always works anyway, but just to be sure use the correct variant)
Furthermore, and most likely the main culprit, you're using the ejb-name as-if it was a global JNDI name but this is not the case. The ejb-name is a somewhat confusing logical name that's used as an extra kind of qualifier when referring to a specific bean when injecting or creating an ejb-ref.
In your case you're setting it to the same as the unqualified bean name, which is already the default. Since SinaBean exists, I'm guessing you're not deploying via an EAR, but deploy a standalone EJB jar, right?
If you are using JBoss AS 6 (EJB3.1) you can use the global portable JNDI names, otherwise the old JBoss specific JNDI naming can be used: [ear name]/[simple bean name]/remote. Note that [simple bean name] is not the ejb-name, but again in your case they happened to be the same. Since [ear name]/ is removed when you're deploying only an EJB jar, this explains the exception you're getting: SinaBean is a directory (context in JNDI terms), which can contain the actual entries local, remote and no-interface. What you're doing is trying to cast this intermediate directory node to the proxy to your bean, which of course doesn't work.
Anyway, finally, remove the usage of PortableRemoteObject, this is no longer needed. The client code will then look like this:
Properties properties = new Properties();
properties.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
properties.setProperty(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
properties.setProperty(Context.PROVIDER_URL, "localhost:1099");
context = new InitialContext(properties);
SinaRemote sina = (SinaRemote) context.lookup("SinaBean/remote");
Here If you are using EJB 2.0 along with Jboss 5.0 and trying to type cast Object obj to your Home Object from Client as below then it might throw java.lang.ClassCastException com.sun.proxy.$Proxy104 cannot be cast to org.omg.CORBA.Object .
Code to lookup JNDI :
java.lang.Object obj = ctx.lookup("java:comp/env/ATSessionBean");
ATHome home = (ATHome) PortableRemoteObject.narrow(obj, ATHome.class);
Solution : Here u need to add additional jars in classpath of Client app those are available in Higher version of JBOSS.
So better to download latest version **jboss-7.1.1.Final server and deploy your application and it will run successfully. :)**
download JBOSS server (Download EAP 6.4.0) from below link : http://jbossas.jboss.org/downloads/

Categories

Resources