Meaningless Drivel is fun!*
The moose likes EJB and other Java EE Technologies and the fly likes how to include @local session bean in existing hello world application? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "how to include @local session bean in existing hello world application?" Watch "how to include @local session bean in existing hello world application?" New topic
Author

how to include @local session bean in existing hello world application?

prashant gandhi
Greenhorn

Joined: May 21, 2013
Posts: 18
hi,
i am using ejb3, eclipse indigo, jboss AS 7.1.1.final. i have written simple hello world application using @remote session bean successfully. i have added another @remote session bean successfully too, which is printing "hello world2" no issues. for this, i am using following jboss-ejb-client.property file.

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.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS=false

now, i want to add the @local session bean to it which is similar to hello world and prints "hello world3" but it doesnt work. i get errors when i do ctrl+F11 - proxy0 errors. i believe this is happening because i am using the remote port 4447 in above property file. am i correct? can someone tell me what is missing that would make this @local session bean work in my simple day1 program? thanks.
Prajakta Acharya
Ranch Hand

Joined: Nov 08, 2012
Posts: 138

Hi Prashant,

I am not very sure what is happening here.But logically, remote look-up specific features should be referred to by the server while doind remote look-ups only.
What does following configuration mean?
remote.connections=default

Can you share your project structure? Where are the local EJBs located in your application?


Regards,
Prajakta
prashant gandhi
Greenhorn

Joined: May 21, 2013
Posts: 18
Thanks for your reply prajakta.

I have followed the below link that shows how to setup the hello world project.

http://theopentutorials.com/examples/java-ee/ejb3/how-to-create-a-simple-ejb3-project-in-eclipse-jboss-7-1/

it worked for me.

Then I could enhance the project to add one more remote (same type) bean called: HelloWorldBeanTestRemote - which does the exact same thing and prints "hello world2". As a result, I get output:

Hello World
Hello World2

Now, I want to add one more bean to it which is of type @local.

basically, i am trying to learn @local and @remote types of beans as you can see. This is my first ever work in Java.

The project is about upgrading Jboss 4.0.1 sp1 to Jboss 7.1.1.final + upgrade the ejb2 to ejb3. Right now i am trying to get handle on ejb - what is in ejb2 - how to upgrade the project to ejb3 and what are the session, entity and message driven beans are - how to use them etc etc.

Current code was written by someone back in about 2004 and it uses xdoclet/ejbdoclet heavily - so many .java files get produced every time you compile/run. I need to work around that too, but that is a later part.

Thanks again for your help.
Prajakta Acharya
Ranch Hand

Joined: Nov 08, 2012
Posts: 138

Hi Prashant,

I have never used any configuration file for JBOSS to look-up remote EJBs, so cant comment much on that set-up.

What is the actual exception thrown?

Just wanted to check following points:
1) Have you annotated your local interface with @Local?
2) Have you created a session bean to implement that local interface?
3) Once your EJB project is deployed, can you see the JNDI names of that local bean on the console? What values do you see?
4) Can you try the following?
Give some name to your EJB in @Stateless(name="someName").
5) In your client code, use @EJB(lookup="<look-up-name-displayed-on-console>") and check the result.

I assume that your application set-up is correct , just check if there are any class loading issues. (This is possible when the interfaces specific jar exists a more than once place in the deployment location).


prashant gandhi
Greenhorn

Joined: May 21, 2013
Posts: 18
This is the exact error message:

Jun 24, 2013 10:14:39 AM org.jboss.ejb.client.remoting.NoSuchEJBExceptionResponseHandler processMessage
INFO: Retrying invocation which failed on node uslcnd11238bx with exception:
javax.ejb.NoSuchEJBException: No such EJB[appname=,modulename=HelloWorldSessionBean,distinctname=,beanname=HelloWorldBeanTestLocal,viewclassname=com.ibytecode.business.HelloWorldTestLocal]
at org.jboss.ejb.client.remoting.NoSuchEJBExceptionResponseHandler.processMessage(NoSuchEJBExceptionResponseHandler.java:64)
at org.jboss.ejb.client.remoting.ChannelAssociation$ResponseReceiver.handleMessage(ChannelAssociation.java:395)
at org.jboss.remoting3.remote.RemoteConnectionChannel$5.run(RemoteConnectionChannel.java:437)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Exception in thread "main" java.lang.IllegalStateException: No EJB receiver available for handling [appName:,modulename:HelloWorldSessionBean,distinctname:] combination for invocation context org.jboss.ejb.client.EJBClientInvocationContext@b01d43
at org.jboss.ejb.client.EJBClientContext.requireEJBReceiver(EJBClientContext.java:584)
at org.jboss.ejb.client.ReceiverInterceptor.handleInvocation(ReceiverInterceptor.java:119)
at org.jboss.ejb.client.EJBClientInvocationContext.sendRequest(EJBClientInvocationContext.java:181)
at org.jboss.ejb.client.EJBClientInvocationContext.retryRequest(EJBClientInvocationContext.java:206)
at org.jboss.ejb.client.EJBReceiverInvocationContext.retryInvocation(EJBReceiverInvocationContext.java:95)
at org.jboss.ejb.client.remoting.NoSuchEJBExceptionResponseHandler.processMessage(NoSuchEJBExceptionResponseHandler.java:78)
at org.jboss.ejb.client.remoting.ChannelAssociation$ResponseReceiver.handleMessage(ChannelAssociation.java:395)
at org.jboss.remoting3.remote.RemoteConnectionChannel$5.run(RemoteConnectionChannel.java:437)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
at ...asynchronous invocation...(Unknown Source)
at org.jboss.ejb.client.remoting.NoSuchEJBExceptionResponseHandler$ResultProducer.getResult(NoSuchEJBExceptionResponseHandler.java:101)
at org.jboss.ejb.client.EJBClientInvocationContext.getResult(EJBClientInvocationContext.java:270)
at org.jboss.ejb.client.TransactionInterceptor.handleInvocationResult(TransactionInterceptor.java:47)
at org.jboss.ejb.client.EJBClientInvocationContext.getResult(EJBClientInvocationContext.java:272)
at org.jboss.ejb.client.ReceiverInterceptor.handleInvocationResult(ReceiverInterceptor.java:132)
at org.jboss.ejb.client.EJBClientInvocationContext.getResult(EJBClientInvocationContext.java:260)
at org.jboss.ejb.client.EJBClientInvocationContext.awaitResponse(EJBClientInvocationContext.java:399)
at org.jboss.ejb.client.EJBInvocationHandler.doInvoke(EJBInvocationHandler.java:140)
at org.jboss.ejb.client.EJBInvocationHandler.doInvoke(EJBInvocationHandler.java:121)
at org.jboss.ejb.client.EJBInvocationHandler.invoke(EJBInvocationHandler.java:104)
at $Proxy2.sayHellofromLocal(Unknown Source)
at com.ibytecode.client.EJBApplicationClient.main(EJBApplicationClient.java:28)


--------------

HelloWorldTestLocal.java

package com.ibytecode.business;

import javax.ejb.Local;

@Local
public interface HelloWorldTestLocal {
public String sayHellofromLocal();
}

--------------

HelloWorldBeanTestLocal.java


package com.ibytecode.businesslogicTestLocal;

import com.ibytecode.business.HelloWorldTestLocal;
import javax.ejb.Stateless;

/**
* Session Bean implementation class HelloWorldBeanTestLocal
*/
@Stateless
public class HelloWorldBeanTestLocal implements HelloWorldTestLocal {

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

public String sayHellofromLocal(){
return "Hello World from Local Bean!!";
}
}


----------------------------------

EJBApplicationClient.java

package com.ibytecode.client;

import javax.naming.Context;
import javax.naming.NamingException;

import com.ibytecode.business.HelloWorld;
import com.ibytecode.business.HelloWorldTestLocal;
import com.ibytecode.business.HelloWorldTestRemote;
import com.ibytecode.businesslogic.HelloWorldBean;
import com.ibytecode.businesslogicTestRemote.HelloWorldBeanTestRemote;
import com.ibytecode.businesslogicTestLocal.HelloWorldBeanTestLocal;
import com.ibytecode.clientutility.ClientUtility;

public class EJBApplicationClient {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
HelloWorld bean = doLookup();
System.out.println(bean.sayHello()); // 4. Call business logic
HelloWorldTestRemote beanTestRemote = doLookupTestRemote();
System.out.println(beanTestRemote.typeHello());

HelloWorldTestLocal beanTestLocal = doLookupTestLocal();
System.out.println("will now print the sayHellofromLocal");
System.out.println(beanTestLocal.sayHellofromLocal()); --> error generated due to this line.
// System.out.println("printed");

}

private static HelloWorldTestLocal doLookupTestLocal() {
Context context = null;
HelloWorldTestLocal bean = null;
try {
context = ClientUtility.getInitialContext();
String lookupName = "ejb:/HelloWorldSessionBean//HelloWorldBeanTestLocal!com.ibytecode.business.HelloWorldTestLocal";
System.out.println(lookupName);
bean = (HelloWorldTestLocal) context.lookup(lookupName);
System.out.println("came here");
} catch (NamingException e) {
e.printStackTrace();
}
System.out.println("came here too");
return bean;
}

private static HelloWorldTestRemote doLookupTestRemote() {
Context context = null;
HelloWorldTestRemote bean = null;
try {
// 1. Obtaining Context
context = ClientUtility.getInitialContext();
// 2. Generate JNDI Lookup name
String lookupName = getLookupNameTestRemote();
// 3. Lookup and cast
System.out.println(lookupName);
bean = (HelloWorldTestRemote) context.lookup(lookupName);

} catch (NamingException e) {
e.printStackTrace();
}
return bean;
}
private static HelloWorld doLookup() {
Context context = null;
HelloWorld bean = null;
try {
// 1. Obtaining Context
context = ClientUtility.getInitialContext();
// 2. Generate JNDI Lookup name
String lookupName = getLookupName();
// 3. Lookup and cast
bean = (HelloWorld) context.lookup(lookupName);

} catch (NamingException e) {
e.printStackTrace();
}
return bean;
}

private static String getLookupName() {
/*
The app name is the EAR name of the deployed EJB without .ear suffix.
Since we haven't deployed the application as a .ear,
the app name for us will be an empty string
*/
String appName = "";

/* The module name is the JAR name of the deployed EJB
without the .jar suffix.
*/
String moduleName = "HelloWorldSessionBean";

/*AS7 allows each deployment to have an (optional) distinct name.
This can be an empty string if distinct name is not specified.
*/
String distinctName = "";

// The EJB bean implementation class name
String beanName = HelloWorldBean.class.getSimpleName();

// Fully qualified remote interface name
final String interfaceName = HelloWorld.class.getName();

// Create a look up string name
String name = "ejb:" + appName + "/" + moduleName + "/" +
distinctName + "/" + beanName + "!" + interfaceName;

return name;
}

private static String getLookupNameTestRemote() {
/*
The app name is the EAR name of the deployed EJB without .ear suffix.
Since we haven't deployed the application as a .ear,
the app name for us will be an empty string
*/
String appName = "";

/* The module name is the JAR name of the deployed EJB
without the .jar suffix.
*/
String moduleName = "HelloWorldSessionBean";

/*AS7 allows each deployment to have an (optional) distinct name.
This can be an empty string if distinct name is not specified.
*/
String distinctName = "";

// The EJB bean implementation class name
String beanName = HelloWorldBeanTestRemote.class.getSimpleName();

// Fully qualified remote interface name
final String interfaceName = HelloWorldTestRemote.class.getName();

// Create a look up string name
String name = "ejb:" + appName + "/" + moduleName + "/" +
distinctName + "/" + beanName + "!" + interfaceName;

return name;
}

}


------------------------------------

Thanks.
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1505
    
    5

Hi prashant,

Welcome to CodeRanch!

Please UseCodeTags.

Regarding your question, if you are using local client view of an EJB (i.e. local interface) then it is mandatory that your client is collocated with the same JVM (simply running client and server on same node is not enough).

Thus, if your client and EJB are not in the same JVM, you cannot use local client view of the EJB.

I hope this answers your question.

Correct me if I'm wrong.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Prajakta Acharya
Ranch Hand

Joined: Nov 08, 2012
Posts: 138

Hi Prashant,

Just a request. Please use code tags while posting the code. It becomes more readable.

By looking at the code, I noticed that you are using the same method ClientUtility.getInitialContext() to lookup your local as well as remote EJB.
Can you post that code as well?



prashant gandhi
Greenhorn

Joined: May 21, 2013
Posts: 18
This is how the getInitialContext() looks like.

prashant gandhi
Greenhorn

Joined: May 21, 2013
Posts: 18
Anayonkar Shivalkar wrote:Hi prashant,

Welcome to CodeRanch!

Please UseCodeTags.

Regarding your question, if you are using local client view of an EJB (i.e. local interface) then it is mandatory that your client is collocated with the same JVM (simply running client and server on same node is not enough).

Thus, if your client and EJB are not in the same JVM, you cannot use local client view of the EJB.

I hope this answers your question.

Correct me if I'm wrong.


How do i make sure that I am not running client and server on same node or not?
How do I make sure that the my client is collocated with the same JVM?
How to check if my client and EJB are in same JVM or not?

Sorry, but I am very new to EJB world, trying to learn hello world with @local and @remote

Thanks for your help.
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1505
    
    5

prashant gandhi wrote:How do i make sure that I am not running client and server on same node or not?
How do I make sure that the my client is collocated with the same JVM?
How to check if my client and EJB are in same JVM or not?

1) By same node, I meant to say the same logical host (from container pov, if you are using 2 virtual machines on same host, those machines are still considered as different nodes).
2) If your client is standalone POJO, and you are using another 'java' command to run it, then clearly, you are starting a new JVM, and your server and client are not collocated in same JVM.
3) Same as above. Examples of client and EJB being in same JVM might be: a) Client to the EJB being another EJB (which is in same container); b) Client to the EJB being a servlet (which is in same container)

HIH
 
 
subject: how to include @local session bean in existing hello world application?