wood burning stoves 2.0*
The moose likes JBoss/WildFly and the fly likes JBoss and Head First EJB Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Products » JBoss/WildFly
Bookmark "JBoss and Head First EJB" Watch "JBoss and Head First EJB" New topic
Author

JBoss and Head First EJB

John Gregory
Ranch Hand

Joined: Oct 05, 2006
Posts: 115
Ok, in the HF Ejb book, they talk about the server kicking out
a .jar file with the stub. Does JBoss do this? With the j2ee
1.4, is it need anymore?

As for the jndi.properties file, it goes in with my AdviceClient.java,
correct?

Thanks,

John Gregory
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10207
    
166

they talk about the server kicking out
a .jar file with the stub. Does JBoss do this?


JBoss creates dynamic proxies (i.e. proxies on the fly). As far as i know it does not create any jar file for saving these proxies.

As for the jndi.properties file, it goes in with my AdviceClient.java,
correct?


The jndi.properties is required for specifying the application server specific JNDI environment. JBoss has a default jndi.properties in the server/< serverName>/conf folder, which is used by server side classes. However, if there is a standalone java class (for example the AdviceClient), then the standalone application has to have to its own jndi.properties in its classpath.


[My Blog] [JavaRanch Journal]
John Gregory
Ranch Hand

Joined: Oct 05, 2006
Posts: 115
Ok, just for arguements sake, I created a jndi.properties
file in my AdviceClient classpath. I put the line:
Advisor=AdviceBean
in the properties file. Nothing, yet...I took a break to
check any resonse.

Ok, what are the "Stub" files the Head First book uses?
Supposedly, they're generated by the container and thrown
into a jar file returned to somebody...I'm not sure how/where.
I know they're using a different server, so the process is
going to be different...a bit.

As for my AdviceClient, I get it to compile, but don't get
any advice...

John
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10207
    
166

Originally posted by John Gregory:
I created a jndi.properties
file in my AdviceClient classpath. I put the line:
Advisor=AdviceBean
in the properties file.


This file is supposed to contain the server specific JNDI environment entries. For JBoss, this file should contain:



You will have to substitute localhost:1099 with the appropriate server name.

Originally posted by John Gregory:
Ok, what are the "Stub" files the Head First book uses?
Supposedly, they're generated by the container and thrown
into a jar file returned to somebody...I'm not sure how/where.
I know they're using a different server, so the process is
going to be different...a bit.


Unfortunately, i haven't gone through that book. But the part that you mention above is right. Each server generates the "Stubs" for the beans. JBoss too does the same. It generates these stubs on the fly (dynamically). It then returns these stubs to the clients whenever the client does a lookup of the bean and performs operation on that bean.

Originally posted by John Gregory:
As for my AdviceClient, I get it to compile, but don't get
any advice...


Do you see any exceptions?
[ October 24, 2007: Message edited by: Jaikiran Pai ]
John Gregory
Ranch Hand

Joined: Oct 05, 2006
Posts: 115
Jaikiran,

Ok, I got my program to work, but don't understand why.

What's the mapping between ejb-jar.xml and jboss.xml file?

If I have an EJB named

bean name --> StringProcessorBean
bean home --> StringProcessorHome
bean remote --> StringProcessor

what should go in my jboss.xml given that I've got stuff mapped
as it should in my ejb-jar.xml?

Thanks,

John
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10207
    
166

In the jboss.xml you can specify the custom JNDI name for your bean. By default your bean will be bound to a JNDI name. If you want the bean to be bound to a specific jndi name of your choice, you can do so in the jboss.xml file. Something like:



There are many other things that you can configure in jboss.xml, but this one is the simplest and most relevant to the example we are considering here.
John Gregory
Ranch Hand

Joined: Oct 05, 2006
Posts: 115
Jaikiran,

Ok, for my ejb-jar.xml file:

<?xml version="1.0" encoding="UTF-8"?>

<ejb-jar>
<description>Your first EJB application </description>
<display-name>String Processor Application</display-name>
<enterprise-beans>
<session>
<ejb-name>StringProcessor</ejb-name>
<home>com.javapro.ejb.StringProcessorHome</home>
<remote>com.javapro.ejb.StringProcessor</remote>
<ejb-class>com.javapro.ejb.StringProcessorBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Bean</transaction-type>
</session>
</enterprise-beans>
</ejb-jar>

and my client code:

import javax.naming.*;
import javax.rmi.PortableRemoteObject;
import java.util.Properties;
import com.javapro.ejb.StringProcessor;
import com.javapro.ejb.StringProcessorHome;

public class Client {

public static void main(String[] args) {

// first argument must be the input
if (args.length==0) {
System.out.println("Please specify the input to convert to upper case.");
return;
}
String input = "hello world"

// preparing properties for constructing an InitialContext object
Properties properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
properties.put(Context.PROVIDER_URL, "localhost:1099");

try {
// Get an initial context
InitialContext jndiContext = new InitialContext(properties);
System.out.println("Got context");

// Get a reference to the Bean
Object ref = jndiContext.lookup("StringProcessor");
System.out.println("Got reference");

// Get a reference from this to the Bean's Home interface
StringProcessorHome home = (StringProcessorHome)
PortableRemoteObject.narrow (ref, StringProcessorHome.class);

// Create an Adder object from the Home interface
StringProcessor sp = home.create();
System.out.println ("Uppercase of '" + input + "' is " +
sp.toUpperCase(input));
}
catch(Exception e) {
System.out.println(e.toString());
}
}
}

does the StringProcessor in the jndiContext.lookup("StringProcessor") look
at the <ejb-name> in the ejb-jar.xml file? Do I even need the jboss.xml?
As of now, I've not gotten it to compile w/o the jboss file...I somehow
did something to it and now I can't get it to run again. In the meantime,
I am trying to figure out what name in the xml file maps to the lookup
in the client code.

John
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10207
    
166

does the StringProcessor in the jndiContext.lookup("StringProcessor") look
at the <ejb-name> in the ejb-jar.xml file?


Yes that's right. By default for EJB2.x, the jndi name of the bean will be the <ejb-name>. So in this case you can lookup the bean using the StringProcessor jndi name.

Do I even need the jboss.xml?


No, in this example, you don't need a jboss.xml. It's optional.
John Gregory
Ranch Hand

Joined: Oct 05, 2006
Posts: 115
Jaikiran,

Ok, thank you very much. That helps immensely. I think I
understand a couple things to get me started. In fact, both
using netbeans 5.5 and doing everything by hand, I got my
examples to work.

Again, thanks.

John
 
wood burning stoves
 
subject: JBoss and Head First EJB