There are two ways to do it.
1.
package session.bean; public interface StatelessRemote { public String helloWorld(); } package session.bean; import javax.ejb.Remote; import javax.ejb.Stateless; @Stateless(name="abc", mappedName="def") @Remote(StatelessRemote.class) public class StatelessBean implements StatelessRemote { public String helloWorld() { return "Hello World"; } }
2.
package session.bean; import javax.ejb.Remote; @Remote public interface StatelessRemote { public String helloWorld(); } package session.bean; import javax.ejb.Stateless; @Stateless(name="abc", mappedName="def") public class StatelessBean implements StatelessRemote { public String helloWorld() { return "Hello World"; } }
Deploy the project into Glassfish 3.1 server
Ensure Glassfish is running, right click on GlassFish, and select 'Add Deployment'
Select the new created ejb project, click on 'Finish' button
The process is deployment is package the ejb project into a jar file, and copy that jar file to D:\glassfish3\glassfish\domains\domain1\autodeploy, (Assume the installation folder of Glassfish is D:\glassfish3\)
Check the JNDI name in the console.
The above 4 JNDI names can be used to find the statelessBean we just defined.
Create a Java project to test it.
public class Main { /** * @param args */ public static void main(String[] args) throws Exception{ Context ctx = new InitialContext(); StatelessRemote bean = (StatelessRemote) ctx.lookup ("java:global/ejb/abc!session.bean.StatelessRemote"); System.out.println(bean.helloWorld()); bean = (StatelessRemote)ctx.lookup("java:global/ejb/abc"); System.out.println(bean.helloWorld()); bean = (StatelessRemote) ctx.lookup ("def#session.bean.StatelessRemote"); System.out.println(bean.helloWorld()); bean = (StatelessRemote)ctx.lookup("def"); System.out.println(bean.helloWorld()); } }
The difference between name attribute and mappedName attribute is: The value of mappedName is unique in a server and it's not portable. (When you change to a different server, it may not work)