• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem facing in Ejb execution in eclipse IDE

 
Greenhorn
Posts: 28
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello friends,
I am new to EJB's I started with head first EJB.I am using eclipse IDE configured with JBoss 5.0.1 and I added Xdoclet for annotations purpose and configured it in eclipse. I have developed a simple bean and its two component and home interfaces and configured "ejb-jar.xml".Till this everything is fine.But after this I am not getting how to create "ejb-jar.jar" file and how to configure JNDI source etc and also how to deploy and how to see the output.PLease guide me These are the files which I have created:

Bean class(AdviceBean):
package headfirst;

import java.rmi.RemoteException;

import javax.ejb.EJBException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.ejb.Stateless;

/**
* Session Bean implementation class AdviceBean
*/
@Stateless
public class AdviceBean implements SessionBean {

/**
*
*/

private String[] advString={"check your status","Entered correctly","Password length"};
private static final long serialVersionUID = -2874839413219948343L;

/**
* Default constructor.
*/
public AdviceBean() {
// TODO Auto-generated constructor stub
}

@Override
public void ejbActivate() throws EJBException, RemoteException {
// TODO Auto-generated method stub
System.out.println("In activate");

}

@Override
public void ejbPassivate() throws EJBException, RemoteException {
// TODO Auto-generated method stub
System.out.println("In passivate");


}

@Override
public void ejbRemove() throws EJBException, RemoteException {
// TODO Auto-generated method stub
System.out.println("In remove");


}

@Override
public void setSessionContext(SessionContext arg0) throws EJBException,
RemoteException {
// TODO Auto-generated method stub

System.out.println("In sessioncontext");
}

public String getAdvice()
{
int random=(int) (Math.random() *advString.length);
return(advString[random]);
}
public void ejbCreate()
{

System.out.println("In ejbCreate()");

}

}

Component interface(Advice):

/**
*
*/
package headfirst;

import java.rmi.RemoteException;

import javax.ejb.EJBObject;


public interface Advice extends EJBObject{
public String getAdvice() throws RemoteException;

}

Home interface(AdviceHome):
package headfirst;

import java.rmi.RemoteException;

import javax.ejb.CreateException;
import javax.ejb.EJBHome;

public interface AdviceHome extends EJBHome{
public Advice create() throws RemoteException,CreateException;

}

ejb-jar.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>EjbSample</display-name>
<enterprise-beans>
<session>
<ejb-name>AdviceBean</ejb-name>
<home>headfirst.AdviceHome</home>
<remote>headfirst.Advice</remote>
<ejb-class>headfirst.AdviceBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Bean</transaction-type>
</session>
</enterprise-beans>
</ejb-jar>

Client file(AdviceClient):

import headfirst.Advice;
import javax.naming.*;
import javax.rmi.*;
import java.rmi.*;
import headfirst.*;
import javax.ejb.*;

import javax.rmi.PortableRemoteObject;


public class AdviceClient {

public static void main(String k[])

{
new AdviceClient().go();
}

public void go() {
// TODO Auto-generated method stub
try
{
Context ic=new InitialContext();
Object o=ic.lookup("AdviceBean");
AdviceHome home=(AdviceHome) PortableRemoteObject.narrow(o, AdviceHome.class);
Advice advisor=home.create();
System.out.println(advisor.getAdvice());

}
catch(Exception e)
{
System.out.println("error Now in catch");
}
}
}


These are all the files after this what I have to exe4cute the EJB's please tell em step by step in detail I am after this from 2 days But in vain. Please its haunting me. please help me anyone.
Thanks in advance.
 
Srinivas Karthik
Greenhorn
Posts: 28
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello anybody there,
Please anyone reply.
 
Ranch Hand
Posts: 34
Mac IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sri,

In EJB 3 or later version ejb-jar.xml file is optional. I can see from the code you have pasted, that you are using ejb 3.0 or later versions. To create a jar file and package you application, you can use maven. Maven will do it for you.

You can find all about maven on its home page.

To quickly start building your project, at the base level add pom.xml with following contents




(you can also do it by using maven ejb plugin)

I am assuming you will use maven 2.2.1.

jar can be build by executing on command prompt mvn package

Once jar is build you can then deploy in your jboss server. If you are new, I would recommend using glassfish server.
 
Srinivas Karthik
Greenhorn
Posts: 28
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Syed,
Thanks a lot for your reply was waiting very badly for a reply. Yes, I am using EJB3.0 and I am new to EJB's thats why I am facing problems in executing these things, I can't use glassfish ,because I have to use JBoss 5.1 for the project so I am getting used to it by practicing using JBoss only.You said that ejb-jar.xml is not necessary, If i keep it will it be a problem and regarding jar I din't use any tool I just created it using concole I added my three .class files bean class, home and component interface's .class files and the ejb-xml.xml and made a jar and placed it in the deployment descriptor and trying to execute I am getting an exception.
only half of the program of the client is executing . This is the exception

DEPLOYMENTS IN ERROR:
Deployment "vfszip:/home/karthik/Documents/jboss-5.1.0.GA/server/default/deploy/EjbSample.jar/" is in error due to the following reason(s): java.lang.RuntimeException: Bean Class headfirst.AdviceBean has no local, webservice, or remote interfaces defined and does not implement at least one business interface: AdviceBean


xml:
<?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>EjbSample</display-name>
<enterprise-beans>
<session>
<ejb-name>Advice</ejb-name>
<home>headfirst.AdviceHome</home>
<remote>headfirst.Advice</remote>
<ejb-class>headfirst.AdviceBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Bean</transaction-type>
</session>
</enterprise-beans>
</ejb-jar>

Client program
import headfirst.Advice;
import javax.naming.*;
import javax.rmi.*;
import java.rmi.*;
import headfirst.*;
import javax.ejb.*;
import java.util.Properties;

import javax.rmi.PortableRemoteObject;



@SuppressWarnings("unused")
public class AdviceClient {

public static void main(String k[])

{
new AdviceClient().go();
}

public void go() {
// TODO Auto-generated method stub

Properties pro=new Properties();
pro.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
pro.put(Context.PROVIDER_URL,"localhost:8080");

try
{

InitialContext jndi=new InitialContext(pro);
System.out.println("Got Context");
//Context ic=new InitialContext();

//Object o=ic.lookup("Advice");

Object o=jndi.lookup("Advice");
System.out.println("Got Reference");
AdviceHome home=(AdviceHome) PortableRemoteObject.narrow(o, AdviceHome.class);
Advice advisor=home.create();
System.out.println(advisor.getAdvice());

}
catch(Exception e)
{
System.out.println("error Now in catch");
}
}
}

I am getting the output as:
Got context
error now in catch...

It's always failing at this statement Object o=jndi.lookup("Advice");
and jumping to catch.
Please can you figure out what mistake I have done.
The home and component interfaces are un-changed.

Awaiting for your reply.
Thanks a lot in advance..








 
Greenhorn
Posts: 4
Google Web Toolkit Hibernate Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I had a look at the code snippets and it seems you have mixed up between the EJB 2.x and EJB3.0.
A few quick inputs if you are developing with EJB 3.x :
- You don't need a home interface.
- Your Bean class need not implement SessionBean
- information in ejb-jar.xml need to be only about things which are not provided through annotations or if you want to override what you have specified in annotations.

So the code should be like ( I wrote it on notepad so .. you might need to fix a compilation issues if any ). You can create the jar by any mean you are comfortable with and deploy it .. and you don't need the ejb-jar.xml here ...

/* Bean definition */
package headfirst;
import javax.ejb.Remote;
@Remote
public interface Advice {
public String getAdvice();
}


/* Bean implementation */

package headfirst;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

@Stateless
public class AdviceBean implements Advice {

// you would need this if you use JPA for persistance
@PersistenceContext
private EntityManager em;

private String[] advString={"check your status","Entered correctly","Password length"};

public void getAdvice() {
int random=(int) (Math.random() *advString.length);
return(advString[random]);
}
}


/* The client */
import headfirst.Advice;
import javax.naming.*;
public class AdviceClient {

public static void main(String k[]){
new AdviceClient().go();
}

public void go() {
try {
Context ic=new InitialContext();
// To change this JNDI name, add the annotation or make an entry in ejb-jar.xml
Advice advisor = ic.lookup("AdviceBean/remote");
System.out.println(advisor.getAdvice());

}catch(Exception e){
System.out.println("error Now in catch");
}
}
}




 
Srinivas Karthik
Greenhorn
Posts: 28
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Aravind,
Thanks a lot for the reply, Sure I will try this code and what do you mean by we do not need ejb-jar.xml means we don't need to configure them that's what you meant right.anyways I will try this code and get back to you. Thank you once again
 
Mujahed Syed
Ranch Hand
Posts: 34
Mac IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sri,

Try these, may be you will get an idea:

http://javahowto.blogspot.co.uk/2007/11/simple-ejb-3-on-jboss-application.html
http://docs.jboss.org/ejb3/docs/tutorial/1.0.7/html_single/index.html
http://docs.jboss.org/ejb3/app-server/tutorial/
 
Srinivas Karthik
Greenhorn
Posts: 28
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I searched the google for a sample ejb 3.0 execution using eclipse and jboss and found this link
I followed the same and after creating the three files I am trying to deploy But its giving an exception..
It is as follows


13:30:08,039 ERROR [AbstractKernelController] Error installing to Start: name=jboss.j2ee:jar=EjbProject.jar,name=MyBean,service=EJB3 state=Create
java.lang.ClassCastException: $Proxy271 cannot be cast to javax.naming.Context
at org.jboss.util.naming.Util.createSubcontext(Util.java:70)
at org.jboss.util.naming.Util.bind(Util.java:104)
at org.jboss.util.naming.Util.bind(Util.java:91)
at org.jboss.ejb3.proxy.impl.jndiregistrar.JndiSessionRegistrarBase.bind(JndiSessionRegistrarBase.java:910)
at org.jboss.ejb3.proxy.impl.jndiregistrar.JndiSessionRegistrarBase.bind(JndiSessionRegistrarBase.java:895)
at org.jboss.ejb3.proxy.impl.jndiregistrar.JndiSessionRegistrarBase.bind(JndiSessionRegistrarBase.java:884)
at org.jboss.ejb3.proxy.impl.jndiregistrar.JndiSessionRegistrarBase.bindEjb(JndiSessionRegistrarBase.java:151)
at org.jboss.ejb3.session.SessionContainer.lockedStart(SessionContainer.java:210)
at org.jboss.ejb3.session.SessionSpecContainer.lockedStart(SessionSpecContainer.java:577)
at org.jboss.ejb3.stateless.StatelessContainer.lockedStart(StatelessContainer.java:192)
at org.jboss.ejb3.EJBContainer.start(EJBContainer.java:884)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at org.jboss.reflect.plugins.introspection.ReflectionUtils.invoke(ReflectionUtils.java:59)
at org.jboss.reflect.plugins.introspection.ReflectMethodInfoImpl.invoke(ReflectMethodInfoImpl.java:150)
at org.jboss.joinpoint.plugins.BasicMethodJoinPoint.dispatch(BasicMethodJoinPoint.java:66)
at org.jboss.kernel.plugins.dependency.KernelControllerContextAction$JoinpointDispatchWrapper.execute(KernelControllerContextAction.java:241)
at org.jboss.kernel.plugins.dependency.ExecutionWrapper.execute(ExecutionWrapper.java:47)
at org.jboss.kernel.plugins.dependency.KernelControllerContextAction.dispatchExecutionWrapper(KernelControllerContextAction.java:109)
at org.jboss.kernel.plugins.dependency.KernelControllerContextAction.dispatchJoinPoint(KernelControllerContextAction.java:70)
at org.jboss.kernel.plugins.dependency.LifecycleAction.installActionInternal(LifecycleAction.java:221)
at org.jboss.kernel.plugins.dependency.InstallsAwareAction.installAction(InstallsAwareAction.java:54)
at org.jboss.kernel.plugins.dependency.InstallsAwareAction.installAction(InstallsAwareAction.java:42)
at org.jboss.dependency.plugins.action.SimpleControllerContextAction.simpleInstallAction(SimpleControllerContextAction.java:62)
at org.jboss.dependency.plugins.action.AccessControllerContextAction.install(AccessControllerContextAction.java:71)
at org.jboss.dependency.plugins.AbstractControllerContextActions.install(AbstractControllerContextActions.java:51)
at org.jboss.dependency.plugins.AbstractControllerContext.install(AbstractControllerContext.java:348)
at org.jboss.dependency.plugins.AbstractController.install(AbstractController.java:1631)
at org.jboss.dependency.plugins.AbstractController.incrementState(AbstractController.java:934)
at org.jboss.dependency.plugins.AbstractController.resolveContexts(AbstractController.java:1082)
at org.jboss.dependency.plugins.AbstractController.resolveContexts(AbstractController.java:984)
at org.jboss.dependency.plugins.AbstractController.install(AbstractController.java:774)
at org.jboss.dependency.plugins.AbstractController.install(AbstractController.java:540)
at org.jboss.deployers.vfs.deployer.kernel.BeanMetaDataDeployer.deploy(BeanMetaDataDeployer.java:121)
at org.jboss.deployers.vfs.deployer.kernel.BeanMetaDataDeployer.deploy(BeanMetaDataDeployer.java:51)
at org.jboss.deployers.spi.deployer.helpers.AbstractSimpleRealDeployer.internalDeploy(AbstractSimpleRealDeployer.java:62)
at org.jboss.deployers.spi.deployer.helpers.AbstractRealDeployer.deploy(AbstractRealDeployer.java:50)
at org.jboss.deployers.plugins.deployers.DeployerWrapper.deploy(DeployerWrapper.java:171)
at org.jboss.deployers.plugins.deployers.DeployersImpl.doDeploy(DeployersImpl.java:1439)
at org.jboss.deployers.plugins.deployers.DeployersImpl.doInstallParentFirst(DeployersImpl.java:1157)
at org.jboss.deployers.plugins.deployers.DeployersImpl.doInstallParentFirst(DeployersImpl.java:1178)
at org.jboss.deployers.plugins.deployers.DeployersImpl.install(DeployersImpl.java:1098)
at org.jboss.dependency.plugins.AbstractControllerContext.install(AbstractControllerContext.java:348)
at org.jboss.dependency.plugins.AbstractController.install(AbstractController.java:1631)
at org.jboss.dependency.plugins.AbstractController.incrementState(AbstractController.java:934)
at org.jboss.dependency.plugins.AbstractController.resolveContexts(AbstractController.java:1082)
at org.jboss.dependency.plugins.AbstractController.resolveContexts(AbstractController.java:984)
at org.jboss.dependency.plugins.AbstractController.change(AbstractController.java:822)
at org.jboss.dependency.plugins.AbstractController.change(AbstractController.java:553)
at org.jboss.deployers.plugins.deployers.DeployersImpl.process(DeployersImpl.java:781)
at org.jboss.deployers.plugins.main.MainDeployerImpl.process(MainDeployerImpl.java:702)
at org.jboss.system.server.profileservice.repository.MainDeployerAdapter.process(MainDeployerAdapter.java:117)
at org.jboss.system.server.profileservice.hotdeploy.HDScanner.scan(HDScanner.java:362)
at org.jboss.system.server.profileservice.hotdeploy.HDScanner.run(HDScanner.java:255)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
at java.util.concurrent.FutureTask$Sync.innerRunAndReset(FutureTask.java:351)
at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:178)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:165)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:267)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:679)
13:30:08,062 WARN [HDScanner] Failed to process changes
org.jboss.deployers.client.spi.IncompleteDeploymentException: Summary of incomplete deployments (SEE PREVIOUS ERRORS FOR DETAILS):

DEPLOYMENTS MISSING DEPENDENCIES:
Deployment "jboss.j2ee:jar=EjbProject.jar,name=MyBean,service=EJB3_endpoint" is missing the following dependencies:
Dependency "jboss.j2ee:jar=EjbProject.jar,name=MyBean,service=EJB3" (should be in state "Configured", but is actually in state "**ERROR**")

DEPLOYMENTS IN ERROR:
Deployment "jboss.j2ee:jar=EjbProject.jar,name=MyBean,service=EJB3" is in error due to the following reason(s): java.lang.ClassCastException: $Proxy271 cannot be cast to javax.naming.Context, **ERROR**

at org.jboss.deployers.plugins.deployers.DeployersImpl.checkComplete(DeployersImpl.java:993)
at org.jboss.deployers.plugins.deployers.DeployersImpl.checkComplete(DeployersImpl.java:939)
at org.jboss.deployers.plugins.main.MainDeployerImpl.checkComplete(MainDeployerImpl.java:873)
at org.jboss.system.server.profileservice.repository.MainDeployerAdapter.checkComplete(MainDeployerAdapter.java:128)
at org.jboss.system.server.profileservice.hotdeploy.HDScanner.scan(HDScanner.java:369)
at org.jboss.system.server.profileservice.hotdeploy.HDScanner.run(HDScanner.java:255)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
at java.util.concurrent.FutureTask$Sync.innerRunAndReset(FutureTask.java:351)
at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:178)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:165)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:267)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:679)
It is a dead simple application. If its deployed I can write a client application and add the project.jar and proceed. But during deployment itself its throwing exceptions.
Can any one please figure out what's wrong here..

IMyBean.java:


MyBean.java


MyBeanLocal


MyBeanRemote


Please anyone figure out what's wrong. Do I need to import any jar files or something in my build path for execution of ejb's other than jdk from jboss..
Please I need this. Feeling so down, NOt able to execute a "hello world" in EJB's....
 
Srinivas Karthik
Greenhorn
Posts: 28
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Heyy Guys,
I finally Made it..
I just started from the scratch and built the ejb application adn again got an error, error was like..

javax.naming.CommunicationException: Could not obtain connection to any of these urls: localhost:10299 and discovery failed with error: javax.naming.CommunicationException: Receive timed out [Root exception is java.net.SocketTimeoutException: Receive timed out] [Root exception is javax.naming.CommunicationException: Failed to connect to server localhost/127.0.0.1:10299 [Root exception is javax.naming.ServiceUnavailableException: Failed to connect to server localhost/127.0.0.1:10299 [Root exception is java.net.ConnectException: Connection refused]]]

I used the web port number before here in my client file..
props.setProperty("java.naming.provider.url", "localhost:8080");
Later I explored evrything and found that I have to give the JNDI port number..I gave it as
props.setProperty("java.naming.provider.url", "localhost:1099");

and the result..Wooww My ejb executed successfully I accessed my ejb using a palin java client.. ..After a hell lot of struggle finally made it guys.. and my learning is in progress..will be posting
on in case of queries.
Thank you evryone for replying...
Cheers
 
Stop it! You're embarassing me! And you are embarrassing this tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic