cancel
Showing results for 
Search instead for 
Did you mean: 

Help needed in EJB

Former Member
0 Kudos

Hi friends ,

I need your help in EJB

I have to perform these operations 1) Calling an Appl Service from a EJB module. 2) Calling an EJB module from an Appl Service. 3) Basics of JNDI.

for the second part I am exposing the EJB module as an Web Service and then consuming it in the Application Service as an External Service(that EJB has a Session Bean with a local Interface that has some logic to be exposed ).(Any further suggestions for this !!!!)

1)But I still dont know how to call an Application Service from an EJB module . 3)And finally I have an Application Service which would have a JNDI strings pointing to the Local EJB Interfaces and I have to access any one string from a Application Service and call that Local EJB Interface(local EJB invocation)

So can anyone help me in these two areas

Please keep in mind that I am relatively new to J2EE.

Thanks in advance

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hey Brian,

Firstly about the links, following are the Links specific to CE:

1. [Genarate Web Service from Application Service|http://help.sap.com/saphelp_nwce10/helpdata/en/44/24128b347c1c9fe10000000a11466f/frameset.htm]

2. [Creating Web Service Proxies |http://help.sap.com/saphelp_nwce10/helpdata/en/45/100c6be9f87201e10000000a155369/frameset.htm]

3. [Creating Clients |http://help.sap.com/saphelp_nwce10/helpdata/en/45/113a1221337249e10000000a155369/frameset.htm]

Next, here is something about [JNDI|http://help.sap.com/saphelp_nwce10/helpdata/en/99/e8b84172133131e10000000a155106/frameset.htm.].

Now, Concepts of EJB and EAR.

EJB, in a very laymen language, are server-side components that encapsulate the business logic of an application.

In connection with JNDI,

The EJBs, as Java EE component, need to “find” each other. They need a registry to expose themselves.

Such a registry is the Java Naming and Directory Interface (JNDI). It is an application programming interface

(API) that provides naming and directory functionality to applications written in Java. It is independent of any

specific directory service implementations. The *main functions of the JNDI API are to bind, unbind and lookup

different objects, which have specific names*.

EAR, is the deployable archive for the JAR created by the EJB. As EJB is a Server-Side component it needs to

be deployed on the server.

Also, about the code snippet, the code given was for older versions.

For CE you may refer to [this|http://help.sap.com/saphelp_nwce10/helpdata/en/45/e692b2cfaa5591e10000000a1553f7/frameset.htm].

Kindly search well for more concepts on EJB on any search engine.

Regards,

Alka.

Edited by: Alka Panday on May 7, 2008 9:50 AM

Edited by: Alka Panday on May 7, 2008 9:52 AM

Former Member
0 Kudos

Hi Alka ,

I tried all the options I know, I have been strictly told not to use Web Services as all the applications are on the same server

actually I have to make an EJB invocation to an Application Service from the CAF layer.

the CAF layer has 5 parts metadata , permissions , dictionary , ear and ejbmodule.

I added the ejbModule to the Build Path of the calling EJB Session Bean. And so I was able to access the operations of the Application Service which in turn accesses a BAPI.

But while executing I got the exception java.lang.RuntimeException: java.lang.NoClassDefFoundError

My method :

Normally from a Dynamic Web Project acting as a client to an EJB Session bean, I was able to get access to EJB Local Interface by using


  private ConverterLocal converterBean = null;
  public void jspInit() {
    try {
     InitialContext ic = new InitialContext();
     converterBean = (ConverterLocal)
     ic.lookup("java:comp/env/Converter");
    } catch (Exception ex) {
   	 System.out.println("Couldn't create converter bean."+
       ex.getMessage());
    }
  }
.
.
.
BigDecimal dollarAmount =  converterBean.euroToDollar(value);

So I created a JNDI field in ejb-j2ee-engine.xml


enterprise-beans>
        <enterprise-bean>
	<ejb-name>com.sap*******.bapi_company_getlist.BAPI_COMPANY_GETLIST</ejb-name>
               <jndi-name>COMPANY</jndi-name>
    	   </enterprise-bean>
</enterprise-beans>

And from my Session Bean , I tried to access it using the code in already existing Bean which was working fine


BAPI_COMPANY_GETLISTBeanImpl bean = new BAPI_COMPANY_GETLISTBeanImpl();
		try {
			InitialContext ctx = new InitialContext();
			bean = (BAPI_COMPANY_GETLISTBeanImpl)ctx.lookup("COMPANY");
		} catch (NamingException e) {
			System.out.println("Converter Bean can't be created");
		}
		
		
			String send = bean.returnDetails();

And I tried to print the String , but I got the above error

I have a doubt :

1) How do I specify the JNDI name ie in the ejb-j2ee-engine.xml file , what should I add as the name of the ejb

is this sufficient , or are there more steps ??

Former Member
0 Kudos

Hi Alka ,

Can You help me with this basic example.

Suppose I have HelloWorldEJB and ChildEJB EJB projects.

and HelloWorldEJB(the one given in the std examples http://help.sap.com/saphelp_nwce10/helpdata/en/46/923ba726d4581de10000000a1553f7/content.htm )

and that is exposed as a WebService via which I get a message as output.

So I just want to access the ChildEJB which will have a Local Interface , and one method returning a String , and I want to access that String in HelloWorldEJB and print it.

Please tell me how should I add the dependencies for each.

should I be adding @EJB private ChildEJB

in HelloWorldEJB ???????????

or should I be using InitialContext.lookup.

All my applications are on the same server and I generally only need the Local Interfaces.

Former Member
0 Kudos

Hi ,

I have the document to access my Bean from another Bean.

I could access my Bean from a standalone Java class with this code


Properties props = new Properties();
		props.put(Context.INITIAL_CONTEXT_FACTORY,"com.sap.engine.services.jndi.InitialContextFactoryImpl");
		props.put(Context.PROVIDER_URL,"hostname:port");
		
		
		try{
			Context ctx = new InitialContext(props);
			Object o = ctx.lookup("sap.com/TestEAR/REMOTE/TestBean/com.@#$.TestRemote");
			TestRemote testRem = (TestRemote)javax.rmi.PortableRemoteObject.narrow(o, TestRemote.class);
			String msg = testRem.getMessage();
			System.out.println(msg);
			

And in this document there is a way shown to access a bean from another Bean.

http://help.sap.com/saphelp_nw04/helpdata/en/13/c8633e0084e946e10000000a114084/frameset.htm

If your client is another enterprise bean, declare the EJB reference in the ejb-jar.xml.

-For more information, see Declaring EJB References.

But the problem is that in EJB 3.0 , I can't see the ejb-jar.xml file .

So how should I have a Bean as a client to another Bean.

Former Member
0 Kudos

Hey Brian,

In CE, the ejb-jar.xml is available under,

Project Name --> Build --> Classes --> META-INF

folder sturcture...

Also while calling a Bean from a Bean you need not specify the properties for initial context.

Regards,

Alka.

Former Member
0 Kudos

No Alka, I searched in that location ,but I didn't get it .

But I was able to complete the action what I wanted.

I can now access a EJB from another EJB.

The problem was in runtime.

Finally I used a common EAR file for both the EJBs and it worked.

Now I am trying to access a Composite Application Service from an EJB.

Hope that works.

Thanks for all your help.

I shall contact you if I need any help.

Thanks again

Former Member
0 Kudos

Hey Brian,

The ejb-jar.xml file is vailable under the said location, but, make sure that you are in Java EE perspective, with Project Explorer view. This can also be verified in Navigator View under any perspective.

AFAIK, when working with DCs, if you want to access other DC content in your DC, you need to create a DC dependency.

That means, you will have to include the Public Parts of the DC being Accessed in the DC which access it.

This can be done in the Dependencies Tab.

For further reference see [this|http://help.sap.com/saphelp_nwce10/helpdata/en/45/4e792ff73b1192e10000000a1553f7/frameset.htm].

Regards,

Alka.

Answers (1)

Answers (1)

Former Member
0 Kudos

Hi Brian,

>

> 1)But I still dont know how to call an Application Service from an EJB module .

For this, you can

1. [Generate Web Service for Application Service|http://help.sap.com/saphelp_nw70/helpdata/EN/44/24128b347c1c9fe10000000a11466f/frameset.htm].

2. [Create Deployable Proxy to consume Web Service|http://help.sap.com/saphelp_nw70/helpdata/EN/2d/b9766df88f4a24967dae38cb672fe1/frameset.htm].

3. [Create a Client Session Bean|http://help.sap.com/saphelp_nw70/helpdata/EN/45/029840cf43495195da923f32262911/content.htm] to consume this proxy.

Hope it helps.

Regards,

Alka.

Former Member
0 Kudos

Hi Alka ,

Thank you so much for your help.

Please do me one favour .

I am finding it difficult to grasp with JNDI.

I have a Application Service in CAF which holds the JNDI strings pointing to the local EJB references , I can then select any one string , and then invoke that EJB

If you do have any example with concrete steps , please send it. Awaiting your reply.

Thanks in advance ,

and thanks for the help you already gave.

Former Member
0 Kudos

Hi Brian,

I'm not too well versed with the application and usage of Application Services.

But, as per the problem description that you have given,

Given a JNDI name of the EJB you need to invoke it.

The code can be as:

InitialContext ctx = new InitialContext();
EntityServiceHome home = (EntityServiceHome)ctx.lookup("EJB_JNDI_NAME");
EntityService rem = home.create();
output = rem.methodName1();

To specify the JNDI name of the EJB, in the EJB Project:

1. Open ejb-j2ee-engine.xml.

2. Under Enterprise Beans Tab, select your Session Bean

3. On the right hand side, enter the JNDI Name.

Hope this is helpful.

Regards,

Alka

Former Member
0 Kudos

Hi Alka ,

In my ejb-j2ee-engine.xml this is my code


<?xml version="1.0" encoding="UTF-8"?>
<ejb-j2ee-engine xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ejb-j2ee-engine_3_0.xsd">
	<enterprise-beans>
		<enterprise-bean>
			<ejb-name>ejb-name</ejb-name>
		</enterprise-bean>
	</enterprise-beans>
</ejb-j2ee-engine>

where do I speify the Session Bean name and the JNDI name ?

Former Member
0 Kudos

Hi Brian,

No problems. I am eager to help.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE ejb-j2ee-engine SYSTEM "ejb-j2ee-engine.dtd">
<ejb-j2ee-engine>
	<enterprise-beans>
		<enterprise-bean>
			<ejb-name>CalculatorBean</ejb-name>
			<jndi-name>CALCBEAN</jndi-name>
			<session-props/>
		</enterprise-bean>
	</enterprise-beans>
</ejb-j2ee-engine>

Make a note of the tag jndi-name used to specify the JNDI Name for the Bean.

The code given in the above post can be used to invoke the EJB from any Java based service.

You are right in saying that you need not create a Web Service of your EJB to consume it. That was one of the alternative solutions.

Regards,

Alka.

Former Member
0 Kudos

Hi Alka ,

thank you so much for all your help.

It has really been useful for me as a beginner.

Now the only question I want to ask is (from an conceptual point of view)

what is the use of the EAR project we add when we create an EJB project and then we add the same EAR file when we create a web module to create the client.

I am asking this because

In my CAF layer ie in my Application Service I have 5 components metadata , permissions , dictionary , ejbmodule , ear

In my ejbmodule(of EJB Project) I have the ejb-j2ee-engine.xml which I need to update (This is to access the EJB project from the Application Service) ,

so now when I create the EJB project should I add the ear file of my Application Service to the EAR(when I am creating the EJB project)

Similarly when I access the Application Service from the EJB module , in the ejbmodule of my Application Service I would edit the ejb-j2ee-engine.xml file , then I would call the Appln Service from my EJB module . So even here what should I do about the EAR part of my Application Service.

Former Member
0 Kudos

InitialContext ctx = new InitialContext();

EntityServiceHome home = (EntityServiceHome)ctx.lookup("EJB_JNDI_NAME");

EntityService rem = home.create();

output = rem.methodName1();

I tried writing this code , but I got an error at the second line .

do I have to import anything before I write EntityServiceHome home.........

or do I need to add something as dependency